Skip to content

Commit b8196fd

Browse files
committed
Allow to customize helpers
1 parent 6a48b76 commit b8196fd

File tree

5 files changed

+135
-4
lines changed

5 files changed

+135
-4
lines changed

src/JsPhpize/Compiler/Compiler.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,19 @@ public function compileDependencies($dependencies)
7979
$varPrefix = $this->varPrefix;
8080

8181
return implode('', array_map(function ($name) use ($varPrefix) {
82+
$code = $name;
83+
$file = $name;
84+
85+
if (preg_match('/^[a-z0-9_-]+$/', $file)) {
86+
$file = __DIR__ . '/Helpers/' . ucfirst($name) . '.h';
87+
}
88+
89+
if (file_exists($file)) {
90+
$code = file_get_contents($file);
91+
}
92+
8293
return '$GLOBALS[\'' . $varPrefix . $name . '\'] = ' .
83-
trim(file_get_contents(__DIR__ . '/Helpers/' . ucfirst($name) . '.h')) .
94+
trim($code) .
8495
";\n";
8596
}, $dependencies));
8697
}
@@ -159,7 +170,9 @@ protected function visitDyiade(Dyiade $dyiade, $indent)
159170
$arguments[] = $this->visitNode($dyiade->rightHand, $indent);
160171
}
161172

162-
return $this->helperWrap('plus', $arguments);
173+
$plus = $this->engine->getHelperName('plus');
174+
175+
return $this->helperWrap($plus, $arguments);
163176
}
164177

165178
return $leftHand . ' ' . $dyiade->operator . ' ' . $rightHand;
@@ -292,7 +305,8 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent)
292305
if (count($functionCall->children)) {
293306
$arguments = $this->mapNodesArray($functionCall->children, $indent);
294307
array_unshift($arguments, $dynamicCall);
295-
$dynamicCall = $this->helperWrap('dot', $arguments);
308+
$dot = $this->engine->getHelperName('dot');
309+
$dynamicCall = $this->helperWrap($dot, $arguments);
296310
}
297311

298312
return $dynamicCall;
@@ -357,7 +371,8 @@ protected function visitVariable(Variable $variable, $indent)
357371
if (count($variable->children)) {
358372
$arguments = $this->mapNodesArray($variable->children, $indent);
359373
array_unshift($arguments, $php);
360-
$php = $this->helperWrap('dot', $arguments);
374+
$dot = $this->engine->getHelperName('dot');
375+
$php = $this->helperWrap($dot, $arguments);
361376
}
362377

363378
return $php;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
function ($base) {
2+
$getFromArray = function ($base, $key) {
3+
return isset($base[$key])
4+
? $base[$key]
5+
: null;
6+
};
7+
$getCallable = function ($base, $key) use ($getFromArray) {
8+
if (is_callable(array($base, $key))) {
9+
return array($base, $key);
10+
}
11+
if ($base instanceof \ArrayAccess) {
12+
return $getFromArray($base, $key);
13+
}
14+
};
15+
$fallbackDot = function ($base, $key) use ($getCallable) {
16+
if (is_string($base)) {
17+
if ($key === 'substr' || $key === 'slice') {
18+
return function ($start, $length = null) use ($base) {
19+
return func_num_args() === 1 ? substr($base, $start) : substr($base, $start, $length);
20+
};
21+
}
22+
if ($key === 'charAt') {
23+
return function ($pos) use ($base) {
24+
return substr($base, $pos, 1);
25+
};
26+
}
27+
if ($key === 'indexOf') {
28+
return function ($needle) use ($base) {
29+
$pos = strpos($base, $needle);
30+
31+
return $pos === false ? -1 : $pos;
32+
};
33+
}
34+
if ($key === 'toUpperCase') {
35+
return function () use ($base) {
36+
return strtoupper($base);
37+
};
38+
}
39+
if ($key === 'toLowerCase') {
40+
return function () use ($base) {
41+
return strtolower($base);
42+
};
43+
}
44+
}
45+
46+
return $getCallable($base, $key);
47+
};
48+
foreach (array_slice(func_get_args(), 1) as $key) {
49+
$base = is_array($base)
50+
? $getFromArray($base, $key)
51+
: (is_object($base)
52+
? (method_exists($base, $method = "get" . ucfirst($key))
53+
? $base->$method()
54+
: (method_exists($base, $key)
55+
? array($base, $key)
56+
: $base->$key
57+
)
58+
)
59+
: $fallbackDot($base, $key)
60+
);
61+
}
62+
63+
return $base;
64+
}

src/JsPhpize/JsPhpizeOptions.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ public function getVarPrefix()
100100
return $this->getOption('varPrefix', static::VAR_PREFIX);
101101
}
102102

103+
/**
104+
* Retrieve the prefix of specific variables.
105+
*
106+
* @return string
107+
*/
108+
public function getHelperName($key)
109+
{
110+
$helpers = $this->getOption('helpers', []);
111+
112+
return is_array($helpers) && isset($helpers[$key])
113+
? $helpers[$key]
114+
: $key;
115+
}
116+
117+
103118
/**
104119
* Retrieve the prefix of specific constants.
105120
*

tests/Plus.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function ($base) {
2+
foreach (array_slice(func_get_args(), 1) as $value) {
3+
$base = $base * $value;
4+
}
5+
6+
return $base;
7+
}

tests/dotHelper.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,34 @@ public function testArrayAccess()
163163
$this->assertSame('bar', $dotHelper($object, 'foo'));
164164
$this->assertSame(null, $dotHelper($object, 'biz'));
165165
}
166+
167+
public function testCustomHelper()
168+
{
169+
$plusHelper = 'function ($base) {
170+
foreach (array_slice(func_get_args(), 1) as $value) {
171+
$base = $base * $value;
172+
}
173+
174+
return $base;
175+
}';
176+
$jsPhpize = new JsPhpize(array(
177+
'helpers' => array(
178+
'plus' => $plusHelper,
179+
),
180+
'returnLastStatement' => true,
181+
));
182+
183+
$this->assertEquals(18, $jsPhpize->render('3 + 6'));
184+
185+
$jsPhpize = new JsPhpize(array(
186+
'helpers' => array(
187+
'dot' => 'plus',
188+
),
189+
'returnLastStatement' => true,
190+
));
191+
192+
$this->assertEquals('xb', $jsPhpize->render('a.b', array(
193+
'a' => 'x',
194+
)));
195+
}
166196
}

0 commit comments

Comments
 (0)