/** * @throws \Exception * @return \Micro\Model\ModelInterface */ public function getModel() { if ($this->model === \null) { $package = $this->request->getParam('package'); $controller = $this->request->getParam('controller'); if ($package && $controller) { $package = ucfirst(Utils::camelize($package)); $controller = ucfirst(Utils::camelize($controller)); $model = $package . '\\Model\\' . $controller; if (class_exists($model, \true)) { $this->model = new $model(); } else { $model = $package . '\\Model\\' . $package; if (class_exists($model, \true)) { $this->model = new $model(); } } } } else { if (is_string($this->model) && class_exists($this->model, \true)) { $this->model = new $this->model(); } } if (!$this->model instanceof ModelInterface) { throw new \Exception(sprintf('Model [%s] must be instanceof %s', is_object($this->model) ? get_class($this->model) : gettype($this->model), ModelInterface::class)); } return $this->model; }
/** * @param EntityInterface $entity * @return RedirectResponse|View */ public function addAction(EntityInterface $entity = \null) { $module = $this->request->getParam('module'); $controller = $this->request->getParam('controller'); $model = $this->getModel(); if ($entity === \null) { $entity = $model->createEntity(); } $form = new Form(module_path(ucfirst(Utils::camelize($module)), '/Resources/forms/' . ($this->scope ? $this->scope . '/' : '') . $controller . '-add.php')); $form->populate($entity->toArray()); // hook $this->prepareForm($form, $entity); if ($this->request->isPost()) { $post = $this->request->getPost(); if (isset($post['btnBack'])) { return new RedirectResponse(route(\null, ['action' => 'index', 'id' => \null])); } $post = Utils::arrayMapRecursive('trim', $post); // hook $this->modifyPost($form, $entity, $post); // hook $this->preValidate($form, $entity, $post); $form->isValid($post); // hook $this->postValidate($form, $entity, $post); if (!$form->hasErrors()) { if (!isset($post['languageId']) && $this->container->has('language') && ($language = $this->container->get('language')) instanceof LanguageInterface) { $post['languageId'] = $language->getId(); } try { $post = Utils::arrayMapRecursive('trim', $post, true); // hook $this->modifyData($post); $entity->setFromArray($post); // hook $this->modifyEntity($entity); $model->save($entity); if (isset($post['btnApply'])) { $redirectResponse = new RedirectResponse(route(\null, ['action' => 'edit', 'id' => $entity[$model->getIdentifier()]])); } else { $redirectResponse = new RedirectResponse(route(\null, ['action' => 'index', 'id' => \null])); } return $redirectResponse->withFlash('Информацията е записана'); } catch (\Exception $e) { if ($entity[$model->getIdentifier()]) { $redirectResponse = new RedirectResponse(route(\null, ['action' => 'edit', 'id' => $entity[$model->getIdentifier()]])); } else { $redirectResponse = new RedirectResponse(route(\null, ['action' => 'add', 'id' => \null])); } return $redirectResponse->withFlash(env('development') ? $e->getMessage() : 'Възникна грешка. Опитайте по-късно', 'danger'); } } } $this->view->setTemplate(($this->scope ? $this->scope . '/' : '') . $controller . '/add'); return $this->view->assign(['form' => $form, 'item' => $entity]); }
/** * @throws \Exception * @return ModelInterface */ public function getModel() { $modelCandidate = \null; if ($this->model === \null) { $module = $this->request->getParam('module'); $controller = $this->request->getParam('controller'); if ($module && $controller) { $module = ucfirst(Utils::camelize($module)); $controller = ucfirst(Utils::camelize($controller)); $model = $module . '\\Model\\' . $controller; if (\class_exists($model, \true)) { $modelCandidate = $model; } else { $model = $module . '\\Model\\' . $module; if (\class_exists($model, \true)) { $modelCandidate = $model; } } } } else { if (\is_string($this->model) && \class_exists($this->model, \true)) { $modelCandidate = $this->model; } } if ($modelCandidate !== \null) { if ($this->container->has($modelCandidate)) { $this->model = $this->container->get($modelCandidate); } else { $this->model = new $modelCandidate(); } } if (!$this->model instanceof ModelInterface) { throw new \Exception(\sprintf('Model [%s] must be instanceof %s', is_object($this->model) ? get_class($this->model) : (\is_string($this->model) ? $this->model : \gettype($this->model)), ModelInterface::class)); } return $this->model; }
/** * @return Router */ public function loadDefaultRoutes() { if (!isset($this->routes['admin'])) { $route = $this->map('/admin[/{module}][/{controller}][/{action}][/{id}]', function () { static $cache = []; $params = $this->getParams() + $this->getDefaults(); $module = $params['module']; $controller = $params['controller']; $action = $params['action']; $id = $params['id']; $hash = 'admin_' . $module . '_' . $controller . '_' . $action . '_' . $id; if (isset($cache[$hash])) { return $cache[$hash]; } $module = \ucfirst(Utils::camelize($module)); $controller = \ucfirst(Utils::camelize($controller)); $action = \lcfirst(Utils::camelize($action)); return $cache[$hash] = $module . '\\Controller\\Admin\\' . $controller . '@' . $action; }, 'admin'); $route->setDefaults(['module' => 'app', 'controller' => 'index', 'action' => 'index', 'id' => \null]); } if (!isset($this->routes['default'])) { $route = $this->map('/{module}[/{controller}][/{action}][/{id}]', function () { static $cache = []; $params = $this->getParams() + $this->getDefaults(); $module = $params['module']; $controller = $params['controller']; $action = $params['action']; $id = $params['id']; $hash = 'front_' . $module . '_' . $controller . '_' . $action . '_' . $id; if (isset($cache[$hash])) { return $cache[$hash]; } $module = \ucfirst(Utils::camelize($module)); $controller = \ucfirst(Utils::camelize($controller)); $action = \lcfirst(Utils::camelize($action)); return $cache[$hash] = $module . '\\Controller\\' . $controller . '@' . $action; }, 'default'); $route->setDefaults(['module' => 'app', 'controller' => 'index', 'action' => 'index', 'id' => \null]); } return $this; }
/** * @param string $package * @param Http\Request $request * @param Http\Response $response * @param bool $subRequest * @throws CoreException * @return \Micro\Http\Response */ public function resolve($package, Http\Request $request, Http\Response $response, $subRequest = \false) { if (!is_string($package) || strpos($package, '@') === \false) { throw new CoreException('[' . __METHOD__ . '] Package must be in [Package\\Handler@action] format', 500); } list($package, $action) = explode('@', $package); if (!class_exists($package, \true)) { throw new CoreException('[' . __METHOD__ . '] Package class "' . $package . '" not found', 404); } $parts = explode('\\', $package); $packageParam = Utils::decamelize($parts[0]); $controllerParam = Utils::decamelize($parts[count($parts) - 1]); $actionParam = Utils::decamelize($action); $request->setParam('package', $packageParam); $request->setParam('controller', $controllerParam); $request->setParam('action', $actionParam); $packageInstance = new $package($request, $response); if ($packageInstance instanceof Controller) { $actionMethod = lcfirst(Utils::camelize($action)) . 'Action'; } else { $actionMethod = lcfirst(Utils::camelize($action)); } if (!method_exists($packageInstance, $actionMethod)) { throw new CoreException('[' . __METHOD__ . '] Method "' . $actionMethod . '" not found in "' . $package . '"', 404); } if ($packageInstance instanceof ContainerAwareInterface) { $packageInstance->setContainer($this); } $scope = ''; if ($packageInstance instanceof Controller) { $packageInstance->init(); $scope = $packageInstance->getScope(); } if (($packageResponse = $packageInstance->{$actionMethod}()) instanceof Http\Response) { return $packageResponse; } if (is_object($packageResponse) && !$packageResponse instanceof View) { throw new CoreException('[' . __METHOD__ . '] Package response is object and must be instance of View', 500); } if ($packageResponse === \null || is_array($packageResponse)) { if ($packageInstance instanceof Controller) { $view = $packageInstance->getView(); } else { $view = new View(); } if (is_array($packageResponse)) { $view->addData($packageResponse); } $packageResponse = $view; } if ($packageResponse instanceof View) { if ($packageResponse->getTemplate() === \null) { $packageResponse->setTemplate(($scope ? $scope . '/' : '') . $controllerParam . '/' . $actionParam); } $packageResponse->injectPaths((array) package_path($parts[0], 'Resources/views')); if (($eventResponse = $this->get('event')->trigger('render.start', ['view' => $packageResponse])) instanceof Http\Response) { return $eventResponse; } if ($subRequest) { $packageResponse->setRenderParent(\false); } $response->setBody((string) $packageResponse->render()); } else { $response->setBody((string) $packageResponse); } return $response; }
protected function getNavigationHelper($routeName) { $route = $this->container->get('router')->getRoute($routeName); $handler = $route->getHandler(\false); if (is_string($handler) && strpos($handler, '@') !== \false) { list($package, $action) = explode('@', $handler); $parts = explode('\\', $package); $navigationHelper = \ucfirst(Utils::camelize($parts[0])) . '\\Navigation\\' . \ucfirst(Utils::camelize($routeName)); if (class_exists($navigationHelper, \true)) { $navigationHelper = new $navigationHelper($route); return $navigationHelper; } } return null; }
protected function getNavigationHelper($routeName) { $route = $this->container->get('router')->getRoute($routeName); if ($route !== \null && ($matches = $this->container->get('resolver')->matchResolve($route->getHandler())) !== \null) { $parts = explode('\\', $matches[0]); $navigationHelper = \ucfirst(Utils::camelize($parts[0])) . '\\Navigation\\Route\\' . \ucfirst(Utils::camelize($routeName)); if (\class_exists($navigationHelper, \true)) { $navigationHelper = new $navigationHelper($route); return $navigationHelper; } } return \null; }