Hoa central
Public Member Functions | Protected Attributes | List of all members
Hoa\Core\Consistency\Xcallable Class Reference
Inheritance diagram for Hoa\Core\Consistency\Xcallable:

Public Member Functions

 __construct ($call, $able= '')
 
 __invoke ()
 
 distributeArguments (Array $arguments)
 
 getValidCallback (Array &$arguments=[])
 
 getHash ()
 
 getReflection ()
 
 __toString ()
 

Protected Attributes

 $_callback = null
 
 $_hash = null
 

Detailed Description

Class Hoa.

Build a callable object, i.e. function, class::method, object->method or closure, they all have the same behaviour. This callable is an extension of native PHP callable (aka callback) to integrate Hoa's structures.

Definition at line 652 of file Consistency.php.

Constructor & Destructor Documentation

Hoa\Core\Consistency\Xcallable::__construct (   $call,
  $able = '' 
)

Build a callback. Accepted forms: • 'function'; • 'class::method'; • 'class', 'method'; • $object, 'method'; • $object, ''; • function ( … ) { … }; • ['class', 'method']; • [$object, 'method'].

Parameters
mixed$callFirst callable part.
mixed$ableSecond callable part (if needed).
Returns
mixed

Definition at line 686 of file Consistency.php.

687  {
688  if (null === $call) {
689  return null;
690  }
691 
692  if ($call instanceof \Closure) {
693  $this->_callback = $call;
694 
695  return;
696  }
697 
698  if (!is_string($able)) {
699  throw new Core\Exception(
700  'Bad callback form.',
701  0
702  );
703  }
704 
705  if ('' === $able) {
706  if (is_string($call)) {
707  if (false === strpos($call, '::')) {
708  if (!function_exists($call)) {
709  throw new Core\Exception(
710  'Bad callback form.',
711  1
712  );
713  }
714 
715  $this->_callback = $call;
716 
717  return;
718  }
719 
720  list($call, $able) = explode('::', $call);
721  } elseif (is_object($call)) {
722  if ($call instanceof Stream\IStream\Out) {
723  $able = null;
724  } elseif (method_exists($call, '__invoke')) {
725  $able = '__invoke';
726  } else {
727  throw new Core\Exception(
728  'Bad callback form.',
729  2
730  );
731  }
732  } elseif (is_array($call) && isset($call[0])) {
733  if (!isset($call[1])) {
734  return $this->__construct($call[0]);
735  }
736 
737  return $this->__construct($call[0], $call[1]);
738  } else {
739  throw new Core\Exception(
740  'Bad callback form.',
741  3
742  );
743  }
744  }
745 
746  $this->_callback = [$call, $able];
747 
748  return;
749  }
__construct($call, $able= '')

Member Function Documentation

Hoa\Core\Consistency\Xcallable::__invoke ( )

Call the callable.

Parameters
...
Returns
mixed

Definition at line 757 of file Consistency.php.

758  {
759  $arguments = func_get_args();
760  $valid = $this->getValidCallback($arguments);
761 
762  return call_user_func_array($valid, $arguments);
763  }
getValidCallback(Array &$arguments=[])

Here is the call graph for this function:

Hoa\Core\Consistency\Xcallable::__toString ( )

Return the hash.

Returns
string

Implement a fake Throwable class, introduced in PHP7.0.

Definition at line 915 of file Consistency.php.

