Esempio n. 1
0
 /**
  * @param Config $config
  * @return Route
  */
 public static function element($config)
 {
     $spec = $config->core();
     $type = $config->value('type');
     //
     if ($type) {
         $class = str_replace('~', __NAMESPACE__, $type);
         $factory = "{$class}::factory";
         if (is_callable($factory)) {
             $route = $factory($config);
         } else {
             throw new Exception\RuntimeException("Factory [{$factory}] is not callable", 1);
         }
         if (!$route instanceof Route) {
             throw new Exception\RuntimeException("Factory [{$factory}] did not resurn a Route instance", 2);
         }
     } elseif (isset($spec['regex'])) {
         $route = RegexRoute::factory($config);
     } elseif (isset($spec['path']) || isset($spec['domain'])) {
         $route = SimpleRoute::factory($config);
     } else {
         $route = SegmentRoute::factory($config);
     }
     return $route;
 }
Esempio n. 2
0
 /**
  * @param string $level
  * @param string $message
  * @param array $context
  */
 public function log($level, $message, array $context = array())
 {
     $max = $this->levelOrder($this->getFilterLevel());
     $ord = $this->levelOrder($level);
     if ($ord <= $max) {
         $params = new Param\Node($context);
         $prefix = "[{$level}] ";
         $suffix = $this->type == self::T_FILE ? "\n" : "";
         $output = $prefix . $params->render($message) . $suffix;
         error_log($output, $this->type, $this->destination);
     }
 }
Esempio n. 3
0
 /**
  * Logs a message
  * @param type $level
  * @param type $message
  * @param array $context
  * @return void
  */
 public function log($level, $message, array $context = array())
 {
     $target = $this->getTargetFile();
     $max = $this->levelOrder($this->getFilterLevel());
     $order = $this->levelOrder($level);
     if ($target && $order <= $max) {
         $params = new Param\Node($context);
         $timestamp = $this->getTimestamp();
         $resolved = $params->render($message);
         $output = "[{$timestamp}] [{$level}] {$resolved}\n";
         $target->fwrite($output);
     }
 }
Esempio n. 4
0
 /**
  * @param array|Traversable $config
  */
 public function fromConfig($config)
 {
     $params = Param\Node::cast($config);
     foreach ($params as $key => $node) {
         $this->setSeed($key, $node->getData());
     }
     return $this;
 }
Esempio n. 5
0
 /**
  * @param Param $params
  * @return object
  * @throws Exception\RuntimeException
  */
 protected function controllerInstance(Param\Node $params, Resources $context, &$class = null)
 {
     // Get the parameter values
     $name = $params->value('controller');
     if (!$name) {
         throw new Exception\RuntimeException("Routing parameter 'controller' is empty", 5);
     }
     $class = $this->className($name);
     $instance = null;
     // Create the instance
     if ($context && $context->has($class)) {
         $instance = $context->get($class);
     } elseif (class_exists($class, true)) {
         $instance = new $class();
     }
     // Initialize
     if (method_exists($instance, 'init')) {
         $instance->init($context);
     }
     return $instance;
 }
Esempio n. 6
0
 /**
  * @param type $config
  * @return static
  */
 public static function factory($config)
 {
     $route = new static();
     $param = Param\Node::cast($config);
     $route->setSource($param->value('subject'));
     $route->setPattern($param->value('segment', $param->value('pattern')));
     $route->setConstraints($param->value('constraints'));
     $route->setDefaults($param->value('defaults', $param->value('params')));
     if ($param->value('children')) {
         $route->setPartial(true);
     }
     return $route;
 }
Esempio n. 7
0
 /**
  * @return boolean
  */
 protected function tableExists()
 {
     $exists = null;
     $table = $this->params->value('session_table');
     if ($table) {
         $db = $this->getDb();
         try {
             $status = $db->query("SELECT 1 FROM {$table} LIMIT 1");
             $exists = (bool) $status;
         } catch (PDOException $ex) {
             $ex->getCode();
             $exists = false;
         }
     }
     return $exists;
 }
