Beispiel #1
0
 public function testForMiddleWares()
 {
     $cfg = new Config(['routes' => ['get the/right/result' => ['before' => ['strModification'], 'handler' => function () {
     }]]]);
     $m = new BaseModule($cfg);
     $result = '';
     $stringMod = function ($req, $res) use(&$result) {
         $result = "The Right Result";
     };
     $m->setMiddleWare('strModification', $stringMod);
     $mw = $m->getMiddleWare('strModification');
     $req = new Request('the/right/result', 'the=value1&param=value2', 'iplox.org', 'GET');
     $m->init($req);
     $this->assertEquals($stringMod, $mw, "The middlware are not registering corretly. The functions must be the same.");
     $this->assertEquals('The Right Result', $result, 'The before middlewares are not getting executed.');
 }
Beispiel #2
0
 public function __construct(Config $cfg, AbstractModule $mod, array $injections = null)
 {
     parent::__construct($cfg, $mod, $injections);
     // Add the general options.
     $cfg->addKnownOptions(['controllerNamespace' => 'Controllers', 'controllerSuffix' => '', 'alternativeMethodSuffix' => 'Action', 'modelSuffix' => '', 'entitySuffix' => '', 'notFoundHandler' => 'Classes\\NotFound->index', 'defaultController' => 'Controllers\\Index', 'defaultMethod' => 'index', 'moduleClassName' => __CLASS__, 'contentType' => 'application/json']);
     // Options for mapping resources to controllers.
     $cfg->addKnownOptions('resourceMappings', ['index' => 'Index']);
     $cfg->refreshCache();
     $this->router->addFilters(['resource' => function ($val) {
         $resources = $this->config->getSet('resourceMappings');
         return is_array($resources) && array_key_exists($val, $resources) ? true : false;
     }]);
     // Filters for the Restful functionality
     $this->router->appendRoutes(['/:resource/(*params)?' => function ($resourceName, $params = '/') {
         if ($class = $this->getResourceController($resourceName)) {
             $c = new $class($this->config, $this, $params);
             return $c->response;
         }
         return false;
     }, '/*params' => function ($params) {
         return false;
         // Not found.
     }, '/' => function () {
         //Check if the global handler method exists
         return $this->callGlobalIfDefined();
     }]);
 }
Beispiel #3
0
 public function __construct(Config $cfg, AbstractModule $parent = null, array $injections = null)
 {
     parent::__construct($cfg, $parent, $injections);
     // Add the options related to this module.
     $cfg->addKnownOptions(['controllerNamespace' => 'Controllers', 'controllerSuffix' => '', 'alternativeMethodSuffix' => 'Action', 'viewsDir' => 'views', 'modelSuffix' => '', 'entitySuffix' => '', 'notFoundHandler' => 'Classes\\NotFound->index', 'defaultController' => 'Index', 'defaultMethod' => 'index', 'moduleClassName' => __CLASS__]);
     $cfg->refreshCache();
     //Determine if any of this routes actually exists as an object
     $this->router->addFilters(array('controller' => function ($name, $path) {
         $ns = $this->config->namespace . '\\' . ucwords(preg_replace(['/^\\//', '/\\//'], ['', '\\'], $path)) . $this->config->controllerNamespace . '\\';
         $class = $ns . ucwords($name) . $this->config->controllerSuffix;
         if (class_exists($class, true)) {
             return true;
         }
         return false;
     }));
     // Filters for the MVC Controller functionality
     $this->router->appendRoutes(['/:controller/:method/{*params}?' => array($this, 'captureControllerMethod'), '/:controller/{*param}?' => array($this, 'captureController'), '/{*param}' => array($this, 'notFoundHandler'), '/' => array($this, 'captureDefault')]);
 }