Пример #1
0
 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;
 }
Пример #2
0
 /**
  * @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();
 }
Пример #3
0
 /**
  * @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;
 }
Пример #4
0
 /**
  * @return Entity
  * @throws \Exception
  */
 public function getRightEntity()
 {
     if (is_string($this->right)) {
         $this->right = Reflect::create($this->right);
     }
     return $this->right;
 }
Пример #5
0
 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;
 }
Пример #6
0
 /**
  * @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 -->';
         }
     }
 }
Пример #7
0
 /**
  * @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();
 }
Пример #8
0
 /**
  * @param $method
  * @param $args
  *
  * @return $this|Relation
  * @throws Exception
  */
 public function callWith($method, $args, $object, $returnRelation = false)
 {
     if (is_string($object)) {
         $object = Reflect::create($object);
     }
     /**
      * Check if $method prefix is listed in autocallPrefixes.
      */
     foreach ($this->autocallPrefixes as $prefix) {
         if (substr($method, 0, strlen($prefix)) === $prefix && strtoupper(substr($method, strlen($prefix), 1)) == substr($method, strlen($prefix), 1)) {
             /**
              * We are calling relation function without arguments: $entity->something();.
              */
             $relationMethod = lcfirst(substr($method, strlen($prefix)));
             $relation = Reflect::method($object, $relationMethod, $args);
             // $relation = $object->{$relationMethod}();
             if (isset($args[0]) && ($args[0] instanceof Closure || is_callable($args[0]))) {
                 /**
                  * If callback was added, we run it.
                  * Now, this is a problem if we're making join because query was already merged.
                  * So, we'll call this magically and provide both - relation and query.
                  *
                  * @T00D00
                  */
                 if ($prefix == 'join') {
                     $rightEntity = $relation->getRightEntity();
                     $oldEntityQuery = $rightEntity->getQuery();
                     $rightEntity->setQuery($relation->getQuery());
                     Reflect::call($args[0], [$relation, $relation->getQuery()]);
                     $rightEntity->setQuery($oldEntityQuery);
                 } else {
                     Reflect::call($args[0], [$relation, $relation->getQuery()]);
                 }
             }
             /**
              * We'll call $entity->with($relation), and return Relation;
              */
             $return = $object->{$prefix}($relation);
             /**
              * Then we return relation.
              */
             return $returnRelation ? $relation : $return;
         }
     }
     if (!method_exists($object, $method)) {
         if (prod()) {
             return null;
         }
         throw new Exception('Method ' . $method . ' does not exist in ' . get_class($object));
     }
     /**
      * Autoprefixes failed, return relation, probably?
      */
     $relation = Reflect::method($object, $method, $args);
     if (isset($args[0]) && ($args[0] instanceof Closure || is_callable($args[0]))) {
         Reflect::call($args[0], [$relation, $relation->getQuery()]);
     }
     return $relation;
 }
Пример #9
0
 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();
         }
     }
 }
Пример #10
0
 /**
  * @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;
 }
Пример #11
0
 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;
 }
Пример #12
0
 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();
 }
Пример #13
0
 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);
 }
Пример #14
0
 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();
 }
Пример #15
0
Файл: Auth.php Проект: pckg/auth
 /**
  * @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;
 }
Пример #16
0
 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;
     }];
 }
Пример #17
0
 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;
     }
 }
Пример #18
0
 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']);
 }
Пример #19
0
 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'));
         });
     }
 }
Пример #20
0
 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']);
 }
Пример #21
0
 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;
                 }
             }
         }
     }
 }
Пример #22
0
 function run()
 {
     Reflect::create(ProcessRouteMatch::class, ['match' => $this->match])->execute();
 }
Пример #23
0
 /**
  * @return mixed
  */
 public function prepareEntity()
 {
     return Reflect::create($this->getEntityClass());
 }
Пример #24
0
 public function resolve($class)
 {
     return Reflect::create($class);
 }
Пример #25
0
 public function createConsoleApplication($appName = null)
 {
     /**
      * Examples:
      *  - php console
      *  - php console project:update
      *  - php console derive
      *  - php console derive migrator:install
      */
     $argv = $_SERVER['argv'];
     $commandIndex = null;
     foreach ($argv as $key => $arg) {
         if (strpos($arg, ':')) {
             $commandIndex = $key;
             break;
         }
     }
     $application = null;
     if (!$appName) {
         if (!$commandIndex && isset($argv[1])) {
             $appName = $argv[1];
         } elseif ($commandIndex == 2) {
             $appName = $argv[1];
         } elseif (isset($argv[1]) && $commandIndex > 1) {
             $appName = $argv[1];
         }
     }
     /**
      * We need to set Console Application.
      */
     $this->bind(SymfonyConsole::class, new SymfonyConsole());
     if ($appName) {
         Context::bind('appName', $appName);
         path('app', path('root') . "app" . path('ds') . strtolower($appName) . path('ds'));
         path('app_src', path('app') . "src" . path('ds'));
         path('app_public', path('app') . "public" . path('ds'));
         path('app_uploads', path('uploads') . strtolower($appName) . path('ds'));
         path('app_storage', path('storage') . strtolower($appName) . path('ds'));
         path('app_private', path('private') . strtolower($appName) . path('ds'));
         /**
          * Add app src dir to autoloader and template engine.
          */
         $this->registerAutoloaders(path('app_src'), $this);
         /**
          * Now we will be able to create and register application provider.
          */
         $applicationProvider = Reflect::create(ucfirst($appName));
         /**
          * We register console provider so consoles can be easily accessable.
          */
         // (new ConsoleProvider())->register();
     } else {
         $applicationProvider = new ConsoleProvider();
     }
     $this->bind(Application::class, $applicationProvider);
     /**
      * Then we create actual application wrapper.
      */
     $application = new Console($applicationProvider);
     return $application;
 }
Пример #26
0
 /**
  * @param $method
  * @param $args
  *
  * @return $this
  * @throws Exception
  */
 public static function __callStatic($method, $args)
 {
     $entity = Reflect::create(static::class);
     return Reflect::method($entity, $method, $args);
 }
Пример #27
0
 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];
 }