Exemplo n.º 1
0
 public function getDepends()
 {
     $name = get_class($this);
     $reflection = new \Addendum\ReflectionAnnotatedClass($name);
     if ($reflection->hasAnnotation('Depends')) {
         $a = $reflection->getAnnotation('Depends');
         return $a->value;
     }
     return array();
 }
Exemplo n.º 2
0
/**
 * Get the annotation from class or method
 *
 * @author Jack
 * @date Sat Feb 21 10:31:44 2015
 * @param class
 * 		The class to get the annotation
 * @param annotation
 * 		The class of the annotation
 * @param method
 * 		The method to get the annotation
 */
function get_annotation($class, $annotation, $method = null)
{
    $re = new \Addendum\ReflectionAnnotatedClass($class);
    if ($method) {
        $an = $re->getMethod($method);
    } else {
        $an = $re;
    }
    if ($an->hasAnnotation($annotation)) {
        foreach ($an->getAnnotations() as $a) {
            if (get_class($a) == $annotation) {
                return $a;
            }
        }
    }
}
Exemplo n.º 3
0
 public function route()
 {
     profile_start('route');
     profile_start('load_controller');
     $request = $this->tool->create('Clips\\HttpRequest');
     html_meta('generator', 'clips-tool ' . $this->frameworkmeta->branch . '(' . $this->frameworkmeta->commit . ')');
     $this->tool->context(array('request' => $request, 'router' => $this));
     // Set the request context to the context
     // Empty the main envrionment
     $this->clips->clear();
     $this->clips->template("Clips\\RouteResult");
     $this->clips->load(clips_config('route_rules', array('/rules/route.rules')));
     // Assert the uris
     $uri = $this->getRequestURI();
     // Record the breadscrumb
     if (\Clips\config('breadscrum')) {
         if ($request->method == 'get' && $request->getType() != 'ajax' && strpos($uri, 'responsive/size') === false) {
             $bs = $request->breadscrumb();
             if (count($bs) > 1) {
                 if ($bs[count($bs) - 1] != $uri) {
                     $request->breadscrumb($uri);
                 }
             } else {
                 $request->breadscrumb($uri);
             }
         }
     }
     context('uri', $uri);
     $this->clips->assertFacts(array('uri', $uri), array('RequestType', $request->getType()), array('RequestMethod', $request->method));
     // Assert the parameters
     $params = array();
     $p = $request->param();
     if ($p) {
         foreach ($request->param() as $k => $v) {
             $params[] = array('Parameter', $k, $v);
         }
         $this->clips->assertFacts($params);
     }
     $this->clips->run();
     $error = $this->clips->queryFacts("RouteError");
     if ($error) {
         $result = new RouteResult();
         $result->controller = $this->tool->controller('error');
         $result->method = 'show';
         $result->args = $error[0];
         $controller_seg = 'error';
         //http_response_code(404);
         //error('RouteError', array($error[0][0]));
     } else {
         $result = $this->clips->queryFacts("Clips\\RouteResult");
         $controller_seg = $this->clips->queryFacts("controller");
         $server_uri = $this->clips->queryFacts("server-uri");
         $result = $result[0];
         $controller_seg = $controller_seg[0][0];
         $server_uri = strtolower(str_replace('\\', '/', $server_uri[0][0]));
     }
     if (!isset($server_uri)) {
         $server_uri = 'error';
     }
     profile_end('load_controller');
     profile_start('controller_init');
     $cc = $result->controller;
     $controller = new $cc();
     $this->tool->context(array('controller_class' => $result->controller, 'controller_seg' => $controller_seg, 'controller' => $controller, 'controller_method' => $result->method, 'args' => $result->args, 'action' => new SimpleAction(array('type' => Action::SERVER, 'content' => $server_uri, 'params' => $result->args))));
     $controller->request = $request;
     $this->tool->enhance($controller);
     $this->filterChain = $this->tool->load_class('FilterChain', true);
     $this->filterChain->addFilter(config('filters'));
     $re = new \Addendum\ReflectionAnnotatedClass($controller);
     // Trying to get the definition from class and the method annotation
     $m = $re->getMethod($result->method);
     foreach ($m->getAllAnnotations() as $a) {
         $this->tool->annotationEnhance($a, $controller);
     }
     profile_end('controller_init');
     profile_start('filter_before');
     $ret = null;
     if ($this->filterChain->filter_before($this->filterChain, $controller, $result->method, $result->args, $request)) {
         // Let the filter before can prevent the run of the controller method
         try {
             profile_end('filter_before');
             profile_start('controller');
             $ret = call_user_func_array(array($controller, $result->method), $result->args);
             profile_end('controller');
         } catch (\Exception $e) {
             error(get_class($e), array($e->getMessage()), true);
         }
     }
     // Getting the error from the context
     $error = context('error');
     if ($ret == null && $error) {
         // If there is no output and we can get the error, show the error
         $default_view = config('default_view');
         if ($default_view) {
             if (is_array($error)) {
                 $cause = $error[0];
                 $cause = $cause->cause;
             } else {
                 $cause = $error->cause;
             }
             $r = new Resource('tpl://error/' . $cause);
             $r = $r->contents();
             if (isset($cause) && $r) {
                 $ret = new ViewModel('error/' . $cause, array('error' => $error), $default_view[0]);
             } else {
                 $ret = new ViewModel('error/error', array('error' => $error), $default_view[0]);
             }
         } else {
             $ret = $error;
         }
     } else {
         if ($error) {
             // We can get the response, so just log the error
             $this->logger->error('Getting an error when serving the request.', array('error' => $error));
         }
     }
     profile_end('route');
     // Always run filter after(since the filter after will render the views)
     $this->filterChain->filter_after($this->filterChain, $controller, $result->method, $result->args, $request, $ret);
 }
