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; }
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; }
/** * Auto register namespace and class map * @throws Exception\RuntimeException * @todo Should cache register classmap */ public function register() { $moduleHandler = $this->diFactory->get('moduleHandler'); $autoloadConf = $moduleHandler->getModulesAutoloadConfig(); if (isset($autoloadConf['namespaces'])) { if (!ArrayUtils::isHashTable($autoloadConf['namespaces'])) { throw new Exception\RuntimeException('Config autoload for namespace is invalid'); } $this->loader->registerNamespaces($autoloadConf['namespaces']); } if (isset($autoloadConf['classmap'])) { $this->registerClassMap($autoloadConf['classmap']); } $this->loader->register(); return $this; }
/** * Create error handler service * @param Di $di * @return \Phalex\Mvc\Exception\HandlerDefault * @throws InvalidArgumentException */ public function createService(Di $di) { $required = ['views_dir' => null, 'template_500' => null, 'template_404' => null]; $config = $di->get('config')['error_handler']->toArray(); $errMsg = sprintf('Cannot create error handler "%s"', __CLASS__); if (!isset($config['options'])) { throw new InvalidArgumentException($errMsg); } $options = array_merge($required, $config['options']); if (empty($options['views_dir']) || empty($options['template_500']) || empty($options['template_404']) || ($realPath = realpath($options['views_dir'])) === false) { throw new InvalidArgumentException($errMsg); } $this->options = $options; $this->di = $di; $this->viewsDir = $realPath; return $this; }
public function run() { $config = $this->di->get('config'); if (!isset($config['console'])) { throw new Exception\RuntimeException('Cannot find config for console'); } $configConsole = $config['console']->toArray(); foreach ($configConsole as $class) { $command = new $class(); if (!$command instanceof Command) { $errMsg = sprintf('"%s" must be extends from "%s"', get_class($command), Command::class); throw new Exception\RuntimeException($errMsg); } $command->setDI($this->di); $this->app->add($command); } $this->app->run(); }
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(); }
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); }
/** * Listen dispatch's events * @param \Phalex\Events\Listener\Dispatch $listener * @return \Phalex\Events\Listener */ public function listenDispatchEvents(Listener\Dispatch $listener) { $this->di->get('eventsManager')->attach('dispatch', $listener, PHP_INT_MAX); return $this; }