Пример #1
0
 public function testGetEmpty()
 {
     $annotation = new Annotation();
     $annotations = new Annotations(array('tag1' => $annotation));
     $this->assertNotSame($annotation, $annotations->get('tag2'));
     $this->assertInstanceOf('TRex\\Annotation\\Annotation', $annotations->get('tag2'));
 }
 /**
  * @test
  * @profile fork
  */
 public function testParse()
 {
     split_time('Reset');
     $annotations = Annotations::get('Components\\Type_Test_Unit_Case_Annotations_Entity');
     split_time('Invoke Annotations::get(Components\\Type_Test_Unit_Case_Annotations_Entity)');
     $annotations = Annotations::get('Components\\Type_Test_Unit_Case_Annotations_Entity');
     split_time('Invoke Annotations::get(Components\\Type_Test_Unit_Case_Annotations_Entity)');
     assertTrue($annotations->hasTypeAnnotation(Annotation_Name::NAME));
     split_time('Invoke Annotations$hasMethodAnnotation(name)');
     assertTrue($annotations->hasTypeAnnotation(Annotation_Name::NAME));
     split_time('Invoke Annotations$hasMethodAnnotation(name)');
     assertTrue($annotations->hasTypeAnnotation('package'));
     split_time('Invoke Annotations$hasMethodAnnotation(package)');
     assertFalse($annotations->hasTypeAnnotation('version'));
     split_time('Invoke Annotations$hasMethodAnnotation(version)');
     assertTrue($annotations->hasMethodAnnotation('poke', Annotation_Name::NAME));
     split_time('Invoke Annotations$hasMethodAnnotation(poke)');
     assertTrue($annotations->hasMethodAnnotation('poke', Annotation_Name::NAME));
     split_time('Invoke Annotations$hasMethodAnnotation(poke)');
     $pokeName = $annotations->getMethodAnnotation('poke', Annotation_Name::NAME);
     split_time('Invoke Annotations$getMethodAnnotation(poke)');
     $pokeName = $annotations->getMethodAnnotation('poke', Annotation_Name::NAME);
     split_time('Invoke Annotations$getMethodAnnotation(poke)');
     $annotations = Annotations::get(__CLASS__);
     split_time('Invoke Annotations::get(' . __CLASS__ . ')');
     $annotations = Annotations::get(__CLASS__);
     split_time('Invoke Annotations::get(' . __CLASS__ . ')');
     assertEquals('poke', $pokeName->value);
 }
 public function __construct($class_, Test_Unit_Runner $runner_ = null)
 {
     $this->m_runner = $runner_;
     $this->m_class = new \ReflectionClass($class_);
     $rootPath = realpath($runner_->getTestRootPath());
     $testClassPath = realpath($this->m_class->getFileName());
     $this->m_path = str_replace("{$rootPath}/", '', $testClassPath);
     $annotations = Annotations::get($class_);
     foreach ($this->m_class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
         if ($ignoreAnnotation = $annotations->getMethodAnnotation($method->name, Annotation_Ignore::NAME)) {
             if (null !== $ignoreAnnotation->value && false === @method_exists($this->m_class->name, $ignoreAnnotation->value)) {
                 throw new Exception_IllegalArgument('test/unit/internal/dynamicproxy', sprintf('Illegal argument in @%1$s(value=%4$s) %2$s::%3$s().', Annotation_Ignore::NAME, $this->m_class->name, $method->name, $ignoreAnnotation->value));
             } else {
                 if (null !== $ignoreAnnotation->value) {
                     if (false !== ($reason = call_user_func([$this->m_class->name, $ignoreAnnotation->value]))) {
                         if (is_string($reason)) {
                             $this->m_skippedTestsReasons[$method->name] = $reason;
                         }
                         $this->m_skippedTests[$method->name] = true;
                     }
                 } else {
                     $this->m_skippedTests[$method->name] = true;
                 }
             }
         }
         if ($testAnnotation = $annotations->getMethodAnnotation($method->name, Annotation_Test::NAME)) {
             array_push($this->m_tests, $method);
             $expectedFail = $testAnnotation->expectedFail;
             $expectedException = $testAnnotation->expectedException;
             if (null !== $expectedException && false === @class_exists($expectedException)) {
                 throw new Exception_IllegalArgument('test/unit/internal/dynamicproxy', sprintf('Illegal argument in @%1$s(expectedException=%4$s) %2$s::%3$s().', Annotation_Test::NAME, $this->m_class->name, $method->name, $expectedException));
             }
             if (null !== $expectedException) {
                 $this->m_exceptionsExpected[$method->name] = $expectedException;
             }
             if (null != $expectedFail && 'false' !== trim(strtolower($expectedFail))) {
                 $this->m_failedExpected[$method->name] = true;
             }
         }
         /*
          * Search for Before-/AfterMethod/-Class/-Suite annotations
          * Only the first occurence per method will be respected.
          */
         foreach (self::$m_staticAnnotations as $staticAnnotation) {
             if ($annotations->hasMethodAnnotation($method->name, $staticAnnotation)) {
                 $this->m_mappedMethods[$staticAnnotation] = $method->name;
                 break;
             }
         }
         $this->m_methods[$method->name] = $method;
         if ($profileAnnotation = $annotations->getMethodAnnotation($method->name, Annotation_Profile::NAME)) {
             $this->m_profileMethods[$method->name] = null;
             if (Annotation_Profile::VALUE_FORK == $profileAnnotation->value) {
                 $this->m_profileMethodsForked[$method->name] = null;
             }
         }
     }
 }
 /**
  * @param string $type_
  *
  * @return array|string
  */
 public static function arrayForType($type_)
 {
     if (isset(self::$m_cache[$type_])) {
         return self::$m_cache[$type_];
     }
     if ($map = Cache::get('components/object/properties/' . md5($type_))) {
         return self::$m_cache[$type_] = $map;
     }
     $annotations = Annotations::get($type_);
     $map = [];
     foreach ($annotations->getPropertyAnnotations() as $propertyName => $propertyAnnotations) {
         if (isset($propertyAnnotations[Annotation_Transient::NAME])) {
             continue;
         }
         $property = array('name' => $propertyName, 'type' => null, 'nameMapped' => $propertyName, 'typeMapped' => null);
         foreach ($propertyAnnotations as $annotation) {
             if ($annotation instanceof Annotation_Type) {
                 if (false === strpos($annotation->value, '[')) {
                     $property['type'] = $annotation->value;
                     if (Primitive::isNative($property['type'])) {
                         $property['type'] = Primitive::asBoxed($property['type']);
                     }
                 } else {
                     if (false !== ($pos = strpos($annotation->value, '[]'))) {
                         $property['type'] = HashMap::TYPE;
                         $property['args'] = ltrim(substr($annotation->value, $pos), '\\');
                         if (Primitive::isNative($property['args'])) {
                             $property['args'] = Primitive::asBoxed($property['args']);
                         }
                     } else {
                         $annotationValue = rtrim($annotation->value, ']');
                         $property['type'] = ltrim(substr($annotationValue, 0, strpos($annotationValue, '[')), '\\');
                         $property['args'] = ltrim(substr($annotationValue, strpos($annotationValue, '[') + 1), '\\');
                         if (Primitive::isNative($property['type'])) {
                             $property['type'] = Primitive::asBoxed($property['type']);
                         }
                         if (Primitive::isNative($property['args'])) {
                             $property['args'] = Primitive::asBoxed($property['args']);
                         }
                     }
                 }
             }
             if ($annotation instanceof Annotation_Name) {
                 $property['nameMapped'] = $annotation->value;
             }
         }
         $map[$propertyName] = $property;
     }
     Cache::set('components/object/properties/' . md5($type_), $map);
     return self::$m_cache[$type_] = $map;
 }
