Пример #1
0
 public function render()
 {
     $template = new TempFile($cacheId = sha1($this->resource->getUri()));
     //			if (!$template->exists()) {
     $template->openForWrite();
     $template->write($this->cache->load($cacheId, function () use($cacheId) {
         $header = sprintf("<?php\n\t/**\n\t * stamp: %s\n\t *\n\t * layout:\n\t * %s", DateTimeUtils::from('now')->format('Y-m-d H:i:s'), $this->resource->getUri());
         if (!empty($this->resourceList)) {
             $header .= "\n\n\t * used templates:";
         }
         foreach ($this->resourceList as $resource) {
             $header .= "\n\t * - " . $resource->getUri() . "\n";
         }
         $header .= "\t */\n?>\n";
         return $this->cache->save($cacheId, $header . $this->compile());
     }));
     $template->close();
     //			}
     call_user_func(function () use($template) {
         require_once $template->getFullFileName();
     });
 }
Пример #2
0
 public function index()
 {
     /**
      * because we can run withou caching, this flag is needed
      */
     if ($this->built === true) {
         return $this;
     }
     /**
      * intentionally first line - built is done even if failature occures
      */
     $this->built = true;
     if ($this->cache->load('index') === true) {
         return $this;
     }
     $exclude = '~(' . implode(')|(', $this->excludeList) . ')~';
     $this->storage->truncate($schema = $this->createScannerFile()->schema());
     $this->storage->createSchema($schema);
     $this->storage->startTransaction(self::class);
     try {
         foreach ($this->pathList as $directory) {
             foreach ($this->createIterator($directory) as $path => $item) {
                 /**
                  * nice line, isn't it ;)?
                  */
                 if (!empty($this->excludeList) && StringUtils::match(($tmp = realpath($path)) ? $path = $tmp : $path, $exclude)) {
                     continue;
                 }
                 $finfo = new \finfo(FILEINFO_MIME);
                 $scannerFile = $this->createScannerFile();
                 $scannerFile->restore(['file' => FileUtils::normalize($item->getRealPath()), 'extension' => ($extension = $item->getExtension()) ? $extension : null, 'mime' => $finfo->file($path), 'size' => (int) $item->getSize(), 'stamp' => DateTimeUtils::create(), 'changed' => DateTimeUtils::from($item->getMTime())]);
                 $this->storage->save($scannerFile);
             }
         }
         $this->storage->commitTransaction(self::class);
         $this->cache->save('index', true);
     } catch (\Exception $e) {
         $this->storage->rollbackTransaction(self::class);
         throw $e;
     }
     return $this;
 }
 /**
  * data směrem ven z aplikace (tzn. směrem k uživateli)
  *
  * @param mixed $value
  *
  * @return DateTimeUtils
  */
 public function output($value)
 {
     return DateTimeUtils::from($value);
 }
