10000 [DI] Fix using private services in expressions · symfony/symfony@68744e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 68744e6

Browse files
[DI] Fix using private services in expressions
1 parent 945596b commit 68744e6

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1597,7 +1597,15 @@ private function getExpressionLanguage()
15971597
throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
15981598
}
15991599
$providers = $this->container->getExpressionLanguageProviders();
1600-
$this->expressionLanguage = new ExpressionLanguage(null, $providers);
1600+
$this->expressionLanguage = new ExpressionLanguage(null, $providers, function ($arg) {
1601+
$id = '""' === substr_replace($arg, '', 1, -1) ? stripcslashes(substr($arg, 1, -1)) : null;
1602+
1603+
if (null !== $id && ($this->container->hasAlias($id) || $this->container->hasDefinition($id))) {
1604+
return $this->getServiceCall($id);
1605+
}
1606+
1607+
return sprintf('$this->get(%s)', $arg);
1608+
});
16011609

16021610
if ($this->container->isTrackingResources()) {
16031611
foreach ($providers as $provider) {

src/Symfony/Component/DependencyInjection/ExpressionLanguage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ class ExpressionLanguage extends BaseExpressionLanguage
2525
/**
2626
* {@inheritdoc}
2727
*/
28-
public function __construct($cache = null, array $providers = array())
28+
public function __construct($cache = null, array $providers = array(), callable $serviceCompiler = null)
2929
{
3030
// prepend the default provider to let users override it easily
31-
array_unshift($providers, new ExpressionLanguageProvider());
31+
array_unshift($providers, new ExpressionLanguageProvider($serviceCompiler));
3232

3333
parent::__construct($cache, $providers);
3434
}

src/Symfony/Component/DependencyInjection/ExpressionLanguageProvider.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@
2424
*/
2525
class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
2626
{
27+
private $serviceCompiler;
28+
29+
public function __construct(callable $serviceCompiler = null)
30+
{
31+
$this->serviceCompiler = $serviceCompiler;
32+
}
33+
2734
public function getFunctions()
2835
{
2936
return array(
30-
new ExpressionFunction('service', function ($arg) {
37+
new ExpressionFunction('service', $this->serviceCompiler ?: function ($arg) {
3138
return sprintf('$this->get(%s)', $arg);
3239
}, function (array $variables, $value) {
3340
return $variables['container']->get($value);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ protected function getMethodCall1Service()
306306
if ($this->has('foobaz')) {
307307
$instance->setBar($this->get('foobaz', ContainerInterface::NULL_ON_INVALID_REFERENCE));
308308
}
309-
$instance->setBar(($this->get("foo")->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
309+
$instance->setBar(($this->get('foo')->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
310310

311311
return $instance;
312312
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ protected function getMethodCall1Service()
299299

300300
$instance->setBar($this->get('foo'));
301301
$instance->setBar(NULL);
302-
$instance->setBar(($this->get("foo")->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
302+
$instance->setBar(($this->get('foo')->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
303303

304304
return $instance;
305305
}

0 commit comments

Comments
 (0)
0