Пример #5
0
 /**
  * Class annotations test.
  * @return void
  */
 public function testClassAnnotations()
 {
     $rc = new ReflectionClass('TestClass');
     $tmp = Annotations::getAll($rc);
     $this->assertEquals("Johno's addendum", $tmp["title"][0]->value);
     $this->assertTrue($tmp["title"][0]->mode);
     $this->assertEquals("One, Two", $tmp["title"][1]->value);
     $this->assertEquals("true or false", $tmp["title"][1]->mode);
     $this->assertEquals("Three (Four)", $tmp["title"][2]->value);
     $this->assertEquals("false", $tmp["title"][2]->mode);
     $this->assertEquals("item 1", $tmp["components"][0]);
     $this->assertTrue($tmp["persistent"][0]);
     $this->assertFalse($tmp["persistent"][1]);
     $this->assertNull($tmp["persistent"][2]);
     $this->assertTrue($tmp["renderable"][0]);
     $this->assertSame($tmp, Annotations::getAll($rc), 'cache test');
     $this->assertNotSame($tmp, Annotations::getAll(new ReflectionClass('ReflectionClass')), 'cache test');
     $this->assertTrue(Annotations::has($rc, 'title'), "has('title')");
     $this->assertEquals("Three (Four)", Annotations::get($rc, 'title')->value);
     $this->assertEquals("false", Annotations::get($rc, 'title')->mode);
     $tmp = Annotations::getAll($rc, 'title');
     $this->assertEquals("Johno's addendum", $tmp[0]->value);
     $this->assertTrue($tmp[0]->mode);
     $this->assertEquals("One, Two", $tmp[1]->value);
     $this->assertEquals("true or false", $tmp[1]->mode);
     $this->assertEquals("Three (Four)", $tmp[2]->value);
     $this->assertEquals("false", $tmp[2]->mode);
     $this->assertTrue(Annotations::has($rc, 'renderable'), "has('renderable')");
     $this->assertTrue(Annotations::get($rc, 'renderable'), "get('renderable')");
     $tmp = Annotations::getAll($rc, 'renderable');
     $this->assertTrue($tmp[0]);
     $tmp = Annotations::getAll($rc, 'persistent');
     $this->assertNull(Annotations::get($rc, 'persistent'), "get('persistent')");
     $this->assertTrue($tmp[0]);
     $this->assertFalse($tmp[1]);
     $this->assertNull($tmp[2]);
     $this->assertFalse(Annotations::has($rc, 'xxx'), "has('xxx')");
     $this->assertNull(Annotations::get($rc, 'xxx'), "get('xxx')");
 }
