8000 [DependencyInjection] made a speed optimization · Dahipster/symfony@c9d0a73 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9d0a73

Browse files
committed
[DependencyInjection] made a speed optimization
1 parent ee3e298 commit c9d0a73

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed

src/Symfony/Components/DependencyInjection/Builder.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ public function setAlias($alias, $id)
174174
$this->aliases[$alias] = $id;
175175
}
176176

177+
/**
178+
* Returns true if an alias exists under the given identifier.
179+
*
180+
* @param string $id The service identifier
181+
*
182+
* @return Boolean true if the alias exists, false otherwise
183+
*/
184+
public function hasAlias($id)
185+
{
186+
return array_key_exists($id, $this->aliases);
187+
}
188+
177189
/**
178190
* Gets all defined aliases.
179191
*
@@ -184,6 +196,25 @@ public function getAliases()
184196
return $this->aliases;
185197
}
186198

199+
/**
200+
* Gets an alias.
201+
*
202+
* @param string $id The service identifier
203+
*
204+
* @return string The aliased service identifier
205+
*
206+
* @throws \InvalidArgumentException if the alias does not exist
207+
*/
208+
public function getAlias($id)
209+
{
210+
if (!$this->hasAlias($id))
211+
{
212+
throw new \InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
213+
}
214+
215+
return $this->aliases[$id];
216+
}
217+
187218
/**
188219
* Registers a service definition.
189220
*

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ protected function dumpValue($value)
442442

443443
protected function getServiceCall($id, Reference $reference = null)
444444
{
445-
if ('service_container' == $id)
445+
if ('service_container' === $id)
446446
{
447447
return '$this';
448448
}
@@ -453,7 +453,14 @@ protected function getServiceCall($id, Reference $reference = null)
453453
}
454454
else
455455
{
456-
return sprintf('$this->getService(\'%s\')', $id);
456+
if ($this->container->hasDefinition($id) || $this->container->hasAlias($id))
457+
{
458+
return sprintf('$this->get%sService()', Container::camelize($id));
459+
}
460+
else
461+
{
462+
return sprintf('$this->getService(\'%s\')', $id);
463+
}
457464
}
458465
}
459466
}

tests/fixtures/Symfony/Components/DependencyInjection/php/services9.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function getFooService()
3838
{
3939
require_once '%path%/foo.php';
4040

41-
$instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $this->getService('foo.baz'), array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo'), 'bar' => $this->getParameter('foo')), true, $this);
41+
$instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $this->getFoo_BazService(), array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo'), 'bar' => $this->getParameter('foo')), true, $this);
4242
$instance->setBar('bar');
4343
$instance->initialize();
4444
sc_configure($instance);
@@ -58,8 +58,8 @@ protected function getBarService()
5858
{
5959
if (isset($this->shared['bar'])) return $this->shared['bar'];
6060

61-
$instance = new FooClass('foo', $this->getService('foo.baz'), $this->getParameter('foo_bar'));
62-
$this->getService('foo.baz')->configure($instance);
61+
$instance = new FooClass('foo', $this->getFoo_BazService(), $this->getParameter('foo_bar'));
62+
$this->getFoo_BazService()->configure($instance);
6363

6464
return $this->shared['bar'] = $instance;
6565
}
@@ -113,7 +113,7 @@ protected function getMethodCall1Service()
113113
if (isset($this->shared['method_call1'])) return $this->shared['method_call1'];
114114

115115
$instance = new FooClass();
116-
$instance->setBar($this->getService('foo'));
116+
$instance->setBar($this->getFooService());
117117
$instance->setBar($this->getService('foo', Container::NULL_ON_INVALID_REFERENCE));
118118
if ($this->hasService('foo'))
119119
{
@@ -134,7 +134,7 @@ protected function getMethodCall1Service()
134134
*/
135135
protected function getAliasForFooService()
136136
{
137-
return $this->getService('foo');
137+
return $this->getFooService();
138138
}
139139

140140
/**

tests/unit/Symfony/Components/DependencyInjection/BuilderTest.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
$fixturesPath = __DIR__.'/../../../../fixtures/Symfony/Components/DependencyInjection/';
1919

20-
$t = new LimeTest(55);
20+
$t = new LimeTest(59);
2121

2222
// ->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition()
2323
$t->diag('->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition()');
@@ -89,7 +89,7 @@
8989
@$builder->getService('baz');
9090
$t->fail('->getService() throws a LogicException if the service has a circular reference to itself');
9191
}
92-
catch (LogicException $e)
92+
catch (\LogicException $e)
9393
{
9494
$t->pass('->getService() throws a LogicException if the service has a circular reference to itself');
9595
}
@@ -105,14 +105,27 @@
105105
$builder->register('bar', 'stdClass');
106106
$t->is($builder->getServiceIds(), array('foo', 'bar', 'service_container'), '->getServiceIds() returns all defined service ids');
107107

108-
// ->setAlias()
109-
$t->diag('->setAlias()');
108+
// ->setAlias() ->getAlias() ->hasAlias()
109+
$t->diag('->setAlias() ->getAlias() ->hasAlias()');
110110
$builder = new Builder();
111111
$builder->register('foo', 'stdClass');
112112
$builder->setAlias('bar', 'foo');
113+
$t->ok($builder->hasAlias('bar'), '->hasAlias() returns true if the alias exists');
114+
$t->ok(!$builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
115+
$t->is($builder->getAlias('bar'), 'foo', '->getAlias() returns the aliased service');
113116
$t->ok($builder->hasService('bar'), '->setAlias() defines a new service');
114117
$t->ok($builder->getService('bar') === $builder->getService('foo'), '->setAlias() creates a service that is an alias to another one');
115118

119+
try
120+
{
121+
$builder->getAlias('foobar');
122+
$t->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist');
123+
}
124+
catch (\InvalidArgumentException $e)
125+
{
126+
$t->pass('->getAlias() throws an InvalidArgumentException if the alias does not exist');
127+
}
128+
116129
// ->getAliases()
117130
$t->diag('->getAliases()');
118131
$builder = new Builder();
@@ -186,7 +199,7 @@
186199
$builder->getService('foo4');
187200
$t->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
188201
}
189-
catch (InvalidArgumentException $e)
202+
catch (\InvalidArgumentException $e)
190203
{
191204
$t->pass('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
192205
}

0 commit comments

Comments
 (0)
0