/** * * @param string $name * @param array $definition */ private function parseRouteDefintion(string $name, array $definition) : array { static $string; if (!isset($string)) { $string = new CamelCase($name); } else { $string->setString($name); } $name = $string->uncamelize(); if (empty($definition['route']) || empty($definition['target']['controller']) || empty($definition['target']['action'])) { // Try to get controller and action from route name $ca = explode('.', $name); if (empty($definition['route'])) { $definition['route'] = '/' . $ca[0]; } if (empty($definition['target']['controller'])) { $definition['target']['controller'] = empty($ca[0]) ? $name : $ca[0]; } if (empty($definition['target']['action'])) { if (empty(preg_match('/\\|\\w+\\:\\w+/', $definition['route']))) { $definition['target']['action'] = empty($ca[1]) ? $name : $ca[1]; } } } $app = $this->getName(true); // Create route string if ($definition['route'] == '/') { $definition['route'] = '/' . $app; } else { if (strpos($definition['route'], '../') === false && $app != 'generic') { $definition['route'] = '/' . $app . $definition['route']; } else { $definition['route'] = str_replace('../', '/', $definition['route']); } } if (empty($definition['target']['app']) && $app != 'generic') { $definition['target']['app'] = $app; } if (empty($definition['method'])) { $definition['method'] = 'GET|POST'; } if (strpos($name, $app) === false) { $name = $app . '.' . $name; } $definition['name'] = $name; return $definition; }
/** * General dispatcher * * @param boolean $match_url * Optional boolean flag to supress route matching. This is important for situations where the Run() * method of an app, maybe altered the values for app, controller and/or action, has been called. * (Default: true) */ private function dispatch() { // Handle possible posted data $this->managePost(); // Send 404 error when no app name is defined in router if (empty($this->router['target']['app'])) { return $this->send404('No appname to call.'); } // We need this toll for some following string conversions $string = new CamelCase($this->router['target']['app']); // The apps classname is camelcased so the name from router match need to be converted. $app_name = $string->camelize(); // Get app instance from app handler $app = $this->getAppInstance($app_name); // Send 404 error when there is no app instance if (empty($app)) { return $this->send404('app.object'); } // Call app event: Run() $event_result = $this->callAppEvent($app, 'Run'); // Redirect from event? if (!empty($event_result) && $event_result != $app_name) { $this->router->setParam('app', $event_result); $this->router->match(); $this->dispatch(); return; } // Load controller object $string->setString($this->router['target']['controller'] ?? 'Index'); $controller_name = $string->camelize(); $controller = $app->getController($controller_name); // Send 404 when controller could not be loaded if ($controller == false) { return $this->send404('Controller::' . $controller_name); } // Handle controller action $string->setString($this->router['target']['action'] ?? 'Index'); $action = $string->camelize(); if (!method_exists($controller, $action)) { return $this->send404('Action::' . $action); } // Prepare controller object $controller->setAction($action); $controller->setParams($this->router->getParams()); $controller->setRoute($this->router->getCurrentRoute()); if ($this->router->isAjax()) { // Controller needs to know the output format $this->router->setFormat('json'); $this->http->header->contentType('application/json', 'utf-8'); $this->http->header->noCache(); // Result will be processed as ajax command list $controller->ajax(); /* @var $ajax \Core\Ajax\Ajax */ $ajax = $this->di->get('core.ajax'); // Handle messages $messages = $this->di->get('core.message.default')->getAll(); if (!empty($messages)) { /* @var $msg \Core\Message\Message */ foreach ($messages as $msg) { // Each message gets its own alert /* @var $alert \Core\Html\Bootstrap\Alert\Alert */ $alert = $this->di->get('core.html.factory')->create('Bootstrap\\Alert\\Alert'); $alert->setContext($msg->getType()); $alert->setDismissable($msg->getDismissable()); // Fadeout message? if ($this->config->get('Core', 'js.style.fadeout_time') > 0 && $msg->getFadeout()) { $alert->html->addCss('fadeout'); } // Has this message an id which we can use as id for the alerts html element? if (!empty($msg->getId())) { $alert->html->setId($msg->getId()); } // At least append the message content $alert->setContent($msg->getMessage()); $ajax->addCommand(new \Core\Ajax\Commands\Dom\DomCommand($msg->getTarget(), $msg->getDisplayFunction(), $alert->build())); } } // @TODO Process possible asset js files to load! $js_objects = $this->di->get('core.asset')->getAssetHandler('js')->getObjects(); if (!empty($js_objects)) { foreach ($js_objects as $js) { if ($js->getType() == 'file') { $ajax->addCommand(new \Core\Ajax\Commands\Act\JQueryGetScriptCommand($js->getContent())); } } } // Run ajax processor $result = $ajax->process(); } else { $result = $controller->run(); $this->router->setFormat($controller->getFormat()); } return $result; }
/** * Shorthand method for a FormDesigner instance with auto attached model * * @return FormDesigner */ protected function getFormDesigner($id = '') { /* @var $fd \Core\Html\FormDesigner\FormDesigner */ $fd = new FormDesigner($this->app->getName(true)); // Generate form id when id is not provided if (!$id) { $pieces = []; $string = new CamelCase($this->app->getName()); $pieces[] = $string->uncamelize(); $string->setString($this->name); $pieces[] = $string->uncamelize(); // get calling method name $dbt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); if (isset($dbt[1]['function'])) { $string->setString($dbt[1]['function']); $pieces[] = $string->uncamelize(); } $id = implode('-', $pieces); } if ($id) { $fd->setId($id); } // Create forms eaction url if (isset($this->route)) { $fd->html->setAction($this->app->url($this->route, $this->params)); } // Set session token $fd->setToken($this->di->get('core.security.form.token.name'), $this->di->get('core.security.form.token')); return $fd; }
/** * Sets the rule to validate the value against * * @param string|array $rule */ public function parseRule($rule) { unset($this->rule); unset($this->text); unset($this->result); $this->args = []; $string = new CamelCase(''); // Array type rules are for checks where the func needs one or more parameter // So $rule[0] is the func name and $rule[1] the parameter. // Parameters can be of type array where the elements are used as function parameters in the .. they are // set. if (is_array($rule)) { // Get the functionname $string->setString($rule[0]); $this->setRule($string->camelize()); // Parameters set? if (isset($rule[1])) { $this->args = !is_array($rule[1]) ? [$rule[1]] : $rule[1]; } // Custom error message if (isset($rule[2])) { $this->text = $rule[2]; } } else { // Get the functionname $string->setString($rule); $this->setRule($string->camelize()); } }