/** * @param $instance */ public function inject(&$instance) { $reader = new MemoryAdapter(); $reflector = $reader->get('\\' . get_class($instance)); $annotations = $reflector->getPropertiesAnnotations(); $this->injectToProperties($instance, $annotations); }
/** * Search annotations */ private function loadAnnotations() { $reader = new Memory(); $reflector = $reader->get(get_called_class()); $getMethodsAnnotations = $reflector->getMethodsAnnotations(); foreach ($getMethodsAnnotations as $methodName => $annotations) { if ($methodName == $this->getName()) { foreach ($annotations as $annotation) { if ($annotation->getName() == TestCase::ANNOTATION_FIXTURE) { $fixtureClass = new DataFixture(); $fixtureClass->loadJson($annotation->getArguments()); } } } } }
private function createHelp() { $scannedTasksDir = array_diff(scandir($this->tasksDir), array('..', '.')); $config = $this->getDI()->get('config'); $dispatcher = $this->getDI()->getShared('dispatcher'); $namespace = $dispatcher->getNamespaceName(); if (isset($config['annotationsAdapter']) && $config['annotationsAdapter']) { $adapter = '\\Phalcon\\Annotations\\Adapter\\' . $config['annotationsAdapter']; if (class_exists($adapter)) { $reader = new $adapter(); } else { $reader = new MemoryAdapter(); } } else { $reader = new MemoryAdapter(); } foreach ($scannedTasksDir as $taskFile) { $taskFileInfo = pathinfo($taskFile); $taskClass = ($namespace ? $namespace . '\\' : '') . $taskFileInfo["filename"]; $taskName = strtolower(str_replace('Task', '', $taskFileInfo["filename"])); $this->documentation[$taskName] = array('description' => array(''), 'actions' => array()); $reflector = $reader->get($taskClass); $annotations = $reflector->getClassAnnotations(); if (!$annotations) { continue; } // Class Annotations foreach ($annotations as $annotation) { if ($annotation->getName() == 'description') { $this->documentation[$taskName]['description'] = $annotation->getArguments(); } } $methodAnnotations = $reflector->getMethodsAnnotations(); // Method Annotations if (!$methodAnnotations) { continue; } foreach ($methodAnnotations as $action => $collection) { if ($collection->has('DoNotCover')) { continue; } $actionName = strtolower(str_replace('Action', '', $action)); $this->documentation[$taskName]['actions'][$actionName] = array(); $actionAnnotations = $collection->getAnnotations(); foreach ($actionAnnotations as $actAnnotation) { $_anotation = $actAnnotation->getName(); if ($_anotation == 'description') { $getDesc = $actAnnotation->getArguments(); $this->documentation[$taskName]['actions'][$actionName]['description'] = $getDesc; } elseif ($_anotation == 'param') { $getParams = $actAnnotation->getArguments(); $this->documentation[$taskName]['actions'][$actionName]['params'][] = $getParams; } } } } }
/** * Scan the added namespaces/directories, create appropriate micro collections * * @throws \Exception */ protected function scanNamespaces() { $reader = new MemoryAnnAdaptor(); foreach ($this->namespaces as $ns) { $scan = scandir($ns['dir']); foreach ($scan as $f) { $ff = $ns['dir'] . DIRECTORY_SEPARATOR . $f; $info = pathinfo($ns['dir'] . PATH_SEPARATOR . $f); // make sure we're working with a php file if (!is_file($ff) || $info['extension'] != 'php') { continue; } // OK, let's get the fully qualified class name $className = $ns['ns'] . '\\' . substr($f, 0, -(strlen($info['extension']) + 1)); $this->autoloadClass($className); // Create the reflector to read the annotations $reflector = $reader->get($className); /** @var Annotation[] $annotations */ $classAnn = $reflector->getClassAnnotations(); // will store the prefix for all URLs here $curPrefix = ''; // Will we setup default rest routes? $doRest = false; // Check if we have a class annotation for this class if ($classAnn) { // loop all annotations foreach ($classAnn as $ann) { // check if we have an annotation that we care about switch ($ann->getName()) { case 'RoutePrefix': $args = $ann->getArguments(); // we need one argument, the prefix! if (count($args) >= 1) { $curPrefix = $args[0]; } break; case 'RouteDefault': $args = $ann->getArguments(); // we need one argument, the default type! if (count($args) >= 1 && $args[0] == 'Rest') { $doRest = true; } } } } if ($doRest) { $this->setupRestRoutes($className, $curPrefix); } /** @var Annotation[] $annotations */ $methAnn = $reflector->getMethodsAnnotations(); // check if we have any method annotations for this class if ($methAnn) { // lopp through the functions with annotations foreach ($methAnn as $function => $annotations) { // loop the annotations for the current function foreach ($annotations as $ann) { $args = $ann->getArguments(); // handle the annotations we care about switch ($ann->getName()) { case 'Get': case 'Post': case 'Put': case 'Delete': if (count($args) != 1) { throw new \Exception('Invalid argument count for ' . $className . '::' . $function . '() / @' . $ann->getName()); } $this->updateCollection($className, $function, $curPrefix . $args[0], $ann->getName()); break; case 'Route': if ($ann->getNamedArgument('methods')) { $httpMethods = $ann->getNamedArgument('methods'); } else { $httpMethods = array('GET', 'PUT', 'POST', 'DELETE'); } if (count($args) < 1) { throw new \Exception('Invalid argument count for ' . $className . '::' . $function . '() / @' . $ann->getName()); } foreach ($httpMethods as $mtd) { $this->updateCollection($className, $function, $curPrefix . $args[0], $mtd); } break; } } } } } } }