public function testAddRouteHttpMethods() { Route::reset(); $config = ['router' => ['home' => ['route' => '/', 'definitions' => ['controller' => 'index', 'action' => 'index', 'module' => 'Application', 'namespace' => 'Application\\Controller'], 'methods' => ['get', 'put']]]]; $di = new Di($config); $di->set('request', function () { return new PhalconRequest(); }); $diManager = new DiManager($di); $diManager->initRouterDi(); $this->assertInstanceOf(Router::class, $diManager->getDI()->get('router')); $router = $diManager->getDI()->get('router'); $httpMethods = ['GET' => true, 'POST' => false, 'PUT' => true, 'PATCH' => false, 'OPTIONS' => false, 'DELETE' => false]; foreach ($httpMethods as $httpMethod => $isMatched) { $_SERVER['REQUEST_METHOD'] = $httpMethod; $router->handle('/'); $this->assertEquals($isMatched, $router->wasMatched()); if ($router->wasMatched()) { $this->assertEquals('index', $router->getControllerName()); $this->assertEquals('index', $router->getActionName()); $this->assertEquals('Application\\Controller', $router->getNamespaceName()); $this->assertEquals('Application', $router->getModuleName()); } } }
private function getDi() { $di = new Di(require './tests/config/config.result.php'); $di->set('request', function () { return new PhalconRequest(); }); return $di; }
/** * * @param Di $diFactory */ public function __construct(Di $diFactory) { $this->diFactory = $diFactory; $this->loader = new Loader(); $ev = new EventsManager(); $ev->enablePriorities(true); $ev->collectResponses(true); $diFactory->set('autoloaderEventsManager', $ev, true); $this->loader->setEventsManager($ev); }
public function __construct(array $config) { $cacheModule = !isset($config['cache_module']) ? null : $this->getCacheInstance($config['cache_module']); $cacheConfig = !isset($config['cache_config']) ? null : $this->getCacheInstance($config['cache_config']); $moduleHandler = new Module($config['modules'], $config['autoload_module_paths'], $cacheModule); $entireAppConf = (new ConfigHandler($moduleHandler, $config['config_glob_paths'], $cacheConfig))->getConfig(); $diFactory = new Di\Di($entireAppConf); $diFactory->set('moduleHandler', $moduleHandler, true); $this->diManager = new Di\DiManager($diFactory); // Create error handler early for handling exception $this->setErrorHanlder(); }
public function __construct(Di $di) { parent::__construct(); $this->di = $di; $eventsManager = new EventsManager(); $eventsManager->enablePriorities(true); $eventsManager->collectResponses(true); $di->set('eventsManager', $eventsManager, true); $this->setEventsManager($eventsManager); $this->setDI($di); $this->setDefaultAction('index'); $this->setDefaultController('index'); }
/** * Set url service when module start * @param Di $di * @param Config $config * @param string $name Module name * @return \Phalex\Events\Listener\Application */ private function setUrlService(Di $di, Config $config, $name) { $base = $static = '/'; if (isset($config['url'])) { $default = isset($config['url']['default']) ? $config['url']['default'] : '/'; if (isset($config['url'][$name])) { $base = isset($config['url'][$name]['uri']) ? $config['url'][$name]['uri'] : $default; $static = isset($config['url'][$name]['static']) ? $config['url'][$name]['static'] : $default; } } $url = new UrlService(); $url->setBaseUri($base); $url->setStaticBaseUri($static); $di->set('url', $url, true); return $this; }
protected function setServiceFactories($serviceName, $serviceConfig, $isShare) { if (!($isCallable = is_callable($serviceConfig)) && !is_string($serviceConfig)) { $msg = sprintf('Config for factories service "%s" must be string or callable', $serviceName); throw new Exception\UnexpectedValueException($msg); } $this->diFactory->set($serviceName, function () use($serviceConfig, $isCallable) { if ($isCallable) { return $serviceConfig($this->diFactory); } $obj = new $serviceConfig(); if (!$obj instanceof DiFactoryInterface) { $msg = sprintf('Class "%s" must be implemented "%s"', $serviceConfig, DiFactoryInterface::class); throw new Exception\RuntimeException($msg); } return $obj->createService($this->diFactory); }, $isShare); }
public function testEvent() { $autoload = (require './tests/config/autoload.result.php'); $autoload['namespaces']['Backend\\Router'] = getcwd() . '/tests/module/Backend/src/Router'; $moduleHandlerMock = $this->getMockBuilder(Module::class)->disableOriginalConstructor()->getMock(); $moduleHandlerMock->expects($this->once())->method('getModulesAutoloadConfig')->will($this->returnValue($autoload)); $di = new Di(require './tests/config/config.result.php'); $di->set('moduleHandler', $moduleHandlerMock); $loader = new Autoloader($di); $trace = array(); $di['autoloaderEventsManager']->attach('loader', function ($event, $loader) use(&$trace) { if (!isset($trace[$event->getType()])) { $trace[$event->getType()] = array(); } $trace[$event->getType()][] = $loader->getCheckedPath(); }); $loader->register(); // Call class new LowerCase(); $cwd = getcwd(); $this->assertEquals($trace, array('beforeCheckClass' => array(null), 'beforeCheckPath' => array($cwd . '/tests/module/Backend/src/Router/LowerCase.php'), 'pathFound' => array($cwd . '/tests/module/Backend/src/Router/LowerCase.php'))); $loader->unregister(); }