示例#1
0
 /**
  * @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;
 }
示例#2
0
 public function indexAction()
 {
     if ($this->request->isPost()) {
         $settingsModel = new SettingsModel();
         $post = $this->request->getPost();
         $post = Utils::arrayMapRecursive('trim', $post, \true);
         foreach ($post as $k => $v) {
             $settingsModel->update(['value' => $v], ['`key` = ?' => $k]);
         }
         SettingsModel::removeCache();
         return (new RedirectResponse(route()))->withFlash();
     }
     $this->view->assign('settings', SettingsModel::getSettings());
 }
示例#3
0
 public function render()
 {
     $tmp = '';
     $name = $this->getFullyName();
     if ($this->isArray) {
         $tmp .= '<input type="hidden" name="' . $name . '" value="" />';
         $name .= '[]';
     }
     $tmp .= '<select' . ($this->isArray ? ' multiple="multiple"' : '') . ' name="' . $name . '"' . $this->htmlAttributes() . '>' . Utils::buildOptions($this->translateData($this->multiOptions), $this->value, $this->emptyOption, $this->emptyOptionValue) . '</select>';
     if ($this->showErrors === \true) {
         $tmp .= $this->renderErrors();
     }
     return $tmp;
 }
示例#4
0
 public function addAction(EntityInterface $entity = \null)
 {
     $model = new Users();
     if ($entity === \null) {
         $entity = $model->createEntity();
     }
     $form = new Form(package_path('UserManagement', '/Resources/forms/admin/users-add.php'));
     $form->populate($entity->toArray());
     if ($entity->getId()) {
         $form->password->setRequired(false);
         $form->repassword->setRequired(false);
     }
     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);
         $form->isValid($post);
         if (!$form->hasErrors()) {
             $post = Utils::arrayMapRecursive('trim', $post, true);
             if (array_key_exists('password', $post)) {
                 if ($post['password']) {
                     $post['password'] = Security::hash($post['password']);
                 } else {
                     unset($post['password']);
                 }
             }
             $entity->setFromArray($post);
             try {
                 $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->getId()) {
                     $redirectResponse = new RedirectResponse(route(\null, ['action' => 'edit', 'id' => $entity->getId()]));
                 } else {
                     $redirectResponse = new RedirectResponse(route(\null, ['action' => 'add', 'id' => \null]));
                 }
                 return $redirectResponse->withFlash(env('development') ? $e->getMessage() : 'Възникна грешка. Опитайте по-късно', 'danger');
             }
         }
     }
     return new View('admin/index/add', ['form' => $form, 'item' => $entity]);
 }
示例#5
0
 /**
  * @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;
 }
示例#6
0
 /**
  * Sets the page's reverse links to other pages
  *
  * This method expects an associative array of reverse links to other pages,
  * where each element's key is the name of the relation (e.g. alternate,
  * prev, next, help, etc), and the value is a mixed value that could somehow
  * be considered a page.
  *
  * @param  array|Traversable $relations [optional] an associative array of
  *                                      reverse links to other pages
  *
  * @throws \InvalidArgumentException if $relations it not an array
  *                                            or Traversable object
  * @return AbstractPage fluent interface, returns self
  */
 public function setRev($relations = \null)
 {
     $this->rev = [];
     if (\null !== $relations) {
         if ($relations instanceof Traversable) {
             $relations = Utils::iteratorToArray($relations);
         }
         if (!is_array($relations)) {
             throw new \InvalidArgumentException('Invalid argument: $relations must be an ' . 'array or an instance of Traversable');
         }
         foreach ($relations as $name => $relation) {
             if (is_string($name)) {
                 $this->rev[$name] = $relation;
             }
         }
     }
     return $this;
 }
示例#7
0
 /**
  * @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;
 }
示例#8
0
 public function wizzardAction()
 {
     $form = new Form(package_path('Brands', 'Resources/forms/admin/wizzard.php'));
     if ($this->request->isPost()) {
         $post = $this->request->getPost();
         $post = Utils::arrayMapRecursive('trim', $post);
         if (isset($post['btnBack'])) {
             return new RedirectResponse(route(\null, ['action' => 'index', 'id' => \null]));
         }
         $form->isValid($post);
         if (isset($post['name']) && $post['name'] && isset($post['countryId']) && $post['countryId']) {
             $m = new \Brands\Model\Table\Brands();
             foreach ($post['countryId'] as $countryId) {
                 $where = array('name = ?' => $post['name'], 'countryId = ?' => $countryId, 'typeId = ?' => $post['typeId']);
                 if ($m->fetchRow($where)) {
                     $form->countryId->addError('Тази марка и тип съществува за някои от избраните държави');
                     $form->markAsError();
                     break;
                 }
             }
         }
         if (!$form->hasErrors()) {
             $redirectResponse = new RedirectResponse(route(\null, ['action' => 'index', 'id' => \null]));
             try {
                 $post = Utils::arrayMapRecursive('trim', $post, true);
                 $this->getModel()->multipleInsert($post);
                 return $redirectResponse->withFlash('Информацията е записана');
             } catch (\Exception $e) {
                 return $redirectResponse->withFlash($e->getMessage(), 'danger');
             }
         }
     }
     $this->view->assign('form', $form);
 }
示例#9
0
 /**
  * @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;
 }
示例#10
0
 public function read()
 {
     return isset($this->session->{$this->namespace}) ? Utils::safeUnserialize($this->session->{$this->namespace}) : \null;
 }
示例#11
0
<?php

use Micro\Application\Application;
use Micro\Application\Utils;
$config = [];
foreach (glob('{application/config/*.php,application/config/packages/*.php}', GLOB_BRACE) as $file) {
    $config = Utils::merge($config, include $file);
}
if (isset($config['packages'])) {
    MicroLoader::addPath($config['packages']);
}
$app = new Application($config);
$app->registerDefaultServices();
$app->get('router')->mapFromConfig()->loadDefaultRoutes();
return $app;
示例#12
0
 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;
 }
示例#13
0
 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;
 }
示例#14
0
 public function getValue()
 {
     return $this->getSession()->value = md5(Utils::randomSentence(10) . time());
 }
示例#15
0
 /**
  * Handle filters
  * @param string $key
  * @return \Micro\Http\Response\RedirectResponse|array
  */
 protected function handleFilters($key = 'filters', $clearParams = ['id' => \null, 'page' => \null, 'sort' => \null])
 {
     $filters = $this->request->getParam($key);
     if ($this->request->isPost()) {
         $post = $this->request->getPost($key, []);
         if (isset($post['reset'])) {
             return new RedirectResponse(route(\null, [$key => \null] + $clearParams));
         }
         if (isset($post['filter'])) {
             unset($post['filter']);
             foreach ($post as $k => $v) {
                 if (is_object($v) || is_array($v) && empty($v) || \trim((string) $v) === '') {
                     unset($post[$k]);
                 }
             }
             return new RedirectResponse(route(\null, [$key => !empty($post) ? Utils::base64urlEncode(http_build_query($post)) : \null] + $clearParams));
         }
     }
     if ($filters) {
         parse_str(Utils::base64urlDecode($filters, \true), $filters);
         if (empty($filters)) {
             $filters = [];
         }
     } else {
         $filters = [];
     }
     return $filters;
 }
