-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Add ServiceLocatorArgument to generate array-based locators optimized for OPcache shared memory #27783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DI] Add ServiceLocatorArgument to generate array-based locators optimized for OPcache shared memory #27783
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Argument; | ||
|
||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* @author Titouan Galopin <galopintitouan@gmail.com> | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
trait ReferenceSetArgumentTrait | ||
{ | ||
private $values; | ||
|
||
/** | ||
* @param Reference[] $values | ||
*/ | ||
public function __construct(array $values) | ||
{ | ||
$this->setValues($values); | ||
} | ||
|
||
/** | ||
* @return Reference[] The values in the set | ||
*/ | ||
public function getValues() | ||
{ | ||
return $this->values; | ||
} | ||
|
||
/** | ||
* @param Reference[] $values The service references to put in the set | ||
*/ | ||
public function setValues(array $values) | ||
{ | ||
foreach ($values as $k => $v) { | ||
if (null !== $v && !$v instanceof Reference) { | ||
throw new InvalidArgumentException(sprintf('A %s must hold only Reference instances, "%s" given.', __CLASS__, is_object($v) ? get_class($v) : gettype($v))); | ||
} | ||
} | ||
|
||
$this->values = $values; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Argument; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just asking, should it really be in this namespace? Seems a bit confusing to me. Can't we just keep it in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. like RewindableIterator: this is internal class, I don't think it should be at the root of the component There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. But this isn't an argument in the sense of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still not convinced this deserves more. Not all classes in a sub-namespace have to implement the same interface. Here, if there is a rule, it's that all classes that have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem, just random thoughts. |
||
|
||
use Symfony\Component\DependencyInjection\ServiceLocator as BaseServiceLocator; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
* | ||
* @internal | ||
*/ | ||
class ServiceLocator extends BaseServiceLocator | ||
{ | ||
private $factory; | ||
private $serviceMap; | ||
|
||
public function __construct(\Closure $factory, array $serviceMap) | ||
{ | ||
$this->factory = $factory; | ||
$this->serviceMap = $serviceMap; | ||
parent::__construct($serviceMap); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get($id) | ||
{ | ||
return isset($this->serviceMap[$id]) ? ($this->factory)(...$this->serviceMap[$id]) : parent::get($id); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Argument; | ||
|
||
/** | ||
* Represents a closure acting as a service locator. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class ServiceLocatorArgument implements ArgumentInterface | ||
{ | ||
use ReferenceSetArgumentTrait; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is an example showing easier definition of service locators.