Пример #4
0
 /**
  * @param string $indent indentation (for block-parser)
  * @param mixed $result
  * @param mixed $key
  * @param mixed $hasKey
  *
  * @return array
  */
 private function parse($indent, $result = null, $key = null, $hasKey = false)
 {
     $inlineParser = $indent === false;
     $value = null;
     $hasValue = false;
     $tokens = $this->tokens;
     $n =& $this->pos;
     $count = count($tokens);
     $mainResult =& $result;
     for (; $n < $count; $n++) {
         $t = $tokens[$n][0];
         if ($t === ',') {
             // ArrayEntry separator
             if (!$hasKey && !$hasValue || !$inlineParser) {
                 $this->error();
             }
             $this->addValue($result, $hasKey ? $key : null, $hasValue ? $value : null);
             $hasKey = $hasValue = false;
         } elseif ($t === ':' || $t === '=') {
             // KeyValuePair separator
             if ($hasValue && (is_array($value) || is_object($value))) {
                 $this->error('Unacceptable key');
             } elseif ($hasKey && $key === null && $hasValue && !$inlineParser) {
                 $n++;
                 $result[] = $this->parse($indent . '  ', [], $value, true);
                 $newIndent = isset($tokens[$n], $tokens[$n + 1]) ? (string) substr($tokens[$n][0], 1) : '';
                 // not last
                 if (strlen($newIndent) > strlen($indent)) {
                     $n++;
                     $this->error('Bad indentation');
                 } elseif (strlen($newIndent) < strlen($indent)) {
                     return $mainResult;
                     // block parser exit point
                 }
                 $hasKey = $hasValue = false;
             } elseif ($hasKey || !$hasValue) {
                 $this->error();
             } else {
                 $key = (string) $value;
                 $hasKey = true;
                 $hasValue = false;
                 $result =& $mainResult;
             }
         } elseif ($t === '-') {
             // BlockArray bullet
             if ($hasKey || $hasValue || $inlineParser) {
                 $this->error();
             }
             $key = null;
             $hasKey = true;
         } elseif (isset(self::$brackets[$t])) {
             // Opening bracket [ ( {
             if ($hasValue) {
                 if ($t !== '(') {
                     $this->error();
                 }
                 $n++;
                 if ($value instanceof Entity && $value->value === Neon::CHAIN) {
                     end($value->attributes)->attributes = $this->parse(false, []);
                 } else {
                     $value = new Entity($value, $this->parse(false, []));
                 }
             } else {
                 $n++;
                 $value = $this->parse(false, []);
             }
             $hasValue = true;
             if (!isset($tokens[$n]) || $tokens[$n][0] !== self::$brackets[$t]) {
                 // unexpected type of bracket or block-parser
                 $this->error();
             }
         } elseif ($t === ']' || $t === '}' || $t === ')') {
             // Closing bracket ] ) }
             if (!$inlineParser) {
                 $this->error();
             }
             break;
         } elseif ($t[0] === "\n") {
             // Indent
             if ($inlineParser) {
                 if ($hasKey || $hasValue) {
                     $this->addValue($result, $hasKey ? $key : null, $hasValue ? $value : null);
                     $hasKey = $hasValue = false;
                 }
             } else {
                 while (isset($tokens[$n + 1]) && $tokens[$n + 1][0][0] === "\n") {
                     $n++;
                     // skip to last indent
                 }
                 if (!isset($tokens[$n + 1])) {
                     break;
                 }
                 $newIndent = (string) substr($tokens[$n][0], 1);
                 if ($indent === null) {
                     // first iteration
                     $indent = $newIndent;
                 }
                 $minlen = min(strlen($newIndent), strlen($indent));
                 if ($minlen && (string) substr($newIndent, 0, $minlen) !== (string) substr($indent, 0, $minlen)) {
                     $n++;
                     $this->error('Invalid combination of tabs and spaces');
                 }
                 if (strlen($newIndent) > strlen($indent)) {
                     // open new block-array or hash
                     if ($hasValue || !$hasKey) {
                         $n++;
                         $this->error('Bad indentation');
                     }
                     $this->addValue($result, $key, $this->parse($newIndent));
                     $newIndent = isset($tokens[$n], $tokens[$n + 1]) ? (string) substr($tokens[$n][0], 1) : '';
                     // not last
                     if (strlen($newIndent) > strlen($indent)) {
                         $n++;
                         $this->error('Bad indentation');
                     }
                     $hasKey = false;
                 } else {
                     if ($hasValue && !$hasKey) {
                         // block items must have "key"; NULL key means list item
                         break;
                     } elseif ($hasKey) {
                         $this->addValue($result, $key, $hasValue ? $value : null);
                         if ($key !== null && !$hasValue && $newIndent === $indent && isset($tokens[$n + 1]) && $tokens[$n + 1][0] === '-') {
                             $result =& $result[$key];
                         }
                         $hasKey = $hasValue = false;
                     }
                 }
                 if (strlen($newIndent) < strlen($indent)) {
                     // close block
                     return $mainResult;
                     // block parser exit point
                 }
             }
         } elseif ($hasValue) {
             // Value
             if ($value instanceof Entity) {
                 // Entity chaining
                 if ($value->value !== Neon::CHAIN) {
                     $value = new Entity(Neon::CHAIN, [$value]);
                 }
                 $value->attributes[] = new Entity($t);
             } else {
                 $this->error();
             }
         } else {
             // Value
             static $consts = ['true' => true, 'True' => true, 'TRUE' => true, 'yes' => true, 'Yes' => true, 'YES' => true, 'on' => true, 'On' => true, 'ON' => true, 'false' => false, 'False' => false, 'FALSE' => false, 'no' => false, 'No' => false, 'NO' => false, 'off' => false, 'Off' => false, 'OFF' => false, 'null' => 0, 'Null' => 0, 'NULL' => 0];
             if ($t[0] === '"') {
                 $value = preg_replace_callback('#\\\\(?:ud[89ab][0-9a-f]{2}\\\\ud[c-f][0-9a-f]{2}|u[0-9a-f]{4}|x[0-9a-f]{2}|.)#i', [$this, 'cbString'], substr($t, 1, -1));
             } elseif ($t[0] === "'") {
                 $value = substr($t, 1, -1);
             } elseif (isset($consts[$t]) && (!isset($tokens[$n + 1][0]) || $tokens[$n + 1][0] !== ':' && $tokens[$n + 1][0] !== '=')) {
                 $value = $consts[$t] === 0 ? null : $consts[$t];
             } elseif (is_numeric($t)) {
                 $value = $t * 1;
             } elseif (preg_match('#\\d\\d\\d\\d-\\d\\d?-\\d\\d?(?:(?:[Tt]| +)\\d\\d?:\\d\\d:\\d\\d(?:\\.\\d*)? *(?:Z|[-+]\\d\\d?(?::\\d\\d)?)?)?\\z#A', $t)) {
                 $value = DateTimeUtils::from($t);
             } else {
                 // literal
                 $value = $t;
             }
             $hasValue = true;
         }
     }
     if ($inlineParser) {
         if ($hasKey || $hasValue) {
             $this->addValue($result, $hasKey ? $key : null, $hasValue ? $value : null);
         }
     } else {
         if ($hasValue && !$hasKey) {
             // block items must have "key"
             if ($result === null) {
                 $result = $value;
                 // simple value parser
             } else {
                 $this->error();
             }
         } elseif ($hasKey) {
             $this->addValue($result, $key, $hasValue ? $value : null);
         }
     }
     return $mainResult;
 }
 /**
  * data směrem ven z aplikace (tzn. směrem k uživateli)
  *
  * @param mixed $value
  *
  * @return string
  */
 public function output($value)
 {
     return DateTimeUtils::from($value)->format($this->getOption('format-output', 'd.m.Y H:i:s'));
 }