/** * @param null $instance * @param array $parameters * @param Manager $manager * @return mixed * @throws Exception */ function launch($instance = null, $parameters, Manager $manager) { $arguments = array(); foreach ($this->arguments as $index => $argument) { if (is_string($argument) && isset($parameters[$argument])) { $arguments[] = $parameters[$argument]; } elseif ($argument instanceof Reference) { // find parameter by class $foundInParams = false; foreach ($parameters as $param) { if (is_a($param, $argument->getClass())) { $arguments[] = $param; $foundInParams = true; break; } } if (!$foundInParams) { $arguments[] = $argument->getInstance($manager); } } else { if (count(array_filter(array_keys($parameters), 'is_int')) > 0) { $arguments[] = array_shift($parameters); } else { if ($index < $this->requiredCount) { throw new Exception(sprintf("Key %s for method %s::%s not found!", $argument, $this->class, $this->method)); } } } } if ($this->method == '__construct') { if (!count($arguments)) { return new $this->class(); } return Reflection::getReflectionClass($this->class)->newInstanceArgs($arguments); } if (in_array($this->method, $manager->getInspector()->getPublicMethods($this->class))) { return call_user_func_array(array($instance, $this->method), $arguments); } return Reflection::getReflectionMethod($this->class, $this->method)->invokeArgs($instance, $arguments); }
/** * @param $class * @return array */ public function getClassInjection($class) { if ($this->getCache()->contains(__METHOD__, func_get_args())) { return $this->getCache()->get(__METHOD__, func_get_args()); } $injection = array(); $reflectionClass = Reflection::getReflectionClass($class); foreach ($reflectionClass->getProperties() as $property) { if (stristr($property->getDocComment(), '@inject') || stristr($property->getDocComment(), '@new')) { foreach (explode("\n", $property->getDocComment()) as $line) { if (!stristr($line, '@var')) { continue; } foreach (explode(' ', substr($line, stripos($line, '@var') + 4)) as $item) { if (strlen($item) > 0) { $global = false; if ($item[0] == '\\') { $global = true; $item = substr($item, 1); } $injected_class = trim(str_replace("\r", '', $item)); if (!$global) { $aliases = array(); foreach (file($property->getDeclaringClass()->getFileName()) as $line) { if (strpos($line, 'use ') === 0) { $line = substr($line, 0, strpos($line, ';')); $chain = array_filter(explode(' ', $line), 'strlen'); if (strpos($line, ' as ')) { $destination = $chain[1]; $alias = $chain[3]; } else { $destination = $chain[1]; list($alias) = array_reverse(explode("\\", $chain[1])); } $aliases[$alias] = $destination; } if (strpos($line, 'class ') === 0) { break; } } if (isset($aliases[$injected_class])) { // imported with use statement $injected_class = $aliases[$injected_class]; } elseif (!strstr($injected_class, '\\')) { $injected_class = $reflectionClass->getNamespaceName() . '\\' . $injected_class; } else { list($ns) = explode('\\', $injected_class); if (isset($aliases[$ns])) { $injected_class = $aliases[$ns] . substr($injected_class, strlen($ns)); } } } $injection[$property->getName()] = array('class' => $injected_class, 'new' => stristr($property->getDocComment(), '@new')); break; } } } } } $this->getCache()->set(__METHOD__, func_get_args(), $injection); return $injection; }
public function __construct($model) { $this->_model = $model; $reflectionClass = Reflection::getReflectionClass($model); $properties = $reflectionClass->getStaticProperties(); foreach (self::$_VALIDATION_TYPES as $type) { if (!empty($properties[$type])) { foreach ($properties[$type] as $validate) { $validate['type'] = $type; if (empty($validate['on']) || $validate['on'] === 'save') { array_push($this->_registeredValidates['create'], $validate); array_push($this->_registeredValidates['update'], $validate); continue; } if ($validate['on'] === 'create') { array_push($this->_registeredValidates['create'], $validate); continue; } if ($validate['on'] === 'update') { array_push($this->_registeredValidates['update'], $validate); continue; } } } } }