Overview

Namespaces

  • Deimos
    • Flow
      • Extension
        • TBlock
        • TFor
        • TForeach
        • TIF
        • TLiteral
        • TPHP
        • TSimple
        • TTry
        • TWhile
      • Traits
  • PHP

Classes

  • Deimos\Flow\Block
  • Deimos\Flow\Configure
  • Deimos\Flow\DefaultConfig
  • Deimos\Flow\DefaultContainer
  • Deimos\Flow\Extension\TBlock\TBlockStart
  • Deimos\Flow\Extension\TBlock\TEndBlock
  • Deimos\Flow\Extension\TFor\TEndFor
  • Deimos\Flow\Extension\TFor\TFor
  • Deimos\Flow\Extension\TForeach\TElseForeach
  • Deimos\Flow\Extension\TForeach\TEndForeach
  • Deimos\Flow\Extension\TForeach\TForeach
  • Deimos\Flow\Extension\TIF\TELSE
  • Deimos\Flow\Extension\TIF\TELSEIF
  • Deimos\Flow\Extension\TIF\TEndIF
  • Deimos\Flow\Extension\TIF\TIF
  • Deimos\Flow\Extension\TLiteral\TLiteral
  • Deimos\Flow\Extension\TPHP\TEndPHP
  • Deimos\Flow\Extension\TPHP\TPHP
  • Deimos\Flow\Extension\TSimple\TAssign
  • Deimos\Flow\Extension\TSimple\TBreak
  • Deimos\Flow\Extension\TSimple\TContinue
  • Deimos\Flow\Extension\TSimple\TExtends
  • Deimos\Flow\Extension\TSimple\TInclude
  • Deimos\Flow\Extension\TSimple\TPartial
  • Deimos\Flow\Extension\TSimple\TVariable
  • Deimos\Flow\Extension\TTry\TCatch
  • Deimos\Flow\Extension\TTry\TEndTry
  • Deimos\Flow\Extension\TTry\TFinally
  • Deimos\Flow\Extension\TTry\TTry
  • Deimos\Flow\Extension\TWhile\TEndWhile
  • Deimos\Flow\Extension\TWhile\TWhile
  • Deimos\Flow\Flow
  • Deimos\Flow\FlowForeach
  • Deimos\Flow\FlowFunction
  • Deimos\Flow\ForeachState
  • Deimos\Flow\Lexer
  • Deimos\Flow\LexerConst
  • Deimos\Flow\Tokenizer

Interfaces

  • Throwable

Traits

  • Deimos\Flow\Traits\Block
  • Deimos\Flow\Traits\FileSystem
  • Deimos\Flow\Traits\Tokenizer

