public function paramsParse($params) { // set template if (!empty($params['template']) && $params['template'] != 'current') { $this->template = \View\Template::get($params['template']); } //set page if (!empty($params['page']) && $params['page'] != 'current') { $this->template->setPage($params['page']); } //set module if (!empty($params['module'])) { $this->template->setModule($params['module']); } //set content if (!empty($params['content'])) { $this->template->setContent($params['content']); } elseif (!$this->template->contentPath) { $this->template->setContent(); } //set data if (!empty($params['data'])) { $this->contentData = array_merge($this->contentData, $params['data']); } }
function editFileAction($templateName) { $template = \View\Template::get($templateName, \App::$primary); if (!empty($_GET['path']) && file_exists($template->path . '/' . Tools::parsePath($_GET['path']))) { $code = file_get_contents("php://input"); if (!empty($code)) { $result = new Server\Result(); $result->successMsg = 'Файл сохранен'; $content = file_put_contents($template->path . '/' . Tools::parsePath($_GET['path']), $code); $result->send(); } $content = file_get_contents($template->path . '/' . Tools::parsePath($_GET['path'])); $this->libs->loadLib('Ace'); $this->view->page(['content' => 'Template/edit', 'data' => compact('template', 'content')]); } else { $this->view->page(['content' => 'Template/chooseFile', 'data' => compact('template')]); } }
/** * Renders a template and sends the response to the client. The template context * must be provided in an array. * * @param string $templateFile File name of the template * @param array|null $context Array that contains the variables of the template * @return bool Returns true */ protected function render(string $templateFile, array $context = NULL) : bool { $template = new Template(); echo $template->render($templateFile, $context); return true; }
/** * Parses a route and executes the respective action * * @param string $request Route to be parsed * @param array $routes Array containing the routing table * @param null|string $module Name of the module * @return int Returns zero if all is well otherwise returns a value minor to zero * @throws \RuntimeException Throws an exception if the respective route file does * not exists * @throws \RuntimeException Throws an exception if the respective template file does * not exists */ public static function parseRoute(string $request, array $routes, string $module = NULL) : int { foreach ($routes as $route) { if (substr($route['route'], -1) != '$') { $routePattern = sprintf('#%s.*#', $route['route']); } else { $routePattern = sprintf('#%s#', $route['route']); } $routeMatches = array(); if (preg_match($routePattern, $request, $routeMatches) == 1) { if (isset($route['module'])) { $routesFile = sprintf('%s/%s_routes.yml', Settings::getRoutesPath(), $route['module']); if (file_exists($routesFile)) { $moduleRoutes = YAML::load($routesFile); $pattern = sprintf('#%s#', $route['route']); $filterRequest = preg_filter($pattern, '', $request, 1); $parseRoute = self::parseRoute($filterRequest, $moduleRoutes, $route['module']); return $parseRoute; } else { throw new \RuntimeException(sprintf('%s Route file %s does not exists', __METHOD__, $routesFile), self::ROUTE_FILE_DOES_NOT_EXIST); } } elseif (isset($route['controller']) && $module) { Module::set($module); $controllerFile = sprintf('%s/%s_controller.php', Settings::getControllersPath(), $module); include_once $controllerFile; $class = ucfirst($module); $object = new $class(); $objectReflection = new \ReflectionClass($class); if (sizeof($routeMatches)) { $funcParams = array(); $methodReflection = $objectReflection->getMethod($route['controller']); $methodParams = $methodReflection->getParameters(); foreach ($methodParams as $param) { if (isset($routeMatches[$param->getName()])) { $funcParams[] = $routeMatches[$param->getName()]; } } if (Request::isMethodPost()) { if (!CsrfToken::checkPostToken()) { Response::response(Response::HTTP_FORBIDDEN); return 0; } } call_user_func_array(array($object, 'callMethod'), array($route['controller'], $funcParams)); } else { $object->{$route}['controller'](); } unset($object); unset($objectReflection); return 0; } elseif (isset($route['template']) && $module) { Module::set($module); $templateFile = sprintf('%s/%s/%s', Settings::getViewsPath(), $module, $route['template']); if (file_exists($templateFile)) { $template = new Template(); echo $template->render($templateFile); return 0; } else { throw new \RuntimeException(sprintf('%s Template file %s does not exists', __METHOD__, $templateFile), self::TEMPLATE_FILE_DOES_NOT_EXIST); } } } } return -1; }