Example #1
0
 private function createHelp()
 {
     $scannedTasksDir = array_diff(scandir($this->tasksDir), array('..', '.'));
     $config = $this->getDI()->get('config');
     if (isset($config['annotationsAdapter'])) {
         if ($config['annotationsAdapter'] == 'memory') {
             $reader = new \Phalcon\Annotations\Adapter\Memory();
         } elseif ($config['annotationsAdapter'] == 'apc') {
             $reader = new \Phalcon\Annotations\Adapter\Apc();
         } else {
             $reader = new \Phalcon\Annotations\Adapter\Memory();
         }
     } else {
         $reader = new \Phalcon\Annotations\Adapter\Memory();
     }
     foreach ($scannedTasksDir as $taskFile) {
         $taskClass = str_replace('.php', '', $taskFile);
         $taskName = strtolower(str_replace('Task', '', $taskClass));
         $this->documentation[$taskName] = array('description' => array(''), 'actions' => array());
         $reflector = $reader->get($taskClass);
         $annotations = $reflector->getClassAnnotations();
         $methodAnnotations = $reflector->getMethodsAnnotations();
         if ($annotations) {
             //Class Annotations
             foreach ($annotations as $annotation) {
                 if ($annotation->getName() == 'description') {
                     $this->documentation[$taskName]['description'] = $annotation->getArguments();
                 }
             }
             //Method Annotations
             if ($methodAnnotations) {
                 foreach ($methodAnnotations as $action => $collection) {
                     $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;
                         }
                     }
                 }
             }
         }
     }
 }
 public function testMemoryAdapter()
 {
     $adapter = new Phalcon\Annotations\Adapter\Memory();
     $classAnnotations = $adapter->get('TestClass');
     $this->assertTrue(is_object($classAnnotations));
     $this->assertEquals(get_class($classAnnotations), 'Phalcon\\Annotations\\Reflection');
     $this->assertEquals(get_class($classAnnotations->getClassAnnotations()), 'Phalcon\\Annotations\\Collection');
     $classAnnotations = $adapter->get('TestClass');
     $this->assertTrue(is_object($classAnnotations));
     $this->assertEquals(get_class($classAnnotations), 'Phalcon\\Annotations\\Reflection');
     $this->assertEquals(get_class($classAnnotations->getClassAnnotations()), 'Phalcon\\Annotations\\Collection');
     $classAnnotations = $adapter->get('User\\TestClassNs');
     $this->assertTrue(is_object($classAnnotations));
     $this->assertEquals(get_class($classAnnotations), 'Phalcon\\Annotations\\Reflection');
     $this->assertEquals(get_class($classAnnotations->getClassAnnotations()), 'Phalcon\\Annotations\\Collection');
     $classAnnotations = $adapter->get('User\\TestClassNs');
     $this->assertTrue(is_object($classAnnotations));
     $this->assertEquals(get_class($classAnnotations), 'Phalcon\\Annotations\\Reflection');
     $this->assertEquals(get_class($classAnnotations->getClassAnnotations()), 'Phalcon\\Annotations\\Collection');
     $property = $adapter->getProperty('TestClass', 'testProp1');
     $this->assertTrue(is_object($property));
     $this->assertEquals(get_class($property), 'Phalcon\\Annotations\\Collection');
     $this->assertEquals($property->count(), 4);
 }
Example #3
0
 public function initializeFormAnnotations()
 {
     $reader = new \Phalcon\Annotations\Adapter\Memory();
     $formProperties = $reader->getProperties($this);
     foreach ($formProperties as $key => $property) {
         $formProperty = isset($formProperties[$key]) ? $formProperties[$key] : null;
         $element = $this->createElementByProperty($key, $property);
         if ($element && $element instanceof \Phalcon\Forms\ElementInterface) {
             $this->add($element);
         }
     }
     return $this;
 }
Example #4
0
 /**
  * Parse object annotations for find acl rules
  *
  * @param $objectName
  * @return null|\stdClass
  */
 public function getObjectAcl($objectName)
 {
     $object = new \stdClass();
     $object->name = $objectName;
     $object->actions = [];
     $object->options = [];
     $reader = new \Phalcon\Annotations\Adapter\Memory();
     $reflector = $reader->get($objectName);
     $annotations = $reflector->getClassAnnotations();
     if ($annotations && $annotations->has('Acl')) {
         $annotation = $annotations->get('Acl');
         if ($annotation->hasNamedArgument('actions')) {
             $object->actions = $annotation->getArgument('actions');
         }
         if ($annotation->hasNamedArgument('options')) {
             $object->options = $annotation->getArgument('options');
         }
     } else {
         return null;
     }
     return $object;
 }
Example #5
0
 public function getOperations($controllerClass)
 {
     $operations = array();
     $ref = new \ReflectionClass($controllerClass);
     $methods = $ref->getMethods();
     $reader = new \Phalcon\Annotations\Adapter\Memory();
     $reflector = $reader->get($controllerClass);
     $operationAnnotations = $reflector->getMethodsAnnotations();
     foreach ($methods as $method) {
         if (!\Phalcon\Text::endsWith($method->name, 'Action')) {
             continue;
         }
         $operationKey = substr($method->name, 0, -6);
         $operationName = $operationKey;
         $operationDes = '';
         if (isset($operationAnnotations[$method->name]) && ($annotations = $operationAnnotations[$method->name])) {
             if (!$annotations->has('operationName') || !$annotations->has('operationDescription')) {
                 continue;
             }
             $annotation = $annotations->get('operationName');
             $operationName = implode('', $annotation->getArguments());
             $annotation = $annotations->get('operationDescription');
             $operationDes = implode('', $annotation->getArguments());
         }
         $operation = array('name' => $operationName, 'resourceKey' => $controllerClass, 'operationKey' => $operationKey, 'description' => $operationDes);
         $operations[] = $operation;
     }
     return $operations;
 }
<?php

$reader = new \Phalcon\Annotations\Adapter\Memory();
//Reflect the annotations in the class Example
$reflector = $reader->get('Example');
//Read the annotations in the class' docblock
$annotations = $reflector->getClassAnnotations();
//Traverse the annotations
foreach ($annotations as $annotation) {
    //Print the annotation name
    echo $annotation->getName(), PHP_EOL;
    //Print the number of arguments
    echo $annotation->numberArguments(), PHP_EOL;
    //Print the arguments
    print_r($annotation->getArguments());
}