26 protected $prototypes;
30 $this->factory = $factory;
31 $this->prototypes = array();
33 foreach ($this->registerPrototypes() as $prototype =>
$options) {
34 $this->addPrototype($prototype,
$options);
38 public function addPrototype($prototype, array
$options = array())
40 $tokens = token_get_all(
'<?php '.$prototype);
43 $this->prototypes[$prototype] = array($tokens,
$options);
46 public function load(ResourceInterface $resource)
48 if (!$nbProtos = count($this->prototypes)) {
49 throw new \LogicException(
'There are no prototypes registered.');
52 $buffers = array_fill(0, $nbProtos,
'');
53 $bufferLevels = array_fill(0, $nbProtos, 0);
54 $buffersInWildcard = array();
56 $tokens = token_get_all($resource->getContent());
59 while ($token = array_shift($tokens)) {
60 $current = self::tokenToString($token);
62 foreach (array_keys($this->prototypes) as $i) {
63 $prototype = & $this->prototypes[$i][0];
65 $buffer = & $buffers[$i];
66 $level = & $bufferLevels[$i];
68 if (isset($buffersInWildcard[$i])) {
70 case '(': ++$level;
break;
71 case ')': --$level;
break;
77 $calls[] = array($buffer.
';',
$options);
79 unset($buffersInWildcard[$i]);
81 } elseif ($current == self::tokenToString(current($prototype))) {
83 if (
'*' == self::tokenToString(next($prototype))) {
84 $buffersInWildcard[$i] =
true;
89 unset($buffersInWildcard[$i]);
96 foreach ($calls as $call) {
97 $formulae += call_user_func_array(array($this,
'processCall'), $call);
103 private function processCall($call, array $protoOptions = array())
106 file_put_contents($tmp, implode(
"\n", array(
108 $this->registerSetupCode(),
110 'echo serialize($_call);',
112 $args = unserialize(shell_exec(
'php '.escapeshellarg($tmp)));
115 $inputs = isset($args[0]) ? self::argumentToArray($args[0]) : array();
116 $filters = isset($args[1]) ? self::argumentToArray($args[1]) : array();
117 $options = isset($args[2]) ? $args[2] : array();
120 $options[
'debug'] = $this->factory->isDebug();
124 throw new \RuntimeException(
'The third argument must be omitted, null or an array.');
131 $options[
'name'] = $this->factory->generateAssetName($inputs, $filters,
$options);
142 abstract protected function registerPrototypes();
149 abstract protected function registerSetupCode();
151 protected static function tokenToString($token)
153 return is_array($token) ? $token[1] : $token;
156 protected static function argumentToArray($argument)
158 return is_array($argument) ? $argument : array_filter(array_map(
'trim', explode(
',', $argument)));
The asset factory creates asset objects.
static createTemporaryFile($prefix)
Creates a temporary file.
An exception for terminatinating execution or to throw for unit testing.
A resource is something formulae can be loaded from.
if(!is_array($argv)) $options