protected function handleException(Throwable $e) { if (response()->getCode() == 200) { response()->code(404); } $code = $e->getCode() ? $e->getCode() : response()->getCode(); $message = $e->getMessage(); if (class_exists(DeriveAssets::class)) { Reflect::create(DeriveAssets::class)->register(); } $handled = false; $codes = [$code, 'default']; foreach ($codes as $file) { try { $response = view('Pckg\\Framework:error/' . $file, ['message' => $message, 'code' => $code, 'exception' => $e])->autoparse(); if ($response) { $handled = true; break; } } catch (Throwable $e) { dd(exception($e)); } } if ($handled) { echo $response; } else { echo $code . ' : ' . $message; } exit; }
/** * @return mixed */ public function execute() { $data = $this->loginForm->getRawData(['email', 'password']); foreach (config('pckg.auth.providers') as $providerKey => $providerConfig) { /** * Create and set new provider. */ $provider = Reflect::create($providerConfig['type'], [$this->auth]); $provider->setEntity($providerConfig['entity']); /** * If user doesnt exists, don't proceed with execution. */ if (!($user = $provider->getUserByEmailAndPassword($data['email'], sha1($data['password'] . $providerConfig['hash'])))) { continue; } /** * Try to login. */ $this->auth->useProvider($provider, $providerKey); if ($this->auth->performLogin($user)) { /** * @T00D00 - login user on all providers! */ $this->auth->useProvider($provider); trigger('user.loggedIn', [$this->auth->getUser()]); if (isset($data['autologin'])) { $this->auth->setAutologin(); } return $this->successful(); } } return $this->error(); }
/** * @return mixed */ public function execute() { $password = auth()->createPassword(); foreach (config('pckg.auth.providers') as $providerKey => $providerConfig) { if (!$providerConfig['forgotPassword']) { continue; } /** * Create and set new provider. */ $provider = Reflect::create($providerConfig['type'], [auth()]); $provider->setEntity($providerConfig['entity']); /** * If user doesnt exists, don't proceed with execution. */ if (!($user = $provider->getUserByEmail(post('email')))) { continue; } $user->password = sha1($password . $providerConfig['hash']); $user->save(); /** * Send email via queue. */ email('password-update', new User($user), ['data' => ['password' => $password], 'fetch' => ['user' => [$user->getEntityClass() => $user->id]]]); return $this->successful(); } return $this->error(); }
/** * @return Entity * @throws \Exception */ public function getRightEntity() { if (is_string($this->right)) { $this->right = Reflect::create($this->right); } return $this->right; }
/** * @T00D00 - this needs to be refactored without nesting ... * @return null */ public function runChains() { if (!$this->chains) { return null; } $next = $this->firstChain ?: function () { return $this; }; foreach (array_reverse($this->chains) as $chain) { $next = function () use($chain, $next) { if (is_string($chain)) { $chain = Reflect::create($chain); } if (is_callable($chain)) { $result = $chain(array_merge($this->args, ['next' => $next])); } else { $result = Reflect::method($chain, $this->runMethod, array_merge($this->args, ['next' => $next])); } return $result; }; } //startMeasure('Chain: ' . $this->runMethod . '()'); $result = $next(); //stopMeasure('Chain: ' . $this->runMethod . '()'); return $result; }
/** * Register options */ public function register() { if ($this->registered) { return $this; } $hadStack = context()->exists(Stack::class); if (!$hadStack) { context()->bind(Stack::class, new Stack()); } $this->registerAutoloaders($this->autoload()); $this->registerClassMaps($this->classMaps()); $this->registerApps($this->apps()); $this->registerProviders($this->providers()); $this->registerRoutes($this->routes()); $this->registerListeners($this->listeners()); $this->registerMiddlewares($this->middlewares()); $this->registerAfterwares($this->afterwares()); $this->registerPaths($this->paths()); $this->registerViewObjects($this->viewObjects()); $this->registerConsoles($this->consoles()); $this->registerAssets($this->assets()); if (method_exists($this, 'registered')) { Reflect::method($this, 'registered'); } $this->registered = true; /** * Some actions needs to be executed in reverse direction, for example config initialization. */ if (!$hadStack) { $stack = context()->get(Stack::class); $stack->execute(); } return $this; }
public function trigger($event, array $args = [], $method = null) { $eventName = $this->getEventName($event); $handlers = array_merge(isset($this->events[$eventName]) ? $this->events[$eventName]->getEventHandlers() : [], isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : []); $this->triggers[$eventName] = isset($this->triggers[$eventName]) ? $this->triggers[$eventName] + 1 : 1; if (!$handlers) { return null; } /** * Handlers are not chained anymore. * They are interrupted only if handler returns false. */ foreach ($handlers as $handler) { if (is_string($handler)) { $handler = Reflect::create($handler, $args); } if (is_callable($handler)) { Reflect::call($handler, $args); } else { if (is_object($handler)) { Reflect::method($handler, 'handle', $args); } } } return $this; }
/** * @return string * @throws Exception */ public function getHtml() { if ($this->class && $this->method) { $prefix = strtolower(request()->getMethod()); $args = array_merge($this->args, ['action' => $this]); $controller = Reflect::create($this->class, $args); $method = ($prefix ? $prefix . ucfirst($this->method) : $this->method) . 'Action'; if (isset($args['settings'])) { /** * We need to resolve dependencies. ;-) */ $args['settings']->each(function (Setting $setting) use(&$args) { $setting->pivot->resolve($args); }); } $result = null; $e = null; try { $result = Reflect::method($controller, $method, $args); } catch (Throwable $e) { if (prod()) { return null; } throw $e; } if (is_array($result)) { return $result; } else { return '<!-- ' . $this->class . '::' . $method . ' start -->' . ($e ? 'Exception: ' . exception($e) : $result) . '<!-- ' . $this->class . '::' . $method . ' end -->'; } } }
public function resolve(&$args) { if ($this->setting_id == 1 || $this->setting_id == 9) { $args[] = Reflect::create(Table::class)->resolve($this->value); } else { if ($this->setting_id == 7) { $args[] = (new ActionsMorphs())->where('id', $this->value)->oneOrFail(); } } }
/** * @return Entity * @throws \Exception */ public function getMiddleEntity() { if (is_string($this->middle)) { if (class_exists($this->middle)) { $this->middle = Reflect::create($this->middle); } else { $this->middle = (new Entity())->setTable($this->middle)->setRepository($this->getLeftEntity()->getRepository()); } } return $this->middle; }
public function allPermissions(callable $callable = null) { $permissionTable = $this->getPermissionableTable(); $repository = $this->getRepository(); $relation = $this->hasMany((new Entity($repository))->setTable($permissionTable))->foreignKey('id')->fill('allPermissions')->addSelect(['`' . $permissionTable . '`.*']); if ($callable) { $query = $relation->getRightEntity()->getQuery(); Reflect::call($callable, [$query, $relation, $this]); } return $relation; }
public function execute(callable $next) { if ($this->request->isGet() && !$this->request->isAjax()) { $output = $this->response->getOutput(); if (is_string($output) && substr($output, 0, 5) !== '<html' && strtolower(substr($output, 0, 9)) != '<!doctype') { $template = router()->get()['pckg']['generic']['template'] ?? 'Pckg\\Generic:generic'; $output = Reflect::create(Generic::class)->wrapIntoGeneric($output, $template); $this->response->setOutput($output); } } return $next(); }
public function make($controller, $method, $params = [], $byRequest = false) { /** * Create controller. */ $controller = Reflect::create($controller, $params); /** * Call action. */ $view = Reflect::method($controller, (!$byRequest ? $method : (request()->isPost() ? 'post' : 'get') . ucfirst($method)) . 'Action', $params); return (string) $view; }
public function create($key, $params = []) { if (is_array($key)) { return $this->createArray($key, $params); } if (!$this->canCreate($key)) { if (class_exists($key)) { return Reflect::create($key, $params); } throw new Exception($key . " isn't mapped in " . static::CLASS); } return Reflect::create(isset($this->mapper[$key]) ? $this->mapper[$key] : $key, $params); }
protected function initProd() { $router = $this->config->get('router'); if (isset($router['providers'])) { foreach ($router['providers'] as $providerType => $arrProviders) { foreach ($arrProviders as $provider => $providerConfig) { $routeProvider = Reflect::create('Pckg\\Framework\\Router\\Provider\\' . ucfirst($providerType), [$providerType => $provider, 'config' => $providerConfig, 'name' => $provider]); $routeProvider->init(); } } } $this->writeCache(); }
public function execute() { $viewHttp = $this->request->isPost() ? 'post' . ucfirst($this->view) : 'get' . ucfirst($this->view); $result = null; $data = $this->getResolved(); if (!method_exists($this->controller, $viewHttp . "Action")) { throw new Exception('Method ' . $viewHttp . 'Action() does not exist in ' . get_class($this->controller)); } /** * Call main route action. */ $result = Reflect::method($this->controller, $viewHttp . "Action", array_merge($this->data, $data)); return $result; }
public function classes() { /** * @T00D00 - this is useless, right? */ return [Record::class => function ($class) { throw new Exception('Is this needed? Empty class will be created ...'); return new $class(); $result = Reflect::create($class)->getEntity()->where('slug', $this->router->get('name'))->oneOrFail(function () use($class) { $this->response->notFound('Record ' . $class . ' not found, cannot be resolved'); }); return $result; }]; }
/** * @param ProviderInterface|mixed $provider * * @return $this */ public function useProvider($provider, $providerKey = 'default') { if (is_string($provider)) { if (!array_key_exists($provider, $this->providers)) { $config = config('pckg.auth.providers.' . $provider); $this->providers[$provider] = Reflect::create($config['type'], [$this]); } $provider = $this->providers[$provider]; } else { $this->providers[$providerKey] = $provider; } $this->provider = $provider; return $this; }
/** * @param $table * @param null $on * @param null $where * * @return $this */ public function join($table, $on = null, $where = null) { if ($table instanceof Relation) { if (is_callable($on)) { /** * Is this needed? */ Reflect::call($on, [$table, $table->getQuery()]); } $table->mergeToQuery($this->getQuery()); } else { $this->getQuery()->join($table, $on, $where); } return $this; }
public function resolve($form) { if (is_subclass_of($form, Form::class)) { $this->form = Reflect::create($form); $this->request = context()->getOrCreate(Request::class); if (object_implements($form, ResolvesOnRequest::class)) { if ($this->request->isPost()) { $this->response = context()->getOrCreate(Response::class); $this->flash = context()->getOrCreate(Flash::class); return $this->resolvePost(); } elseif ($this->request->isGet()) { return $this->resolveGet(); } } return $this->form; } }
public function init() { //startMeasure('Php RouterProvider: ' . $this->config['file']); $router = (require $this->config['file']); $prefix = isset($this->config['prefix']) ? $this->config['prefix'] : null; if (isset($router['providers'])) { foreach ($router['providers'] as $providerType => $arrProviders) { foreach ($arrProviders as $provider => $providerConfig) { if (isset($providerConfig['prefix'])) { $providerConfig['prefix'] = $prefix . (isset($providerConfig['prefix']) ? $providerConfig['prefix'] : ''); } $routeProvider = Reflect::create('Pckg\\Framework\\Router\\Provider\\' . ucfirst($providerType), [$providerType => $prefix . $provider, 'config' => $providerConfig]); $routeProvider->init(); } } } //stopMeasure('Php RouterProvider: ' . $this->config['file']); }
public function registerApps($apps) { /** * Apps need to be initialized in reverse direction. * Now, how will we manage to do this? * */ if (!is_array($apps)) { $apps = [$apps]; } $stack = context()->get(Stack::class); foreach ($apps as $app) { $appDir = path('apps') . strtolower($app) . path('ds') . 'src'; $this->registerAutoloaders($appDir); $appObject = Reflect::create(ucfirst($app)); $appObject->register(); $stack->push(function () use($app) { config()->parseDir(path('apps') . strtolower($app) . path('ds')); }); } }
public function init() { //startMeasure('Src RouterProvider: ' . $this->config['src']); foreach ([path('app_src') . $this->config['src'] . path('ds'), path('root') . $this->config['src'] . path('ds')] as $dir) { if (is_dir($dir)) { context()->get(Config::class)->parseDir($dir); } } foreach ([path('app_src') . $this->config['src'] . path('ds') . 'Config/router.php', path('root') . $this->config['src'] . path('ds') . 'Config/router.php'] as $file) { if (!is_file($file)) { continue; } $phpProvider = new Php(['file' => $file, 'prefix' => isset($this->config['prefix']) ? $this->config['prefix'] : null]); $phpProvider->init(); // then we have to find provider $class = $this->src . '\\Provider\\Config'; if (class_exists($class)) { $provider = Reflect::create($class); $provider->register(); } } //stopMeasure('Src RouterProvider: ' . $this->config['src']); }
public function resolve($class) { if (isset(static::$bind[$class]) && context()->exists($class)) { return context()->get($class); } if (class_exists($class) && in_array($class, static::$singletones)) { $newInstance = Reflect::create($class); if (isset(static::$bind[$class])) { context()->bind($class, $newInstance); return $newInstance; } } foreach (context()->getData() as $object) { if (is_object($object)) { if (get_class($object) === $class || is_subclass_of($object, $class)) { return $object; } else { if (in_array($class, class_implements($object))) { return $object; } } } } }
function run() { Reflect::create(ProcessRouteMatch::class, ['match' => $this->match])->execute(); }
public function init() { list($namespace, $method) = explode('::', $this->config); Reflect::method($namespace, $method); }
/** * */ public function with($relation, $callback = null) { if ($relation == $this) { return $this; } if (is_callable($callback)) { Reflect::call($callback, [$relation, $relation->getQuery()]); } if ($relation instanceof Relation) { $this->with[] = $relation; } else { $this->with[] = $this->{$relation}(); } return $this; }
public function reflect(callable $callable, $entity, $query = null) { Reflect::call($callable, [$query ?? $this->getQuery(), $this, $entity]); }
protected function getTabelizesAndFunctionizes($tabs, $record, $table) { $relations = $table->hasManyRelation(function (HasMany $query) { $query->where('dynamic_relation_type_id', 2); $query->where('dynamic_table_tab_id', null); }); $tabelizes = []; $recordsController = Reflect::create(Records::class); $relations->each(function (Relation $relation) use($tabs, $record, &$tabelizes, $recordsController) { $entity = $relation->showTable->createEntity(); $entity->where($relation->onField->field, $record->id); $tableResolver = Reflect::create(\Pckg\Dynamic\Resolver\Table::class); $table = $tableResolver->resolve($tableResolver->parametrize($relation->showTable)); $tabelize = $recordsController->getViewTableAction($table, $this->dynamic, $entity); if ($tabs->count()) { $tabelizes[$relation->dynamic_table_tab_id ?? 0][] = (string) $tabelize; } else { $tabelizes[] = (string) $tabelize; } }); $functionizes = []; $functions = $table->functions; $pluginService = $this->pluginService; $functions->each(function (Func $function) use($tabs, &$functionizes, $pluginService, $record) { $functionize = $pluginService->make($function->class, ($this->request()->isGet() ? 'get' : 'post') . ucfirst($function->method), [$record]); if ($tabs->count()) { $functionizes[$function->dynamic_table_tab_id ?? 0][] = (string) $functionize; } else { $functionizes[] = (string) $functionize; } }); return [$tabelizes, $functionizes]; }
/** * @return $this */ public function initExtensions() { foreach (get_class_methods($this) as $method) { if (substr($method, 0, 4) == 'init' && substr($method, -9) == 'Extension') { $this->{$method}(); } else { if (substr($method, 0, 6) == 'inject' && substr($method, -12) == 'Dependencies') { Reflect::method($this, $method); } } } return $this; }