Пример #6
0
 /**
  * Returns array of persistent components.
  * This default implementation detects components by class-level annotation @persistent(cmp1, cmp2).
  * @return array
  */
 public static function getPersistentComponents()
 {
     return (array) Annotations::get(new ReflectionClass(func_get_arg(0)), 'persistent');
 }
 private static function initializeMethods()
 {
     if (false === (self::$m_methods = Cache::get('components/rest/methods'))) {
         self::$m_methods = [];
     }
     foreach (self::$m_resources as $resource) {
         if (isset(self::$m_methods[$resource])) {
             continue;
         }
         $annotations = Annotations::get($resource);
         foreach ($annotations->getMethodAnnotations() as $methodName => $methodAnnotations) {
             $httpMethods = [];
             foreach ($methodAnnotations as $methodAnnotationName => $methodAnnotation) {
                 if ($methodAnnotation instanceof Annotation_Method) {
                     $httpMethods[$methodAnnotationName] = $methodAnnotationName;
                 }
             }
             if (count($httpMethods)) {
                 $type = new \ReflectionClass($resource);
                 $method = $type->getMethod($methodName);
                 $parameters = $method->getParameters();
                 $path = [];
                 $query = [];
                 foreach ($parameters as $parameter) {
                     $parameterAnnotations = $annotations->getParameterAnnotations($methodName, $parameter->name);
                     if (isset($parameterAnnotations[Annotation_Param_Query::NAME])) {
                         if (isset($parameterAnnotations[Annotation_Param_Query::NAME]->name)) {
                             $name = $parameterAnnotations[Annotation_Param_Query::NAME]->name;
                         } else {
                             $name = $parameter->name;
                         }
                         if (isset($parameterAnnotations[Annotation_Param_Query::NAME]->type)) {
                             $type = $parameterAnnotations[Annotation_Param_Query::NAME]->type;
                             if (Primitive::isNative($type)) {
                                 $type = Primitive::asBoxed($type);
                             } else {
                                 if ($lookupType = Runtime_Classloader::lookup($type)) {
                                     $type = $lookupType;
                                 }
                             }
                         } else {
                             if ($type = $parameter->getClass()) {
                                 $type = $type->name;
                             } else {
                                 $type = String::TYPE;
                             }
                         }
                         if (isset($parameterAnnotations[Annotation_Param_Query::NAME]->default)) {
                             $value = $parameterAnnotations[Annotation_Param_Query::NAME]->default;
                         } else {
                             $value = null;
                         }
                         $query[$parameter->name] = ['name' => $name, 'type' => $type, 'value' => $value];
                     } else {
                         if ($parameter->isOptional()) {
                             if ($type = $parameter->getClass()) {
                                 $query[$parameter->name] = $type->name;
                             } else {
                                 $query[$parameter->name] = String::TYPE;
                             }
                         } else {
                             if (isset($parameterAnnotations[Annotation_Param_Path::NAME]->type)) {
                                 $type = $parameterAnnotations[Annotation_Param_Path::NAME]->type;
                                 if (Primitive::isNative($type)) {
                                     $type = Primitive::asBoxed($type);
                                 } else {
                                     if ($lookupType = Runtime_Classloader::lookup($type)) {
                                         $type = $lookupType;
                                     }
                                 }
                                 $path[$parameter->name] = $type;
                             } else {
                                 if ($type = $parameter->getClass()) {
                                     $path[$parameter->name] = $type->name;
                                 } else {
                                     $path[$parameter->name] = String::TYPE;
                                 }
                             }
                         }
                     }
                 }
                 $matches = [];
                 preg_match('/\\@return\\s+([\\a-z]+)\\n/i', $method->getDocComment(), $matches);
                 $return = null;
                 if (isset($matches[1])) {
                     $return = $matches[1];
                 }
                 self::$m_methods[$resource][$methodName] = ['name' => $methodName, 'methods' => $httpMethods, 'path' => $path, 'query' => $query, 'return' => $return];
             }
         }
     }
     Cache::set('components/rest/methods', self::$m_methods);
 }