Exemplo n.º 4
0
 public function execute($command, $args)
 {
     $c = $this->command(ucfirst($command));
     if ($c) {
         $deps = $c->getDepends();
         if (!is_array($deps)) {
             $deps = array($deps);
         }
         foreach ($deps as $dep) {
             $this->execute($dep, $args);
         }
         $reflection = new \Addendum\ReflectionAnnotatedClass($c);
         if (!$reflection->hasAnnotation('Clips\\FullArgs')) {
             array_shift($args);
             // For the clips script
             array_shift($args);
             // For the command
         }
         return $c->execute($args);
     }
     trigger_error('No command named ' . $command . ' found!');
 }
Exemplo n.º 5
0
 /**
  * Forward to another controller method
  */
 protected function forward()
 {
     $args = func_get_args();
     if ($args) {
         $method = array_shift($args);
         $re = new \Addendum\ReflectionAnnotatedClass($this);
         $m = $re->getMethod($method);
         context('must_init_v2', 1, true);
         foreach ($m->getAnnotations() as $a) {
             $this->tool->annotationEnhance($a, $this);
         }
         context_pop('must_init_v2');
         $this->context('forward_method', $method);
         return call_user_func_array(array($this, $method), $args);
     }
 }
Exemplo n.º 6
0
 public function setUp()
 {
     $mute = getenv('MUTE_PHPUNIT');
     $ref = new \ReflectionClass($this);
     $func = $this->getName();
     if (!$mute && $func != 'testStub') {
         echo "\n----------" . $ref->name . " | " . $func . "----------\n";
     }
     $this->tool =& get_clips_tool();
     $this->tool->helper('fake');
     $this->clips = new Engine();
     $re = new \Addendum\ReflectionAnnotatedClass($this);
     foreach (array($re, $re->getMethod($func)) as $m) {
         foreach ($m->getAnnotations() as $a) {
             switch (get_class($a)) {
                 case "Clips\\Rules":
                     // We need to load the rules before test execution
                     $this->clips->clear();
                     // We should clear the clips every time
                     $rules = get_default($a, 'rules', array());
                     $templates = get_default($a, 'templates', array());
                     // Load the templates
                     foreach ($templates as $t) {
                         $this->clips->template($t);
                     }
                     // Load the rules
                     foreach ($rules as $r) {
                         $this->clips->load($r);
                     }
                     break;
                 case "Clips\\FakeModel":
                     $ds = $this->tool->library('dataSource');
                     $fake = $this->tool->library('fakeDataSource');
                     $ds->fake = $fake;
                     if (valid_obj($this, 'Clips\\Interfaces\\FakeDataSourceHandler')) {
                         $fake->handler = $this;
                     } else {
                         if (!$a->value) {
                             $a->value = 'expectFakeDataSourceHandler';
                         }
                         $fake->handler = $this->tool->library($a->value);
                         $this->fake_handler = $fake->handler;
                     }
                     $ds->_datasources = array('fake');
                     break;
                 case "Clips\\TestData":
                 case "Clips\\DataGenerator":
                     $this->data = $this->tool->enhance($a);
                     break;
                 case "Clips\\TestValue":
                     if (isset($a->json)) {
                         $a->file = $a->json;
                     }
                     if (isset($a->file)) {
                         $test_config_dir = clips_config('test_data_dir');
                         if (!$test_config_dir) {
                             $test_config_dir = clips_config('test_dir');
                             $test_config_dir = path_join($test_config_dir[0], 'data');
                         } else {
                             $test_config_dir = $test_config_dir[0];
                         }
                         $p = path_join($test_config_dir, $a->file);
                         if (\file_exists($p)) {
                             $this->value = \file_get_contents($p);
                             if (isset($a->json)) {
                                 $this->value = parse_json($this->value);
                             }
                         }
                     } else {
                         if (isset($a->context)) {
                             $this->value = clips_context($a->context);
                         } else {
                             if (isset($a->value)) {
                                 $this->value = $a->value;
                             }
                         }
                     }
                     break;
                 default:
                     $this->tool->annotationEnhance($a, $this);
             }
         }
     }
     $this->doSetUp();
 }
