/** * Constructor of Router */ public function __construct() { $routers = Cache::get('router:routers'); $reverse = Cache::get('router:reverse'); if (!$routers or !$reverse) { $routers = array(); $reverse = array(); $path = Application::getInstance()->getPath() . '/modules/*/controllers/*.php'; foreach (new \GlobIterator($path) as $file) { /* @var \SplFileInfo $file */ $module = $file->getPathInfo()->getPathInfo()->getBasename(); $controller = $file->getBasename('.php'); $reflection = Application::getInstance()->reflection($file->getRealPath()); if ($routes = $reflection->getRoute()) { foreach ($routes as $route => $pattern) { if (!isset($reverse[$module])) { $reverse[$module] = array(); } $reverse[$module][$controller] = ['route' => $route, 'params' => $reflection->getParams()]; $rule = [$route => ['pattern' => $pattern, 'module' => $module, 'controller' => $controller, 'params' => $reflection->getParams()]]; // static routers should be first if (strpos($route, '$')) { $routers = array_merge($routers, $rule); } else { $routers = array_merge($rule, $routers); } } } } Cache::set('router:routers', $routers); Cache::set('router:reverse', $reverse); } $this->routers = $routers; $this->reverse = $reverse; }
/** * {@inheritdoc} * * @param string $module * @param string $controller * @param array $params * @return void */ protected function preDispatch($module, $controller, $params = array()) { // auth as CLI user $cliUser = Table::findRowWhere(['login' => 'system']); Auth::setIdentity($cliUser); parent::preDispatch($module, $controller, $params); }
/** * Init instance * * @return Instance */ protected static function initInstance() { $instance = new Instance(); $instance->setPath(Application::getInstance()->getPath()); $instance->setEnvironment(Application::getInstance()->getEnvironment()); $instance->init(); return $instance; }
/** * @param string $module * @param string $controller * @param array $params * @return \Bluz\Controller\Controller * @throws \Exception */ public function dispatch($module, $controller, $params = []) { $this->dispatchModule = $module; $this->dispatchController = $controller; try { return parent::dispatch($module, $controller, $params); } catch (\Exception $e) { $this->setException($e); throw $e; } }
/** * @param array $data * @throws Exception * @throws ValidatorException * @return integer */ public function createOne($data) { // password $password = $data['password'] ?? null; $password2 = $data['password2'] ?? null; if (empty($password)) { throw ValidatorException::exception('password', __('Password can\'t be empty')); } if ($password !== $password2) { throw ValidatorException::exception('password2', __('Password is not equal')); } if ($data['id'] == '') { unset($data['id']); } /** @var $row Row */ $row = $this->getTable()->create(); $row->setFromArray($data); $row->status = Table::STATUS_PENDING; $row->save(); $userId = $row->id; // create auth Auth\Table::getInstance()->generateEquals($row, $password); // create activation token // valid for 5 days $actionRow = UsersActions\Table::getInstance()->generate($userId, UsersActions\Table::ACTION_ACTIVATION, 5); // send activation email // generate activation URL $activationUrl = Router::getFullUrl('users', 'activation', ['code' => $actionRow->code, 'id' => $userId]); $subject = "Activation"; $body = Application::getInstance()->dispatch('users', 'mail/template', ['template' => 'registration', 'vars' => ['user' => $row, 'activationUrl' => $activationUrl, 'password' => $password]])->render(); try { $mail = Mailer::create(); $mail->Subject = $subject; $mail->msgHTML(nl2br($body)); $mail->addAddress($data['email']); Mailer::send($mail); } catch (\Exception $e) { Logger::log('error', $e->getMessage(), ['module' => 'users', 'controller' => 'change-email', 'userId' => $userId]); throw new Exception('Unable to send email. Please contact administrator.'); } // show notification and redirect Messages::addSuccess("Your account has been created and an activation link has" . "been sent to the e-mail address you entered.<br/>" . "Note that you must activate the account by clicking on the activation link" . "when you get the e-mail before you can login."); // wtf? // redirectTo('index', 'index'); return $userId; }
* Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\View\Helper; use Bluz\Application\Application; use Bluz\Application\Exception\ForbiddenException; use Bluz\View\View; return function ($module, $controller, $params = array()) { /** * @var View $this */ try { $view = Application::getInstance()->dispatch($module, $controller, $params); } catch (ForbiddenException $e) { // nothing for ForbiddenException return null; } catch (\Exception $e) { return $this->exception($e); } // run closure if ($view instanceof \Closure) { return $view(); } return $view; };
<?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\Controller\Helper; use Bluz\Application\Application; use Bluz\Controller\Controller; use Bluz\Proxy\Layout; /** * Switch layout * * @param $layout */ return function ($layout) { /** * @var Controller $this */ Application::getInstance()->useLayout(true); Layout::setTemplate($layout); };
<?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\View\Helper; use Bluz\Application\Application; use Bluz\Proxy\Layout; return function ($script = null) { if (Application::getInstance()->hasLayout()) { return Layout::headScript($script); } else { // it's just alias to script() call return $this->script($script); } };
/** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\View\Helper; use Bluz\Application\Application; /** * Return Exception message * * @param \Exception $exception * @return string */ return function ($exception) { /** * @var \Exception $exception */ if (Application::getInstance()->isDebug()) { // @codeCoverageIgnoreStart // exception message for developers return '<div class="alert alert-error">' . '<strong>Exception</strong>: ' . $exception->getMessage() . '</div>'; // @codeCoverageIgnoreEnd } else { return ''; } };
/** * @param string $module * @param string $controller * @param array $params * @return \Bluz\View\View|string */ public function dispatch($module, $controller, $params = array()) { $this->dispatchModule = $module; $this->dispatchController = $controller; return parent::dispatch($module, $controller, $params); }
* @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\View\Helper; use Bluz\Application\Application; use Bluz\Proxy\Request; use Bluz\Proxy\Router; use Bluz\View\View; use Bluz\View\ViewException; return function ($module, $controller, $params = [], $checkAccess = false) { try { if ($checkAccess) { if (!Application::getInstance()->isAllowed($module, $controller)) { return null; } } } catch (\Exception $e) { throw new ViewException('Url View Helper: ' . $e->getMessage()); } if (null === $module) { $module = Request::getModule(); } if (null === $controller) { $controller = Request::getController(); } if (null === $params) { $params = Request::getParams(); }
/** * Render with debug headers * @return void */ public function render() { if ($this->debugFlag && !headers_sent()) { $debugString = sprintf("%fsec; %skb", microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], ceil(memory_get_usage() / 1024)); $debugString .= '; ' . Request::getModule() . '/' . Request::getController(); Response::setHeader('Bluz-Debug', $debugString); if ($info = Logger::get('info')) { Response::setHeader('Bluz-Bar', json_encode($info)); } else { Response::setHeader('Bluz-Bar', '{"!":"Logger is disabled"}'); } } parent::render(); }
/** * Magic cast to string * * @return string */ public function __toString() { if (!$this->template) { return ''; } // $view for use in closure $view = new View(); $path = Application::getInstance()->getPath(); // setup additional helper path $view->addHelperPath($path . '/layouts/helpers'); // setup additional partial path $view->addPartialPath($path . '/layouts/partial'); // setup default path $view->setPath($path . '/modules/' . $this->module . '/views'); // setup template $view->setTemplate($this->template); // setup data $view->setFromArray($this->getData()->toArray()); return $view->render(); }
/** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\View\Helper; use Bluz\Application\Application; use Bluz\Proxy\Layout; use Bluz\View\View; /** * Set or generate <script> code for <head> * * @param string $script * @return string|null */ return function ($script = null) { /** * @var View $this */ if (Application::getInstance()->useLayout()) { return Layout::headScript($script); } else { // it's just alias to script() call return $this->script($script); } };
<?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\View\Helper; use Bluz\Application\Application; use Bluz\Application\Exception\ForbiddenException; use Bluz\View\View; return function ($module, $widget, $params = array()) { try { $widgetClosure = Application::getInstance()->widget($module, $widget); $widgetClosure(...$params); } catch (ForbiddenException $e) { // nothing for Acl exception } catch (\Exception $e) { echo $this->exception($e); } };
<?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\View\Helper; use Bluz\Application\Application; use Bluz\View\View; return function ($module, $method, $params = array()) { try { $apiClosure = Application::getInstance()->api($module, $method); return $apiClosure(...$params); } catch (\Exception $e) { return $this->exception($e); } };
<?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\Controller\Helper; use Bluz\Application\Application; use Bluz\Controller\Controller; /** * Switch layout or disable it * * @return void */ return function () { /** * @var Controller $this */ Application::getInstance()->useLayout(false); };
/** * Render like string * * @return string */ public function __toString() { ob_start(); try { if (!file_exists($this->path . '/' . $this->template) || !is_file($this->path . '/' . $this->template)) { throw new ViewException("Template '{$this->template}' not found"); } extract($this->container); require $this->path . '/' . $this->template; } catch (\Exception $e) { // clean output ob_end_clean(); // @codeCoverageIgnoreStart if (Application::getInstance()->isDebug()) { return $e->getMessage() . "\n<br/>" . $e->getTraceAsString(); } // @codeCoverageIgnoreEnd // nothing for production return ''; } return ob_get_clean(); }
/** * Alias for call instance of Application * * @return \Bluz\Application\Application */ function app() { return \Bluz\Application\Application::getInstance(); }
/** * Run REST controller * @return mixed * @throws ForbiddenException * @throws NotImplementedException */ public function run() { // check implementation if (!isset($this->map[$this->method])) { throw new NotImplementedException(); } $map = $this->map[$this->method]; // check permissions if (isset($map['acl'])) { if (!Acl::isAllowed($this->module, $map['acl'])) { throw new ForbiddenException(); } } // dispatch controller return Application::getInstance()->dispatch($map['module'], $map['controller'], ['crud' => $this->getCrud(), 'primary' => $this->getPrimaryKey(), 'data' => $this->data]); }