916  {
917  return $this->getHash();
918  }
919 }
920 
921 }
922 
923 namespace {
924 
925 
933 if (!function_exists('trait_exists')) {
934  function trait_exists($traitname, $autoload = true)
935  {
936  if (true == $autoload) {
937  class_exists($traitname, true);
938  }
939 
940  return false;
941  }
942 }
943 
944 if (70000 > PHP_VERSION_ID) {
948  interface Throwable
949  {
950  public function getMessage();
951  public function getCode();
952  public function getFile();
953  public function getLine();
954  public function getTrace();
955  public function getPrevious();
956  public function getTraceAsString();
957  public function __toString();
958  }
959 }
960 
967 if (!function_exists('from')) {
968  function from($namespace)
969  {
970  return Hoa\Core\Consistency::from($namespace);
971  }
972 }
973 
981 if (!function_exists('dnew')) {
982  function dnew($classname, Array $arguments = [])
983  {
984  return Hoa\Core\Consistency::dnew($classname, $arguments);
985  }
986 }
987 
995 if (!function_exists('xcallable')) {
996  function xcallable($call, $able = '')
997  {
998  if ($call instanceof Hoa\Core\Consistency\Xcallable) {
999  return $call;
1000  }
1001 
1002  return new Hoa\Core\Consistency\Xcallable($call, $able);
1003  }
1004 }
1005 
1026 if (!function_exists('curry')) {
1027  function curry($callable)
1028  {
1029  $arguments = func_get_args();
1030  array_shift($arguments);
1031  $ii = array_keys($arguments, …, true);
1032 
1033  return function () use ($callable, $arguments, $ii) {
1034  return call_user_func_array(
1035  $callable,
1036  array_replace($arguments, array_combine($ii, func_get_args()))
1037  );
1038  };
1039  }
1040 }
1041 
1049 if (!function_exists('curry_ref')) {
1050  function curry_ref(
1051  &$callable,
1052  &$a = null,
1053  &$b = null,
1054  &$c = null,
1055  &$d = null,
1056  &$e = null,
1057  &$f = null,
1058  &$g = null,
1059  &$h = null,
1060  &$i = null,
1061  &$j = null,
1062  &$k = null,
1063  &$l = null,
1064  &$m = null,
1065  &$n = null,
1066  &$o = null,
1067  &$p = null,
1068  &$q = null,
1069  &$r = null,
1070  &$s = null,
1071  &$t = null,
1072  &$u = null,
1073  &$v = null,
1074  &$w = null,
1075  &$x = null,
1076  &$y = null,
1077  &$z = null
1078  ) {
1079  $arguments = [];
1080 
1081  for ($i = 0, $max = func_num_args() - 1; $i < $max; ++$i) {
1082  $arguments[] = &${chr(97 + $i)};
1083  }
1084 
1085  $ii = array_keys($arguments, …, true);
1086 
1087  return function () use (&$callable, &$arguments, $ii) {
1088  return call_user_func_array(
1089  $callable,
1090  array_replace($arguments, array_combine($ii, func_get_args()))
1091  );
1092  };
1093  }
1094 }

Here is the call graph for this function:

Hoa\Core\Consistency\Xcallable::distributeArguments ( Array  $arguments)

Distribute arguments according to an array.

Parameters
array$argumentsArguments.
Returns
mixed

Definition at line 771 of file Consistency.php.

772  {
773  return call_user_func_array([$this, '__invoke'], $arguments);
774  }
Hoa\Core\Consistency\Xcallable::getHash ( )

Get hash. Will produce:

  • function::…;
  • class::…::…;
  • object(…)#…::…;
  • closure(…).
Returns
string

Definition at line 844 of file Consistency.php.

845  {
846  if (null !== $this->_hash) {
847  return $this->_hash;
848  }
849 
850  $_ = &$this->_callback;
851 
852  if (is_string($_)) {
853  return $this->_hash = 'function#' . $_;
854  }
855 
856  if (is_array($_)) {
857  return
858  $this->_hash =
859  (is_object($_[0])
860  ? 'object(' . spl_object_hash($_[0]) . ')' .
861  '#' . get_class($_[0])
862  : 'class#' . $_[0]) .
863  '::' .
864  (null !== $_[1]
865  ? $_[1]
866  : '???');
867  }
868 
869  return $this->_hash = 'closure(' . spl_object_hash($_) . ')';
870  }

Here is the caller graph for this function:

Hoa\Core\Consistency\Xcallable::getReflection ( )

Get appropriated reflection instance.

Parameters
...
Returns

Definition at line 878 of file Consistency.php.

879  {
880  $arguments = func_get_args();
881  $valid = $this->getValidCallback($arguments);
882 
883  if (is_string($valid)) {
884  return new \ReflectionFunction($valid);
885  }
886 
887  if ($valid instanceof \Closure) {
888  return new \ReflectionFunction($valid);
889  }
890 
891  if (is_array($valid)) {
892  if (is_string($valid[0])) {
893  if (false === method_exists($valid[0], $valid[1])) {
894  return new \ReflectionClass($valid[0]);
895  }
896 
897  return new \ReflectionMethod($valid[0], $valid[1]);
898  }
899 
900  $object = new \ReflectionObject($valid[0]);
901 
902  if (null === $valid[1]) {
903  return $object;
904  }
905 
906  return $object->getMethod($valid[1]);
907  }
908  }
getValidCallback(Array &$arguments=[])

Here is the call graph for this function:

Hoa\Core\Consistency\Xcallable::getValidCallback ( Array &  $arguments = [])

Get a valid callback in the PHP meaning.

Parameters
array&$argumentsArguments (could determine method on an object if not precised).
Returns
mixed

Definition at line 783 of file Consistency.php.

784  {
785  $callback = $this->_callback;
786  $head = null;
787 
788  if (isset($arguments[0])) {
789  $head = &$arguments[0];
790  }
791 
792  // If method is undetermined, we find it (we understand event bucket and
793  // stream).
794  if (null !== $head &&
795  is_array($callback) &&
796  null === $callback[1]) {
797  if ($head instanceof Core\Event\Bucket) {
798  $head = $head->getData();
799  }
800 
801  switch ($type = gettype($head)) {
802  case 'string':
803  if (1 === strlen($head)) {
804  $method = 'writeCharacter';
805  } else {
806  $method = 'writeString';
807  }
808 
809  break;
810 
811  case 'boolean':
812  case 'integer':
813  case 'array':
814  $method = 'write' . ucfirst($type);
815 
816  break;
817 
818  case 'double':
819  $method = 'writeFloat';
820 
821  break;
822 
823  default:
824  $method = 'writeAll';
825  $head = $head . "\n";
826  }
827 
828  $callback[1] = $method;
829  }
830 
831  return $callback;
832  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Core\Consistency\Xcallable::$_callback = null
protected

Definition at line 659 of file Consistency.php.

Hoa\Core\Consistency\Xcallable::$_hash = null
protected

Definition at line 666 of file Consistency.php.


The documentation for this class was generated from the following file: