-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathFragment.php
More file actions
46 lines (38 loc) · 1.22 KB
/
Fragment.php
File metadata and controls
46 lines (38 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
*
* Fragments.
*
*/
namespace CssCrush;
class Fragment extends Template
{
public $name;
public function __construct($str, $options = [])
{
parent::__construct($str);
$this->name = $options['name'];
}
public function __invoke(?array $args = null, $str = null)
{
$str = parent::__invoke($args);
// Flatten all fragment calls within the template string.
while (preg_match(Regex::$patt->fragmentInvoke, $str, $m, PREG_OFFSET_CAPTURE)) {
$name = strtolower($m['name'][0]);
$fragment = isset(Crush::$process->fragments[$name]) ? Crush::$process->fragments[$name] : null;
$replacement = '';
$start = $m[0][1];
$length = strlen($m[0][0]);
// Skip over same named fragments to avoid infinite recursion.
if ($fragment && $name !== $this->name) {
$args = [];
if (isset($m['parens'][1])) {
$args = Functions::parseArgs($m['parens_content'][0]);
}
$replacement = $fragment($args);
}
$str = substr_replace($str, $replacement, $start, $length);
}
return $str;
}
}