Пример #1
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;
            }
        }
    }
}
Пример #2
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);
     }
 }
Пример #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);
 }
Пример #4
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();
 }