Example #1
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 -->';
         }
     }
 }
Example #2
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;
 }
Example #3
0
 public function autoparse()
 {
     self::addDir(path('root'), Twig::PRIORITY_LAST);
     $this->initTwig($this->file);
     if ($this->file) {
         $this->twig = $this->twig->loadTemplate($this->file . ".twig");
     } else {
         $this->twig = $this->twig->createTemplate($this->template);
     }
     try {
         /**
          * Trigger rendering event so we can attach some handlers.
          */
         trigger(RenderingView::class, ['view' => $this->file]);
         /**
          * Render template.
          */
         $render = measure('Rendering ' . $this->file, function () {
             return $this->twig->render($this->getFullData());
         });
         if ($render == $this->file . '.twig') {
             if (prod()) {
                 return null;
             }
             return '<p style="color: black; font-weight: bold; background-color: red;">' . 'Cannot load file ' . $this->file . '</p>';
         }
         return $render;
     } catch (Twig_Error_Syntax $e) {
         return "<pre>Twig error:" . exception($e) . "</pre>";
     } catch (Throwable $e) {
         return '<pre>' . exception($e) . '</pre>';
     }
 }
Example #4
0
 public function internal($url = null)
 {
     try {
         if (!$url) {
             $url = $_SERVER['REQUEST_URI'];
         }
         message('Internal redirect to ' . $url);
         /**
          * Set GET method.
          */
         $_SERVER['REQUEST_METHOD'] = 'GET';
         $_SERVER['REQUEST_URI'] = $url;
         $_POST = [];
         /**
          * Replace prefix in url because environment was already set.
          */
         $url = env()->replaceUrlPrefix($url);
         /**
          * Set request url.
          */
         request()->setUrl($url);
         /**
          * Make request internal so we increase counter.
          */
         request()->setInternal();
         /**
          * Find match.
          */
         request()->init();
         /**
          * Run actions.
          */
         request()->run();
         /**
          * Output.
          */
         response()->run();
         exit;
     } catch (Throwable $e) {
         if (prod()) {
             die(exception($e));
             die("Unknown internal error");
         }
         die(exception($e));
     }
     exit;
 }
Example #5
0
function __i18n($key, $data = [], $lang = null)
{
    try {
        $translator = context()->getOrCreate(Translator::class);
        $translation = trim($translator->get($key, $lang));
        return $data ? (new Twig(null, $data))->setTemplate($translation)->autoparse() : $translation;
    } catch (Throwable $e) {
        if (prod()) {
            return $key;
        }
        throw $e;
    }
}
Example #6
0
 /**
  *
  *
  * @param $method
  * @param $args
  *
  * @return mixed
  */
 public function __call($method, $args)
 {
     /**
      * Return value from empty relation.
      */
     $entity = $this->getEntity();
     /**
      * @T00D00 - with should be called only if method starts with 'join' or 'with'
      */
     $relation = $entity->callWith($method, $args, $entity, true);
     if (!$relation && prod()) {
         return null;
     }
     /**
      * This is not needed here?
      */
     //$relation->fill($method);
     $relation->fillRecord($this);
     return $this->getRelation($relation->getFill());
 }