/** * @return CsrfProtectionEngine */ public static function getEngine() { $engine = Registry::get('hyperframework.web.csrf_protection_engine'); if ($engine === null) { $class = Config::getClass('hyperframework.web.csrf_protection.engine_class', CsrfProtectionEngine::class); $engine = new $class(); static::setEngine($engine); } return $engine; }
/** * @return ResponseEngine */ public static function getEngine() { $engine = Registry::get('hyperframework.web.response_engine'); if ($engine === null) { $class = Config::getClass('hyperframework.web.response_engine_class', ResponseEngine::class); $engine = new $class(); static::setEngine($engine); } return $engine; }
/** * @return void */ protected function writeLog() { if ($this->isLoggerEnabled() === false) { return; } $logLevel = $this->getLogLevel(); $callback = function () { return $this->getLog(); }; $loggerClass = Config::getClass('hyperframework.error_handler.logger_class', Logger::class); $loggerClass::log($logLevel, $callback); }
/** * @return void */ protected function renderErrorView() { $error = $this->getError(); if ($error instanceof HttpException) { $statusCode = $error->getStatusCode(); $statusReasonPhrase = $error->getStatusReasonPhrase(); } else { $statusCode = 500; $statusReasonPhrase = 'Internal Server Error'; } $class = Config::getClass('hyperframework.web.error_view.class', ErrorView::class); $view = new $class(); $view->render($statusCode, $statusReasonPhrase, $error); }
/** * @param string $name * @return DbConnection */ public function createConnection($name = null) { $config = $this->getConfig($name); if (isset($config['dsn']) === false) { $errorMessage = "Field 'dsn' is missing in database connection config"; if ($name !== null) { $errorMessage .= " '{$name}'"; } throw new ConfigException($errorMessage . '.'); } $username = isset($config['username']) ? $config['username'] : null; $password = isset($config['password']) ? $config['password'] : null; $options = isset($config['options']) ? $config['options'] : []; $class = Config::getClass('hyperframework.db.connection_class', DbConnection::class); $connection = new $class($name, $config['dsn'], $username, $password, $options); return $connection; }
/** * @return Router */ public function getRouter() { if ($this->router === null) { $configName = 'hyperframework.web.router_class'; $class = Config::getClass($configName); if ($class === null) { $class = 'Router'; $namespace = Config::getAppRootNamespace(); if ($namespace !== '' && $namespace !== '\\') { $class = NamespaceCombiner::combine($namespace, $class); } if (class_exists($class) === false) { throw new ClassNotFoundException("Router class '{$class}' does not exist," . " can be changed using config '{$configName}'."); } } $this->router = new $class($this); } return $this->router; }
/** * @return string */ public function getName() { if ($this->name !== null) { return $this->name; } $words = []; $word = ''; $length = strlen($this->parameterName); for ($index = 0; $index < $length; ++$index) { $char = $this->parameterName[$index]; $ascii = ord($char); if ($char !== '_' && ($ascii < 65 || $ascii > 90)) { $word .= $this->parameterName[$index]; } else { if ($word !== '') { $words[] = $word; $word = ''; } if ($char !== '_') { $word = strtolower($char); } } } if ($word !== '') { if ($this->isRepeatable() && ctype_alpha($word)) { if ($word !== 'list') { $inflectorClass = Config::getClass('hyperframework.inflector_class', Inflector::class); $words[] = $inflectorClass::singularize($word); } elseif (count($words) === 0) { $words[] = 'element'; } } else { $words[] = $word; } } $this->name = implode('-', $words); return $this->name; }
/** * @return LoggerEngine */ public static function getEngine() { $engine = Registry::get('hyperframework.logging.logger_engine'); if ($engine === null) { $class = Config::getClass('hyperframework.logging.logger_engine_class', LoggerEngine::class); $engine = new $class(); static::setEngine($engine); } return $engine; }
/** * @return LogWriter */ protected function getWriter() { if ($this->writer === null) { $class = Config::getClass('hyperframework.logging.log_writer_class', LogWriter::class); $this->writer = new $class(); } return $this->writer; }
/** * @param array $configs * @param string $subcommandName * @return MutuallyExclusiveOptionGroupConfig[] */ private function parseMutuallyExclusiveOptionGroupConfigs($configs, $subcommandName = null) { if ($this->mutuallyExclusiveOptionGroupConfigParser === null) { $class = Config::getClass('hyperframework.cli.' . 'mutually_exclusive_option_group_config_parser_class', MutuallyExclusiveOptionGroupConfigParser::class); $this->mutuallyExclusiveOptionGroupConfigParser = new $class(); } return $this->mutuallyExclusiveOptionGroupConfigParser->parse($configs, $this->getOptionConfigIndex($subcommandName), $this->isMultipleCommandMode(), $subcommandName); }
/** * @return DbConnectionFactory */ private function getConnectionFactory() { if ($this->connectionFactory === null) { $class = Config::getClass('hyperframework.db.connection_factory_class', DbConnectionFactory::class); $this->connectionFactory = new $class(); } return $this->connectionFactory; }
/** * @param mixed $viewModel * @return View */ public static function createView($viewModel = null) { $class = Config::getClass('hyperframework.web.view.class', View::class); return new $class($viewModel); }
/** * @return void */ private function getProfileHandler() { if ($this->isProfileHandlerInitialized === false) { $class = Config::getClass('hyperframework.db.operation_profiler.profile_handler_class'); if ($class !== null) { $this->profileHandler = new $class(); } $this->isProfileHandlerInitialized = true; } return $this->profileHandler; }
/** * @return array */ protected function parseCommand() { try { $class = Config::getClass('hyperframework.cli.command_parser_class', CommandParser::class); $commandConfig = $this->getCommandConfig(); $commandParser = new $class(); return $commandParser->parse($commandConfig); } catch (CommandParsingException $e) { $this->renderCommandParsingError($e); $this->quit(); } }
/** * @param string $pattern * @param array $options * @return bool */ protected function matchResources($pattern, $options = []) { if (preg_match('#[:*]id($|[/{])#', $pattern) !== 0) { throw new RoutingException("Invalid pattern '{$pattern}', " . "dynamic segment ':id' is reserved."); } if (isset($options[':id'])) { throw new RoutingException("Invalid option ':id', " . "use option 'id' to change the pattern of element id."); } if (isset($options['id'])) { $options[':id'] = $options['id']; } else { $options[':id'] = '\\d+'; } $actionOptions = ['default_actions', 'element_acitons', 'collection_actions']; foreach ($actionOptions as $actionOption) { if (isset($options[$actionOption]) && is_array($options[$actionOption]) === false) { throw new RoutingException("Option '{$actionOption}' must be an array, " . gettype($options[$actionOption]) . ' given.'); } } if (isset($options['default_actions']) === false) { $defaultActions = ['index' => ['GET', '/', 'belongs_to' => 'collection'], 'new' => ['belongs_to' => 'collection'], 'create' => ['POST', '/', 'belongs_to' => 'collection'], 'show' => ['GET', '/', 'belongs_to' => 'element'], 'edit' => ['belongs_to' => 'element'], 'update' => [['PATCH', 'PUT'], '/', 'belongs_to' => 'element'], 'delete' => ['DELETE', '/', 'belongs_to' => 'element']]; } else { $defaultActions = $options['default_actions']; foreach ($defaultActions as $key => $value) { if (isset($value['belongs_to']) === false) { throw new RoutingException("Default action '{$key}' is invalid, " . "because property 'belongs_to' is missing."); } if ($value['belongs_to'] !== 'collection' && $value['belongs_to'] !== 'element') { throw new RoutingException("Default action '{$key}' is invalid, " . "the value of property 'belongs_to'" . " must be equal to 'collection' or 'element'."); } } } if (isset($options['collection_actions'])) { foreach ($options['collection_actions'] as $key => $value) { if (is_int($key)) { if (isset($defaultActions[$value])) { $action = $defaultActions[$value]; if ($action['belongs_to'] === 'element') { unset($options['collection_actions'][$key]); $options['collection_actions'][$value] = []; } } } } $options['actions'] = $options['collection_actions']; } else { $options['actions'] = []; foreach ($defaultActions as $key => $value) { if ($value['belongs_to'] === 'collection') { $actionName = $value; if (is_int($key)) { $options['actions'][] = $value; } else { $options['actions'][] = $key; } } } } if (isset($options['element_actions'])) { $actions = $this->convertElementActionsToCollectionActions($options['element_actions'], $defaultActions); $options['actions'] = array_merge($options['actions'], $actions); } else { foreach ($defaultActions as $key => $value) { if ($value['belongs_to'] === 'element') { if (is_int($key)) { $options['actions'][] = $value; } else { $options['actions'][] = $key; } } } } $options['default_actions'] = $this->convertElementActionsToCollectionActions($defaultActions, null, true); $isMatched = $this->matchResource($pattern, $options); if ($isMatched) { $controller = (string) $this->getController(); if ($controller === '') { return true; } $hyphenPosition = strrpos($controller, '-'); $underscorePosiiton = strrpos($controller, '_'); $separatorPosition = $hyphenPosition; if ($hyphenPosition < $underscorePosiiton) { $separatorPosition = $underscorePosiiton; } $controllerPrefix = ''; $controllerLastWord = $controller; if ($separatorPosition > 0) { $controllerPrefix = substr($controller, 0, $separatorPosition + 1); $controllerLastWord = substr($controller, $separatorPosition + 1); if ($controllerLastWord === '') { return true; } } $inflectorClass = Config::getClass('hyperframework.inflector_class', Inflector::class); $controllerLastWord = $inflectorClass::singularize($controllerLastWord); $this->setController($controllerPrefix . $controllerLastWord); } return $isMatched; }