Esempio n. 8
0
 /**
  * @param mixed $value
  * @param bool $force
  * @param bool $silent
  * @return int  Number of errors
  */
 public function filterValue($value, $force = false, $silent = false)
 {
     $status = 0;
     $filtered = $value;
     //
     foreach ($this->getFilters(true) as $spec) {
         $filtered = $this->applyFilter($value, $spec);
         $status = $this->filterErrorCount($filtered, $spec['options']);
         if ($status > 0) {
             $template = $spec['message'] ?: "Invalid data in <{field.name}>";
             if (!$silent) {
                 $json = json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                 $model = Params::cast(['field' => $this, 'data' => $value, 'serialized' => $json]);
                 $this->addError($spec['errno'], $model->render($template));
             }
             break;
         }
     }
     if ($status == 0 || $force) {
         $this->setValue($filtered);
     }
     return $status;
 }
Esempio n. 9
0
 /**
  * @param array|Params $params
  * @return static
  */
 public function setParams($params)
 {
     if ($params !== null) {
         $params = Params::cast($params);
         $params->setDelimiters(['{', '}']);
     }
     $this->params = $params;
     return $this;
 }
Esempio n. 10
0
 /**
  * @return self
  */
 public function current()
 {
     return $this->node->createChild($this->getInnerIterator()->current());
 }
Esempio n. 11
0
 /**
  * @param Model $model
  * @param int $depth
  * @param string|CssClass $base
  * @return CssClass
  */
 protected function itemClass(Model $model, $depth, $base)
 {
     $class = CssClass::cast($base);
     $class->add($model->value('class'));
     return $class;
 }
Esempio n. 12
0
 /**
  * @param array|Param\Node $config
  * @return static
  */
 public function setConfig($config)
 {
     $this->setResource(self::CONFIG, Param\Node::cast($config));
     return $this;
 }
Esempio n. 13
0
 public function testWalk()
 {
     $node = new Node(['a' => 11, 'b' => 22, 'c' => ['m', 'n']]);
     $node->walk(function (&$v, $k) {
         $v .= '/' . $k;
     });
     // var_dump($node->core());
 }
Esempio n. 14
0
 /**
  * @return static
  */
 public function clean()
 {
     $this->data->setData(null);
     return $this;
 }
Esempio n. 15
0
 /**
  * @param Param\Node $defaults
  * @return static
  */
 public function setDefaults($defaults)
 {
     $this->defaults = Param\Node::cast($defaults);
     return $this;
 }
Esempio n. 16
0
 /**
  * @param int $status
  * @param Param\Node $config
  * @return string
  */
 protected function defaultLayout($status, Param\Node $config)
 {
     if ($status && $status != 200) {
         $layout = $config->value('error_layout', 'layout/error');
     } else {
         $layout = $config->value('default_layout', 'layout/default');
     }
     return $layout;
 }
Esempio n. 17
0
 /**
  * @param string $source
  * @param Data $data
  */
 public function render($source, $data, $referer = null)
 {
     $compiler = $this->getCompiler();
     $name = ltrim($referer, '/');
     $key = static::INLINE;
     //
     $this->reset();
     $this->outPush();
     $program = $compiler->compile($source, $name);
     $this->sources[$key] = $source;
     $this->programs[$key] = $program;
     $model = Data::cast($data, $this->dataOptions());
     $this->execProgram($key, $this->prepareData($key, $model));
     $this->cleanupData($key, $model);
     $out = $this->outPop();
     //
     return $out;
 }
Esempio n. 18
0
 /**
  * @param string $level
  * @param string $message
  * @param array $context
  */
 public function log($level, $message, array $context = array())
 {
     $logger = $this->getLogger();
     if ($logger) {
         $logger->log($level, $message, $context);
     } else {
         $params = new Param($context);
         $message = $params->render($message);
         error_log("{$level}: {$message}");
     }
 }