public function init() { /* * Basically, what we want to to is load app's route config * */ $path = path('apps') . $this->app . path('ds') . 'Config' . path('ds') . 'router.php'; //startMeasure('App RouterProvider: ' . $path); $phpProvider = new Php(['file' => $path, 'prefix' => isset($this->config['prefix']) ? $this->config['prefix'] : null]); $phpProvider->init(); $this->router->addCachedInit(['autoloader' => [path('apps') . $this->app . path('ds') . 'src'], 'view' => [path('apps') . $this->app . path('ds') . 'src' . path('ds')]])->writeCache(); /* * And autoloader * */ // @T00D00 - this needs to be called on initialization ... $path = path('apps') . $this->app . path('ds') . 'src'; message('Registering autoloader (App route provider) ' . $path); autoloader()->add('', $path); /* * And add to twig? * */ Twig::addDir($path . path('ds'), Twig::PRIORITY_APP); /* * And then you finally realize this should be refactored to some kind of Command or AppInitializator * */ //stopMeasure('App RouterProvider: ' . $path); }
protected function initDev() { $cache = $this->getCache(); $data = $cache->get(); $this->routes = $data['routes']; $this->cachedInit = $data['cachedInit']; if (isset($this->cachedInit['autoloader'])) { foreach ($this->cachedInit['autoloader'] as $dir) { message('Register autoloader (Dev Router) ' . $dir); autoloader()->add('', $dir); } } if (isset($this->cachedInit['view'])) { foreach ($this->cachedInit['view'] as $dir) { Twig::addDir($dir); } } }
public function registerViewObjects($objects) { foreach ($objects as $key => $val) { $val = context()->getOrCreate($val); Twig::setStaticData($key, $val); } }
/** * List table records. * * @param Table $tableRecord * @param Entity $entity * * @return $this */ public function getViewTableAction(Table $tableRecord, DynamicService $dynamicService, DatabaseEntity $entity = null, $viewType = 'full') { /** * Set table. */ $this->dynamic->setTable($tableRecord); if (!$entity) { $entity = $tableRecord->createEntity(); $dir = path('app_src') . implode(path('ds'), array_slice(explode('\\', get_class($entity)), 0, -2)) . path('ds') . 'View' . path('ds'); Twig::addDir($dir); /** * This is needed for table actions. */ Twig::addDir($dir . 'tabelize' . path('ds') . 'recordActions' . path('ds')); Twig::addDir($dir . 'tabelize' . path('ds') . 'entityActions' . path('ds')); } /** * Apply entity extension. */ $this->dynamic->applyOnEntity($entity); /** * This is used for URLs. */ $entity->setStaticDynamicTable($tableRecord); /** * Join extensions. */ $dynamicService->joinTranslationsIfTranslatable($entity); $dynamicService->joinPermissionsIfPermissionable($entity); $dynamicService->removeDeletedIfDeletable($entity); /** * Get all relations for fields with type (select). */ $relations = (new Relations())->where('on_table_id', $tableRecord->id)->where('dynamic_relation_type_id', 1)->all(); foreach ($relations as $relation) { /** * Right table entity is created here. */ $relationEntity = $relation->showTable->createEntity(); $dynamicService->joinTranslationsIfTranslatable($relationEntity); /** * We need to add relations to select. * $tableRecord is for example users. * So entity is entity with table users. * We will fetch all users and related user_group_id and language_id * as user.relation_user_group_id and user.relation_language_id. */ $entity->with((new BelongsTo($entity, $relationEntity))->foreignKey($relation->onField->field)->fill('relation_' . $relation->onField->field)->after(function ($record) use($relation) { $record->setRelation('select_relation_' . $relation->onField->field, $relation); })); } /** * Filter records by $_GET['search'] */ $this->dynamic->getFilterService()->filterByGet($entity); $groups = $this->dynamic->getGroupService()->getAppliedGroups(); $records = $entity->count()->all(); $total = $records->total(); foreach ($groups as $group) { $records = $records->groupBy($group['field']); } $fieldTransformations = ['tabelizeClass']; if (get_class($entity) == Orders::class) { $fieldTransformations['bills_payed'] = function (Order $order) { return $order->getPayedBillsSum(); }; } /** * Transform field type = php */ $tableRecord->listableFields(function (HasMany $relation) { $relation->withFieldType(); })->each(function (Field $field) use(&$fieldTransformations) { if ($field->fieldType->slug == 'php') { $fieldTransformations[$field->field] = function ($record) use($field) { return $record->{$field->field}; }; } }); $tabelize = $this->tabelize()->setTable($tableRecord)->setTitle($tableRecord->getListTitle())->setEntity($entity)->setRecords($records)->setFields($tableRecord->listableFields(function (HasMany $relation) { $relation->withFieldType(); $relation->joinTranslations(); })->reduce(function (Field $field) use($tableRecord) { $fields = $_SESSION['pckg']['dynamic']['view']['table_' . $tableRecord->id]['view']['fields'] ?? []; return !$fields && $field->visible || in_array($field->field, $fields); }))->setPerPage(50)->setPage(1)->setTotal($total)->setGroups($groups ? range(1, count($groups)) : [])->setEntityActions($tableRecord->getEntityActions())->setRecordActions($tableRecord->getRecordActions())->setViews($tableRecord->actions()->keyBy('slug'))->setFieldTransformations($fieldTransformations); if ($this->request()->isAjax() && (strpos($_SERVER['REQUEST_URI'], '/tab/') === false || post('search'))) { return ['records' => $tabelize->transformRecords(), 'groups' => $groups]; } $tabelize->getView()->addData('dynamic', $this->dynamic); $tabelize->getView()->addData('viewType', $viewType); $tabelize->getView()->addData('searchUrl', router()->getUri()); return $tabelize; }
/** * @param $view * @param array $data * * @return Twig */ function view($view, $data = [], $assets = []) { $view = new Twig($view, $data); if ($parent = realpath(dirname(debug_backtrace()[0]['file']) . path('ds') . '..' . path('ds') . 'View' . path('ds'))) { if (is_dir($parent)) { $view->addDir($parent, Twig::PRIORITY_LAST); } $calculatedParent = realpath(dirname(debug_backtrace()[0]['file']) . path('ds') . '..' . path('ds') . '..' . path('ds') . '..' . path('ds')); if (is_dir($calculatedParent)) { $view->addDir($calculatedParent, Twig::PRIORITY_LAST); } } if ($assets) { assets($assets); } return $view; }
public function execute(callable $next) { Twig::addStaticData('_flash', $this->flash); Twig::addStaticData('_debugBar', debugBar()); return $next(); }