示例#16
0
 /**
  * @param mixed $module
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param bool $subRequest
  * @throws CoreException
  * @return ResponseInterface
  */
 public function resolve($module, ServerRequestInterface $request, ResponseInterface $response, $subRequest = \false)
 {
     if ($module instanceof ResponseInterface) {
         return $module;
     }
     if (($matches = $this->matchResolve($module)) === \null) {
         // write string to the response
         if (\is_string($module) || \is_object($module) && \method_exists($module, '__toString')) {
             $response->getBody()->write((string) $module);
             return $response;
         }
         throw new CoreException(\sprintf('Handler [%s] must be in [Handler@action] format', \is_object($module) ? \get_class($module) : $module));
     }
     $module = $matches[0];
     $action = $matches[1];
     if ($this->container->has($module)) {
         $moduleInstance = $this->container->get($module);
         if (!\is_object($moduleInstance) || $moduleInstance instanceof \Closure) {
             throw new CoreException('Handler "' . $module . '" is container service but it is not object', 500);
         }
         $module = \get_class($moduleInstance);
     } else {
         if (!\class_exists($module, \true)) {
             throw new CoreException('Handler class "' . $module . '" not found', 404);
         }
         $moduleInstance = new $module($request, $response, $this->container);
         if ($moduleInstance instanceof ContainerAwareInterface) {
             $moduleInstance->setContainer($this->container);
         }
     }
     $parts = explode('\\', $module);
     $moduleParam = Utils::decamelize($parts[0]);
     $controllerParam = Utils::decamelize($parts[count($parts) - 1]);
     $actionParam = Utils::decamelize($action);
     $request->withAttribute('module', $moduleParam);
     $request->withAttribute('controller', $controllerParam);
     $request->withAttribute('action', $actionParam);
     if ($moduleInstance instanceof Controller) {
         $action = $action . 'Action';
     }
     if (!\method_exists($moduleInstance, $action) && !\method_exists($moduleInstance, '__call')) {
         throw new CoreException('Method "' . $action . '" not found in "' . $module . '"', 404);
     }
     if ($moduleInstance instanceof Controller) {
         $moduleInstance->init();
     }
     $scope = '';
     if (\method_exists($moduleInstance, 'getScope')) {
         $scope = $moduleInstance->getScope();
     }
     if (($moduleResponse = $moduleInstance->{$action}()) instanceof ResponseInterface) {
         return $moduleResponse;
     }
     if (\is_object($moduleResponse) && !$moduleResponse instanceof View) {
         throw new CoreException('Handler response is object and must be instance of View', 500);
     }
     // resolve View object
     if ($moduleResponse === \null || is_array($moduleResponse)) {
         if ($moduleInstance instanceof Controller) {
             $view = $moduleInstance->getView();
         } else {
             $view = new View();
         }
         if (is_array($moduleResponse)) {
             $view->assign($moduleResponse);
         }
         $moduleResponse = $view;
     }
     if ($moduleResponse instanceof View) {
         if ($moduleResponse->getTemplate() === \null) {
             $moduleResponse->setTemplate(($scope ? $scope . '/' : '') . $controllerParam . '/' . $actionParam);
         }
         try {
             $moduleResponse->addPath(module_path($parts[0], '/Resources/views'), $moduleParam);
             $moduleResponse->addPath(config('view.paths', []));
         } catch (\Exception $e) {
         }
         if ($this->useEvent === \true && ($eventResponse = $this->container->get('event')->trigger('render.start', ['view' => $moduleResponse])) instanceof ResponseInterface) {
             return $eventResponse;
         }
         if ($subRequest) {
             $moduleResponse->setRenderParent(\false);
         }
         $response->getBody()->write((string) $moduleResponse->render());
     } else {
         $response->getBody()->write((string) $moduleResponse);
     }
     return $response;
 }