|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the prooph/php-ddd-cargo-sample. |
|
4
|
|
|
* (c) Alexander Miertsch <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* Date: 8/8/15 - 12:37 AM |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Codeliner\CargoUI; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class RiotCompiler |
|
15
|
|
|
* |
|
16
|
|
|
* This is a very basic compiler for riot.js tag files. |
|
17
|
|
|
* It splits content of a <riot-tag-name>.html file into html and js and compiles |
|
18
|
|
|
* the parts into a riot.tag() statement. |
|
19
|
|
|
* |
|
20
|
|
|
* @package Codeliner\CargoUI |
|
21
|
|
|
* @author Alexander Miertsch <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
final class RiotCompiler |
|
24
|
|
|
{ |
|
25
|
|
|
private $riotTagsRootDir = 'CargoUI/view/riot'; |
|
26
|
|
|
|
|
27
|
|
|
public function compileToRiotStatements() |
|
28
|
|
|
{ |
|
29
|
|
|
$riotRootDir = new \DirectoryIterator($this->riotTagsRootDir); |
|
30
|
|
|
|
|
31
|
|
|
return $this->compileAll($riotRootDir); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private function compileAll(\DirectoryIterator $iterator) |
|
35
|
|
|
{ |
|
36
|
|
|
$jsContent = ""; |
|
37
|
|
|
|
|
38
|
|
|
/** @var $info \DirectoryIterator */ |
|
39
|
|
|
foreach ($iterator as $info) { |
|
40
|
|
|
if ($info->isFile()) { |
|
41
|
|
|
$jsContent.= $this->compileFile($info); |
|
42
|
|
|
} elseif (!$info->isDot()) { |
|
43
|
|
|
$jsContent.= $this->compileAll(new \DirectoryIterator($iterator->getPath() . DIRECTORY_SEPARATOR . $info->getBasename())); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $jsContent; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function compileFile(\DirectoryIterator $file) |
|
51
|
|
|
{ |
|
52
|
|
|
$tag = file_get_contents($file->getPathname()); |
|
53
|
|
|
$tagName = $file->getBasename('.html'); |
|
54
|
|
|
$jsFunc = $this->extractJsFunction($tag, $tagName); |
|
55
|
|
|
$tagHtml = $this->removeJsFromTag($tag, $tagName); |
|
56
|
|
|
|
|
57
|
|
|
$tagHtml = str_replace('"', '\"', $tagHtml); |
|
58
|
|
|
$tagHtml = preg_replace("/\r|\n/", "", $tagHtml); |
|
59
|
|
|
|
|
60
|
|
|
return 'riot.tag("'.$tagName.'", "' . $tagHtml . '", '.$jsFunc.');'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function extractJsFunction($tag, $tagName) |
|
64
|
|
|
{ |
|
65
|
|
|
preg_match('/<script .*type="text\/javascript"[^>]*>[\s]*(?<func>function.+\});?[\s]*<\/script>/is', $tag, $matches); |
|
66
|
|
|
|
|
67
|
|
|
if (! $matches['func']) { |
|
68
|
|
|
throw new \RuntimeException("Riot tag javascript function could not be found for tag name: " . $tagName); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $matches['func']; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function removeJsFromTag($tag, $tagName) |
|
75
|
|
|
{ |
|
76
|
|
|
$tag = preg_replace('/<script .*type="text\/javascript"[^>]*>.*<\/script>/is', "", $tag); |
|
77
|
|
|
|
|
78
|
|
|
if (! $tag) { |
|
79
|
|
|
throw new \RuntimeException("Riot tag template compilation failed for tag: " . $tagName . " with error code: " . preg_last_error()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $tag; |
|
83
|
|
|
} |
|
84
|
|
|
} |