Exceptions

  • BadFunctionCallException
  • Exception
  • InvalidArgumentException
  • LogicException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: namespace Deimos\Flow;
  4: 
  5: class Flow
  6: {
  7: 
  8:     /**
  9:      * @var Configure
 10:      */
 11:     protected $configure;
 12: 
 13:     /**
 14:      * @var string
 15:      */
 16:     protected $viewPath;
 17: 
 18:     /**
 19:      * @var string
 20:      */
 21:     protected $cachePath;
 22: 
 23:     /**
 24:      * @var string
 25:      */
 26:     protected $view;
 27: 
 28:     /**
 29:      * @var string
 30:      */
 31:     protected $curView;
 32: 
 33:     /**
 34:      * @var FlowForeach
 35:      */
 36:     public $foreach;
 37: 
 38:     /**
 39:      * Flow constructor.
 40:      *
 41:      * @param Configure $configure
 42:      */
 43:     public function __construct(Configure $configure = null)
 44:     {
 45:         if (!$configure)
 46:         {
 47:             $configure = new Configure();
 48:         }
 49: 
 50:         $this->configure = $configure;
 51:         $this->foreach   = new FlowForeach();
 52:     }
 53: 
 54:     /**
 55:      * @param string $path
 56:      *
 57:      * @return $this
 58:      *
 59:      * @throws \InvalidArgumentException
 60:      */
 61:     public function setTemplateDir($path)
 62:     {
 63:         $this->configure->template($path);
 64: 
 65:         return $this;
 66:     }
 67: 
 68:     /**
 69:      * @param string $path
 70:      *
 71:      * @return $this
 72:      *
 73:      * @throws \InvalidArgumentException
 74:      */
 75:     public function setCompileDir($path)
 76:     {
 77:         $this->configure->compile($path);
 78: 
 79:         return $this;
 80:     }
 81: 
 82:     /**
 83:      * @param string $view
 84:      *
 85:      * @return string
 86:      *
 87:      * @throws \InvalidArgumentException
 88:      */
 89:     protected function view($view = null)
 90:     {
 91:         if (!$this->view)
 92:         {
 93:             $this->view     = preg_replace('~(\.tpl)$~', '', $view);
 94:             $this->viewPath = $this->configure->template() . $this->view . $this->configure->ext();
 95:         }
 96: 
 97:         return $this->view;
 98:     }
 99: 
100:     /**
101:      * @return string
102:      */
103:     public function selectView()
104:     {
105:         return $this->view;
106:     }
107: 
108:     /**
109:      * @return string
110:      *
111:      * @throws \InvalidArgumentException
112:      */
113:     protected function cachePath()
114:     {
115:         if (!$this->cachePath)
116:         {
117:             $compile   = $this->configure->compile();
118:             $pathCache = $compile . dirname($this->view());
119:             $path      = $this->configure->createDirectory($pathCache);
120: 
121:             $this->cachePath = $path . basename($this->view()) . '.php';
122:         }
123: 
124:         return $this->cachePath;
125:     }
126: 
127:     /**
128:      * @return int
129:      *
130:      * @throws \InvalidArgumentException
131:      */
132:     protected function saveCache()
133:     {
134:         $compile = $this->compile();
135:         $view    = $this->viewPath;
136: 
137:         if (file_exists($view))
138:         {
139:             return file_put_contents($this->cachePath(), $compile);
140:         }
141: 
142:         throw new \InvalidArgumentException('File not found ' . $view);
143:     }
144: 
145:     /**
146:      * @return string
147:      */
148:     protected function curView()
149:     {
150:         if (!$this->curView)
151:         {
152:             $this->curView = file_get_contents($this->viewPath);
153:         }
154: 
155:         return $this->curView;
156:     }
157: 
158:     /**
159:      * @param string $compile
160:      *
161:      * @return array
162:      *
163:      * @throws \InvalidArgumentException
164:      */
165:     protected function lexer($compile)
166:     {
167:         return $this->configure->tokenizer()->lexer($compile);
168:     }
169: 
170:     /**
171:      * @param $view
172:      *
173:      * @return string
174:      */
175:     protected function removeComments($view)
176:     {
177:         return preg_replace('~{*(.*?)\*}([\s]+)?~', '', $view);
178:     }
179: 
180:     /**
181:      * @param $view
182:      *
183:      * @return string
184:      */
185:     protected function removePhpTags($view)
186:     {
187:         $view = preg_replace('~(<\?php)~', '<!-- ', $view);
188:         $view = preg_replace('~(<\?=)~', '<!-- ', $view);
189:         $view = preg_replace('~(<\?)~', '<!-- ', $view);
190: 
191:         return preg_replace('~(\?>)~', ' -->', $view);
192:     }
193: 
194:     protected function literal($view)
195:     {
196:         return preg_replace_callback('~{literal\s*}((\n|.)*){/literal}~', function ($matches)
197:         {
198:             $literal = new Extension\TLiteral\TLiteral($this, $this->configure, [$matches[1]]);
199: 
200:             return $literal->view();
201:         }, $view);
202:     }
203: 
204:     /**
205:      * @param string $command
206:      *
207:      * @return string
208:      *
209:      * @throws \InvalidArgumentException
210:      */
211:     protected function token($command)
212:     {
213:         return $this->configure->tokenizer()
214:             ->commandLexer($this, $this->configure, $command);
215:     }
216: 
217:     /**
218:      * @param $command
219:      *
220:      * @return string
221:      */
222:     protected function quote($command)
223:     {
224:         return '~' . preg_quote($command, null) . '~';
225:     }
226: 
227:     /**
228:      * @param string $command
229:      * @param string $data
230:      * @param string $text
231:      * @param int    $limit
232:      *
233:      * @return string
234:      */
235:     protected function replace($command, $data, $text, $limit = -1)
236:     {
237:         return preg_replace($this->quote($command), $data, $text, $limit);
238:     }
239: 
240:     /**
241:      * @param string $command
242:      * @param string $data
243:      * @param string $text
244:      *
245:      * @return string
246:      */
247:     protected function replaceOne($command, $data, $text)
248:     {
249:         return $this->replace($command, $data, $text, 1);
250:     }
251: 
252:     /**
253:      * @return mixed|string
254:      *
255:      * @throws \InvalidArgumentException
256:      */
257:     protected function compile()
258:     {
259:         $compile = $this->removeComments($this->curView());
260:         $compile = $this->removePhpTags($compile);
261:         $compile = $this->literal($compile);
262: 
263:         foreach ($this->lexer($compile) as $command)
264:         {
265:             $compile = $this->replaceOne(
266:                 '{' . $command . '}',
267:                 $this->token($command),
268:                 $compile
269:             );
270:         }
271: 
272:         return $compile;
273:     }
274: 
275:     /**
276:      * @param $name
277:      *
278:      * @return mixed
279:      */
280:     public function __get($name)
281:     {
282:         return $this->configure->getVariable($name);
283:     }
284: 
285:     /**
286:      * @param $name
287:      * @param $value
288:      */
289:     public function __set($name, $value)
290:     {
291:         $this->assign($name, $value);
292:     }
293: 
294:     /**
295:      * @param $name
296:      * @param $value
297:      */
298:     public function assign($name, $value)
299:     {
300:         $this->configure->setVariable($name, $value);
301:     }
302: 
303:     /**
304:      * @param $name
305:      *
306:      * @return bool
307:      */
308:     public function __isset($name)
309:     {
310:         return $this->configure->issetVariable($name);
311:     }
312: 
313:     /**
314:      * @param string $view
315:      *
316:      * @return string
317:      *
318:      * @throws \InvalidArgumentException
319:      */
320:     public function checkView($view)
321:     {
322:         $this->view($view);
323: 
324:         if ($this->configure->isDebug() ||
325:             !file_exists($this->cachePath()) ||
326:             filemtime($this->viewPath) > filemtime($this->cachePath())
327:         )
328:         {
329:             $this->saveCache();
330:         }
331: 
332:         return $this->cachePath();
333:     }
334: 
335:     /**
336:      * @param $view
337:      *
338:      * @return string
339:      *
340:      * @throws \InvalidArgumentException
341:      */
342:     public function parse($view)
343:     {
344:         $fullPath  = $this->checkView($view);
345:         $cachePath = realpath($this->configure->compile());
346: 
347:         $path = str_replace($cachePath, '', $fullPath);
348: 
349:         return ltrim($path, '/\\');
350:     }
351: 
352:     /**
353:      * @return int
354:      */
355:     public function random()
356:     {
357:         if (function_exists('random_int'))
358:         {
359:             return random_int(0, PHP_INT_MAX);
360:         }
361: 
362:         return mt_rand(0, PHP_INT_MAX);
363:     }
364: 
365:     /**
366:      * @param string $view
367:      *
368:      * @return string
369:      *
370:      * @throws \InvalidArgumentException
371:      */
372:     public function render($view)
373:     {
374:         $rand = $this->random();
375: 
376:         ${'____parse' . $rand} = $this->checkView($view);
377:         unset($view);
378: 
379:         extract($this->configure->getVariables(), EXTR_REFS);
380: 
381:         ob_start();
382:         require ${'____parse' . $rand};
383: 
384:         ${'____result' . $rand} = ob_get_clean();
385: 
386:         ${'____extends' . $rand} = $this->configure->getExtendsFile($this->selectView(), true);
387: 
388:         foreach (${'____extends' . $rand} as ${'____extend' . $rand})
389:         {
390:             ${'____result' . $rand} = (new static($this->configure))
391:                 ->render(${'____extend' . $rand}) . ${'____result' . $rand};
392:         }
393: 
394:         return ${'____result' . $rand};
395:     }
396: 
397: }
API documentation generated by ApiGen