Skip to content
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

[READ ONLY] Ovos Mod #14

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
f20fd0b
Use sourceCache if isSupported check passes, otherwise proceed withou…
falkenhawk Apr 25, 2019
f0a109b
DefinitionGlob to specify a pattern for definition files
falkenhawk Apr 25, 2019
d494750
Prefer composition over inheritance
falkenhawk Apr 26, 2019
ddf0cbd
Optimize compiled string definitions
falkenhawk Apr 27, 2019
4f3fc0d
Optimize compiled factory definitions
falkenhawk Apr 28, 2019
59a2216
AnnotationBasedAutowiring with additional options for performance tweaks
falkenhawk Apr 26, 2019
1cce07a
ServiceLocator
falkenhawk Apr 29, 2019
99305d2
Discard ServiceLocatorRepository::injectOn() method
falkenhawk Apr 30, 2019
7a71a1f
ServiceLocator - rename entry to subscriber, cleanup
falkenhawk May 30, 2019
3938947
ServiceLocatorRepository - replace setService with override method
falkenhawk May 30, 2019
1aa1834
Reference and ServiceLocatorDefinition cleanup
falkenhawk May 30, 2019
d8ea15e
compiler - extract resolveServiceLocator method to CompiledContainer
falkenhawk May 30, 2019
0d1265d
tests for service locators
falkenhawk May 30, 2019
2a10132
code formatting
falkenhawk May 30, 2019
ffad7c9
Merge pull request #2 from ovos/feature/definition-glob
falkenhawk May 30, 2019
851c7c5
Merge pull request #3 from ovos/feature/loosen-apcu-check
falkenhawk May 30, 2019
82af93e
Merge pull request #5 from ovos/feature/compiler-optimize-string-defi…
falkenhawk May 30, 2019
d314100
Merge pull request #6 from ovos/feature/compiler-optimize-factories
falkenhawk May 30, 2019
f2709cd
Merge pull request #4 from ovos/feature/annotations-autowiring-options
falkenhawk May 30, 2019
0fe5cce
Merge pull request #7 from ovos/feature/service-locator
falkenhawk May 30, 2019
4aee110
Merge pull request #7 from ovos/feature/service-locator
falkenhawk May 30, 2019
3fef38a
change package name in composer.json
falkenhawk May 30, 2019
661e59b
Merge branch 'master' into mod
falkenhawk Nov 29, 2019
2466176
Merge pull request #11 from ovos/feature/sourcecache-namespaced
falkenhawk Dec 12, 2019
d7d99f6
Merge pull request #12 from ovos/feature/travis-enable-apcu
falkenhawk Dec 12, 2019
c1a4f7d
Merge branch 'refs/heads/master' into mod
falkenhawk Dec 12, 2019
aa38cca
Merge remote-tracking branch 'ovos/master' into feature/definition-glob
partikus Jan 20, 2021
7b4540a
fix: bump phpunit assertions
partikus Jan 20, 2021
720036d
Merge remote-tracking branch 'ovos/master' into feature/compiler-opti…
partikus Jan 20, 2021
d113eb7
Merge remote-tracking branch 'ovos/master' into feature/compiler-opti…
partikus Jan 20, 2021
4a570b7
adding leading backslash to all native constants
partikus Jan 20, 2021
87b53f1
add psr container exception to the ignored errors
partikus Jan 21, 2021
d474395
csfix
partikus Jan 21, 2021
5219321
Merge branch 'master' into feature/annotations-autowiring-options-wsl
partikus Jan 21, 2021
239a39f
get rid of using deprecated expectException annotations
partikus Jan 21, 2021
80d5ae3
fix a missing default type for Reference::
partikus Jan 21, 2021
33059cf
csfix
partikus Jan 21, 2021
3ab1aab
Merge branch 'feature/annotations-autowiring-options-wsl' into mod
partikus Jan 21, 2021
08bd184
Merge branch 'feature/compiler-optimize-factories' into mod
partikus Jan 21, 2021
25fae83
Merge branch 'feature/compiler-optimize-string-definitions' into mod
partikus Jan 21, 2021
395001c
Merge branch 'feature/definition-glob' into mod
partikus Jan 21, 2021
c119aec
Merge branch 'feature/csfix' into mod
partikus Jan 21, 2021
ce95893
Merge branch 'feature/phpstan-ignore-psr-container-exception' into mod
partikus Jan 21, 2021
2f44a4c
csfix
partikus Jan 21, 2021
a255d11
Merge branch 'feature/annotations-autowiring-options-wsl' into mod
partikus Jan 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ServiceLocatorRepository - replace setService with override method
- the method may be used only before a locator instance is created - to avoid ambiguous situations
- create method refactored - a locator instance will be returned if already created, only if passed services match, otherwise an exception will be thrown
  • Loading branch information
falkenhawk committed May 30, 2019
commit 3938947ed8893f1d8f8e69792f92bcdeb0681304
55 changes: 41 additions & 14 deletions src/ServiceLocatorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ class ServiceLocatorRepository implements ContainerInterface
/**
* @var ServiceLocator[]
*/
protected $locators = [];
private $locators = [];

/**
* Overrides for ServiceLocators
* @var array
*/
private $overrides = [];

/**
* @var ContainerInterface
*/
protected $container;
private $container;

/**
* Constructor.
Expand All @@ -32,35 +38,56 @@ public function __construct(ContainerInterface $container)
*
* @param string $entry
* @param array $services
* @param bool $overwrite if service locator for an entry already exists, should its services be overwritten?
* @return ServiceLocator
*/
public function create(string $entry, array $services = [], $overwrite = false) : ServiceLocator
public function create(string $entry, array $services = []) : ServiceLocator
{
if (isset($this->locators[$entry]) && !$overwrite) {
$services = $overwrite
? array_merge($this->locators[$entry]->getServices(), $services)
: array_merge($services, $this->locators[$entry]->getServices());
if (isset($this->overrides[$entry])) {
$services = array_merge($services, $this->overrides[$entry]);
}
if (!isset($this->locators[$entry])) {
$this->locators[$entry] = new ServiceLocator($this->container, $services, $entry);
} else {
// the service locator cannot be re-created - the existing locator may be returned only if expected services are identical
// compare passed services and those in the already created ServiceLocator
$locatorServices = $this->locators[$entry]->getServices();
foreach ($services as $key => $value) {
if (is_numeric($key)) {
$key = $value;
}
if (!array_key_exists($key, $locatorServices) || $locatorServices[$key] !== $value) {
throw new \LogicException(sprintf(
"ServiceLocator for '%s' cannot be recreated with different services.",
$entry
));
}
}
}

$this->locators[$entry] = new ServiceLocator($this->container, $services, $entry);

return $this->locators[$entry];
}

/**
* Modify a single entry for a service locator.
* Override a single service for a service locator.
* This can be only used before the service locator for the given entry is created.
*
* @param string $entry
* @param string $serviceId
* @param string|null $serviceEntry
* @return $this
*/
public function setService(string $entry, string $serviceId, string $serviceEntry = null)
public function override(string $entry, string $serviceId, string $serviceEntry = null)
{
$serviceEntry = $serviceEntry ?? $serviceId;
$this->create($entry, [$serviceId => $serviceEntry], true);
if (isset($this->locators[$entry])) {
throw new \LogicException(sprintf(
"Service '%s' for '%s' cannot be overridden - ServiceLocator is already created.",
$serviceId,
$entry
));
}

$serviceEntry = $serviceEntry ?? $serviceId;
$this->overrides[$entry][$serviceId] = $serviceEntry;
return $this;
}

Expand Down