Пример #8
0
 /**
  * Returns array of classes persistent parameters. They have public visibility and are non-static.
  * This default implementation detects persistent parameters by annotation @persistent.
  * @return array
  */
 public static function getPersistentParams()
 {
     $rc = new ReflectionClass(func_get_arg(0));
     $params = array();
     foreach ($rc->getProperties() as $rp) {
         if ($rp->isPublic() && !$rp->isStatic() && Annotations::get($rp, 'persistent')) {
             $params[] = $rp->getName();
         }
     }
     return $params;
 }
Пример #9
0
 private static function execute_admin()
 {
     global $RTR, $CI, $EXT, $BM, $URI, $OUT;
     // process annotations so that tokens are defined
     WPCI::process_menu_annotations();
     if ($token = isset($_REQUEST['page']) ? $_REQUEST['page'] : null) {
         $class = null;
         $method = null;
         $app = null;
         $directory = null;
         $app_path = null;
         // exact match for token?
         if (isset(WPCI::$app_index[$token])) {
             // load the menu settings
             $menu = WPCI::$app_index[$token];
             // tell WPCI which app is active
             $app = $menu['app'];
             WPCI::activate($app);
             // load the application controller
             $app_path = $menu['app_path'];
             require_once $app_path;
             $BM->mark('loading_time_base_classes_end');
             // create an instance of the controller
             $class = $menu['class'];
             $method = $menu['method_name'];
         } else {
             if ($token == 'wp-ci') {
                 $app = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
                 $class = isset($_REQUEST['c']) ? strtolower($_REQUEST['c']) : 'settings';
                 $method = isset($_REQUEST['m']) ? $_REQUEST['m'] : 'index';
                 $directory = isset($_REQUEST['d']) ? $_REQUEST['d'] : null;
                 // if app is specified, activate it... (otherwise the core application will be used)
                 if ($app) {
                     WPCI::activate($app);
                 }
                 if ($directory) {
                     $app_path = WPCI::active_app_path() . "/controllers/{$directory}/{$class}" . EXT;
                 } else {
                     $app_path = WPCI::active_app_path() . "/controllers/{$class}" . EXT;
                 }
                 if (!file_exists($app_path)) {
                     wp_die("I don't know how to do <b>{$class}/{$method}</b>.");
                 }
                 // load the contorller
                 require_once $app_path;
             }
         }
         if ($class && $method) {
             // fake the router into thinking he did his job...
             $RTR->set_app($app);
             $RTR->set_class($class);
             $RTR->set_method($method);
             $RTR->set_directory($directory);
             $BM->mark('loading_time_base_classes_end');
             if (!class_exists($class)) {
                 wp_die("I can't find <b>{$class}/{$method}</b>.");
             }
             // make sure app class is at the top of the annotations stack
             $ann = Annotations::get("{$app}/{$class}", $app_path);
             // evaluate permissions, but only when they are specified for evaluation
             $user_can = true;
             if (count($ann->for_class('user_must') + $ann->for_class('user_can') + $ann->for_method($method, 'user_must') + $ann->for_method($method, 'user_can'))) {
                 // first, test all user_must annotations
                 foreach ($ann->for_class('user_must') as $cap) {
                     if (!current_user_can($cap)) {
                         $user_can = false;
                         break;
                     }
                 }
                 // next, test for method
                 if ($user_can) {
                     foreach ($ann->for_method($method, 'user_must') as $cap) {
                         if (!current_user_can($cap)) {
                             $user_can = false;
                             break;
                         }
                     }
                     // then, test user_can
                     if ($user_can) {
                         $user_can = false;
                         foreach ($ann->for_class('user_can') as $cap) {
                             $user_can = $user_can || current_user_can($cap);
                         }
                         foreach ($ann->for_method($method, 'user_can') as $cap) {
                             $user_can = $user_can || current_user_can($cap);
                         }
                     }
                 }
             }
             if ($method == 'controller' or strncmp($method, '_', 1) == 0 or in_array(strtolower($method), array_map('strtolower', get_class_methods('Controller'))) or !$user_can) {
                 wp_die("You're not allowed to do <b>{$class}/{$method}</b>.");
             }
             $EXT->_call_hook('pre_controller');
             $BM->mark('controller_execution_time_( ' . $class . ' / ' . $method . ' )_start');
             $CI = new $class();
             $CI->method = strtoupper($_SERVER['REQUEST_METHOD']);
             $EXT->_call_hook('post_controller_constructor');
             // ajax annotation = no header
             $is_ajax = $ann->for_class('ajax') || $ann->for_method($method, 'ajax');
             $no_chrome = $ann->for_class('no_chrome') || $ann->for_method($method, 'chrome');
             if ($is_ajax || $no_chrome) {
                 $_GET['noheader'] = 1;
             }
             $ajax_content = null;
             // Is there a "remap" function?
             if (method_exists($CI, '_remap')) {
                 $CI->_remap($method);
             } else {
                 // is_callable() returns TRUE on some versions of PHP 5 for private and protected
                 // methods, so we'll use this workaround for consistent behavior
                 if (!in_array(strtolower($method), array_map('strtolower', get_class_methods($CI)))) {
                     wp_die("I'm not allowed to do <b>{$class}/{$method}</b>.");
                 }
                 log_message('debug', "Executing {$class}/{$method}()");
                 // Call the requested method.
                 // Any URI segments present (besides the class/function) will be passed to the method for convenience
                 if ($is_ajax || $no_chrome) {
                     ob_start();
                 }
                 call_user_func_array(array(&$CI, $method), array());
                 if ($is_ajax || $no_chrome) {
                     $ajax_content = ob_get_clean();
                 }
             }
             $BM->mark('controller_execution_time_( ' . $class . ' / ' . $method . ' )_end');
             $EXT->_call_hook('post_controller');
             $EXT->_call_hook('post_system');
             if (class_exists('CI_DB') and isset($CI->db)) {
                 $CI->db->close();
             }
             // if this was an ajax request, then we display the output and terminate
             if ($is_ajax || $no_chrome) {
                 if ($is_ajax) {
                     header('Content-type: application/json', true);
                 }
                 echo $ajax_content;
                 $OUT->_display();
                 exit(0);
             }
         }
     }
 }
 /**
  * Inject dependencies into given instance.
  *
  * @param mixed $object_
  */
 private function injectMembersImpl($object_)
 {
     $object = new \ReflectionObject($object_);
     foreach (Annotations::get($object->name)->getPropertyAnnotations() as $propertyName => $annotations) {
         if (false === isset($annotations[Annotation_Inject::NAME]) || null === ($binding = $this->getBindingForAnnotations($annotations))) {
             continue;
         }
         $property = $object->getProperty($propertyName);
         if (false === ($public = $property->isPublic())) {
             $property->setAccessible(true);
         }
         if ($binding instanceof Binding_Type_Provider && array_key_exists(Annotation_Binding_Provider::NAME, $annotations)) {
             $property->setValue($object_, $binding->getProvider());
         } else {
             $property->setValue($object_, $this->createInstanceImpl($binding));
         }
         $property->setAccessible($public);
     }
 }
 protected function initialize()
 {
     $a = Annotations::get($this->entityType);
     $this->entityName = Runtime_Classloader::lookupName($this->entityType);
     if ($annotation = $a->getTypeAnnotation(Annotation_Name::NAME)) {
         $this->collectionName = $a->getTypeAnnotation(Annotation_Name::NAME)->value;
     } else {
         $this->collectionName = strtolower(substr($this->entityType, strrpos($this->entityType, '_') + 1));
     }
     if ($annotation = $a->getTypeAnnotation(Annotation_Collection::NAME)) {
         $this->collectionType = Runtime_Classloader::lookup($annotation->value);
     } else {
         $this->collectionType = Entity_Collection::TYPE;
     }
     $this->autoIncrement = true;
     foreach ($a->getPropertyAnnotations() as $property => $annotations) {
         if (isset($annotations[Annotation_Id::NAME])) {
             $this->collectionPrimaryKey = $property;
             $this->collectionPrimaryKeyAutoIncrement = Boolean::valueIsTrue($annotations[Annotation_Id::NAME]->auto);
         }
     }
 }