This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
generated from spaceonfire/skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from spaceonfire/rename-chain-to-composite
Rename chain to composite
- Loading branch information
Showing
10 changed files
with
268 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ on: | |
pull_request: | ||
branches: | ||
- master | ||
workflow_dispatch: | ||
|
||
jobs: | ||
composer: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,7 @@ | |
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "2.1-dev" | ||
"dev-master": "2.2-dev" | ||
} | ||
}, | ||
"config": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spaceonfire\Container; | ||
|
||
use Psr\Container\ContainerInterface as PsrContainerInterface; | ||
use spaceonfire\Collection\Collection; | ||
use spaceonfire\Collection\CollectionInterface; | ||
use spaceonfire\Container\Definition\DefinitionInterface; | ||
use spaceonfire\Container\Exception\ContainerException; | ||
use spaceonfire\Container\Exception\NotFoundException; | ||
|
||
/** | ||
* Class CompositeContainer | ||
* | ||
* Attention: You should not extend this class because it will become final in the next major release | ||
* after the backward compatibility aliases are removed. | ||
* | ||
* @package spaceonfire\Container | ||
* @final | ||
*/ | ||
class CompositeContainer implements ContainerWithServiceProvidersInterface, ContainerAwareInterface | ||
{ | ||
use ContainerAwareTrait; | ||
|
||
/** | ||
* @var PsrContainerInterface[] | ||
*/ | ||
private $containers = []; | ||
/** | ||
* @var ContainerInterface|null | ||
*/ | ||
private $primary; | ||
|
||
/** | ||
* CompositeContainer constructor. | ||
* @param PsrContainerInterface[] $containers | ||
*/ | ||
public function __construct(iterable $containers) | ||
{ | ||
$this->setContainer($this); | ||
$this->addContainers($containers); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function setContainer(ContainerInterface $container): ContainerAwareInterface | ||
{ | ||
$this->container = $container; | ||
foreach ($this->containers as $delegate) { | ||
if ($delegate instanceof ContainerAwareInterface) { | ||
$delegate->setContainer($container); | ||
} | ||
} | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Getter for `primary` property | ||
* @return ContainerInterface | ||
*/ | ||
private function getPrimary(): ContainerInterface | ||
{ | ||
if ($this->primary === null) { | ||
throw new ContainerException('No primary container provided with support of definitions'); | ||
} | ||
|
||
return $this->primary; | ||
} | ||
|
||
/** | ||
* Setter for `primary` property | ||
* @param ContainerInterface $primary | ||
*/ | ||
private function setPrimary(ContainerInterface $primary): void | ||
{ | ||
if ( | ||
$this->primary === null || | ||
( | ||
!$this->primary instanceof ContainerWithServiceProvidersInterface && | ||
$primary instanceof ContainerWithServiceProvidersInterface | ||
) | ||
) { | ||
$this->primary = $primary; | ||
} | ||
} | ||
|
||
/** | ||
* Add containers to the chain | ||
* @param PsrContainerInterface[] $containers | ||
* @return $this | ||
*/ | ||
public function addContainers(iterable $containers): self | ||
{ | ||
foreach ($containers as $container) { | ||
$this->addContainer($container); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* Add container to the chain | ||
* @param PsrContainerInterface $container | ||
* @return $this | ||
*/ | ||
public function addContainer(PsrContainerInterface $container): self | ||
{ | ||
if ($container instanceof ContainerInterface) { | ||
$this->setPrimary($container); | ||
} | ||
|
||
if ($container instanceof ContainerAwareInterface) { | ||
$container->setContainer($this->getContainer()); | ||
} | ||
|
||
$this->containers[] = $container; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function get($id, array $arguments = []) | ||
{ | ||
foreach ($this->containers as $container) { | ||
if ($container->has($id)) { | ||
return $container instanceof ContainerInterface | ||
? $container->get($id, $arguments) | ||
: $container->get($id); | ||
} | ||
} | ||
|
||
throw new NotFoundException(sprintf('Alias (%s) is not being managed by any container in chain', $id)); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function has($id): bool | ||
{ | ||
foreach ($this->containers as $container) { | ||
if ($container->has($id)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function make(string $alias, array $arguments = []) | ||
{ | ||
return $this->getPrimary()->make($alias, $arguments); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function invoke(callable $callable, array $arguments = []) | ||
{ | ||
return $this->getPrimary()->invoke($callable, $arguments); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function add(string $id, $concrete = null, bool $shared = false): DefinitionInterface | ||
{ | ||
return $this->getPrimary()->add($id, $concrete, $shared); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function share(string $id, $concrete = null): DefinitionInterface | ||
{ | ||
return $this->getPrimary()->share($id, $concrete); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function addServiceProvider($provider): ContainerWithServiceProvidersInterface | ||
{ | ||
$primary = $this->getPrimary(); | ||
|
||
if ($primary instanceof ContainerWithServiceProvidersInterface) { | ||
$primary->addServiceProvider($provider); | ||
return $this; | ||
} | ||
|
||
throw new ContainerException('No container provided with support of service providers'); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function hasTagged(string $tag): bool | ||
{ | ||
foreach ($this->containers as $container) { | ||
if ($container instanceof ContainerInterface && $container->hasTagged($tag)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getTagged(string $tag): CollectionInterface | ||
{ | ||
$result = new Collection(); | ||
|
||
foreach ($this->containers as $container) { | ||
if (!$container instanceof ContainerInterface) { | ||
continue; | ||
} | ||
|
||
$result = $result->merge($container->getTagged($tag)); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
Oops, something went wrong.