Exemplo n.º 7
0
 /**
  * Loads the metadata for the specified class into the provided container.
  */
 public function loadMetadataForClass($className, ClassMetadata $metadata)
 {
     $annotClass = new \Addendum\ReflectionAnnotatedClass($className);
     // Evaluate DoctrineEntity annotation
     if (($entityAnnot = $annotClass->getAnnotation('DoctrineEntity')) === false) {
         throw DoctrineException::updateMe("{$className} is no entity.");
     }
     $metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
     // Evaluate DoctrineTable annotation
     if ($tableAnnot = $annotClass->getAnnotation('DoctrineTable')) {
         $metadata->setPrimaryTable(array('name' => $tableAnnot->name, 'schema' => $tableAnnot->schema, 'catalog' => $tableAnnot->catalog));
     }
     // Evaluate DoctrineInheritanceType annotation
     if ($inheritanceTypeAnnot = $annotClass->getAnnotation('DoctrineInheritanceType')) {
         $metadata->setInheritanceType($inheritanceTypeAnnot->value);
     }
     // Evaluate DoctrineDiscriminatorColumn annotation
     if ($discrColumnAnnot = $annotClass->getAnnotation('DoctrineDiscriminatorColumn')) {
         $metadata->setDiscriminatorColumn(array('name' => $discrColumnAnnot->name, 'type' => $discrColumnAnnot->type, 'length' => $discrColumnAnnot->length));
     }
     // Evaluate DoctrineDiscriminatorMap annotation
     if ($discrValueAnnot = $annotClass->getAnnotation('DoctrineDiscriminatorValue')) {
         $metadata->setDiscriminatorValue($discrValueAnnot->value);
     }
     // Evaluate DoctrineSubClasses annotation
     if ($subClassesAnnot = $annotClass->getAnnotation('DoctrineSubClasses')) {
         $metadata->setSubclasses($subClassesAnnot->value);
     }
     // Evaluate DoctrineChangeTrackingPolicy annotation
     if ($changeTrackingAnnot = $annotClass->getAnnotation('DoctrineChangeTrackingPolicy')) {
         $metadata->setChangeTrackingPolicy($changeTrackingAnnot->value);
     }
     // Evaluate annotations on properties/fields
     foreach ($annotClass->getProperties() as $property) {
         if ($metadata->hasField($property->getName())) {
             continue;
         }
         $mapping = array();
         $mapping['fieldName'] = $property->getName();
         // Check for DoctrineJoinColummn/DoctrineJoinColumns annotations
         $joinColumns = array();
         if ($joinColumnAnnot = $property->getAnnotation('DoctrineJoinColumn')) {
             $joinColumns[] = array('name' => $joinColumnAnnot->name, 'referencedColumnName' => $joinColumnAnnot->referencedColumnName, 'unique' => $joinColumnAnnot->unique, 'nullable' => $joinColumnAnnot->nullable, 'onDelete' => $joinColumnAnnot->onDelete, 'onUpdate' => $joinColumnAnnot->onUpdate);
         } else {
             if ($joinColumnsAnnot = $property->getAnnotation('DoctrineJoinColumns')) {
                 $joinColumns = $joinColumnsAnnot->value;
             }
         }
         // Field can only be annotated with one of: DoctrineColumn,
         // DoctrineOneToOne, DoctrineOneToMany, DoctrineManyToOne, DoctrineManyToMany
         if ($columnAnnot = $property->getAnnotation('DoctrineColumn')) {
             if ($columnAnnot->type == null) {
                 throw DoctrineException::updateMe("Missing type on property " . $property->getName());
             }
             $mapping['type'] = $columnAnnot->type;
             $mapping['length'] = $columnAnnot->length;
             $mapping['nullable'] = $columnAnnot->nullable;
             if ($idAnnot = $property->getAnnotation('DoctrineId')) {
                 $mapping['id'] = true;
             }
             if ($generatedValueAnnot = $property->getAnnotation('DoctrineGeneratedValue')) {
                 $metadata->setIdGeneratorType($generatedValueAnnot->strategy);
             }
             $metadata->mapField($mapping);
             // Check for SequenceGenerator/TableGenerator definition
             if ($seqGeneratorAnnot = $property->getAnnotation('DoctrineSequenceGenerator')) {
                 $metadata->setSequenceGeneratorDefinition(array('sequenceName' => $seqGeneratorAnnot->sequenceName, 'allocationSize' => $seqGeneratorAnnot->allocationSize, 'initialValue' => $seqGeneratorAnnot->initialValue));
             } else {
                 if ($tblGeneratorAnnot = $property->getAnnotation('DoctrineTableGenerator')) {
                     throw new DoctrineException("DoctrineTableGenerator not yet implemented.");
                 }
             }
         } else {
             if ($oneToOneAnnot = $property->getAnnotation('DoctrineOneToOne')) {
                 $mapping['targetEntity'] = $oneToOneAnnot->targetEntity;
                 $mapping['joinColumns'] = $joinColumns;
                 $mapping['mappedBy'] = $oneToOneAnnot->mappedBy;
                 $mapping['cascade'] = $oneToOneAnnot->cascade;
                 $metadata->mapOneToOne($mapping);
             } else {
                 if ($oneToManyAnnot = $property->getAnnotation('DoctrineOneToMany')) {
                     $mapping['mappedBy'] = $oneToManyAnnot->mappedBy;
                     $mapping['targetEntity'] = $oneToManyAnnot->targetEntity;
                     $mapping['cascade'] = $oneToManyAnnot->cascade;
                     $metadata->mapOneToMany($mapping);
                 } else {
                     if ($manyToOneAnnot = $property->getAnnotation('DoctrineManyToOne')) {
                         $mapping['joinColumns'] = $joinColumns;
                         $mapping['cascade'] = $manyToOneAnnot->cascade;
                         $mapping['targetEntity'] = $manyToOneAnnot->targetEntity;
                         $metadata->mapManyToOne($mapping);
                     } else {
                         if ($manyToManyAnnot = $property->getAnnotation('DoctrineManyToMany')) {
                             $joinTable = array();
                             if ($joinTableAnnot = $property->getAnnotation('DoctrineJoinTable')) {
                                 $joinTable = array('name' => $joinTableAnnot->name, 'schema' => $joinTableAnnot->schema, 'catalog' => $joinTableAnnot->catalog, 'joinColumns' => $joinTableAnnot->joinColumns, 'inverseJoinColumns' => $joinTableAnnot->inverseJoinColumns);
                             }
                             $mapping['joinTable'] = $joinTable;
                             $mapping['targetEntity'] = $manyToManyAnnot->targetEntity;
                             $mapping['mappedBy'] = $manyToManyAnnot->mappedBy;
                             $mapping['cascade'] = $manyToManyAnnot->cascade;
                             $metadata->mapManyToMany($mapping);
                         }
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 8
0
 /**
  * Define the template according to the class
  */
 public function defineTemplate($class)
 {
     if (is_string($class) && class_exists($class)) {
         $reflection = new \Addendum\ReflectionAnnotatedClass($class);
         $ret = array();
         $ret[] = '(deftemplate ' . $class;
         foreach (get_class_vars($class) as $slot => $v) {
             if ($reflection->getProperty($slot)->hasAnnotation('Clips\\Multi')) {
                 $ret[] = '(multislot ' . $slot . ')';
             } else {
                 $ret[] = '(slot ' . $slot . ')';
             }
         }
         return implode(' ', $ret) . ')';
     }
     return false;
 }