Skip to content

Commit

Permalink
Merge pull request #10 from ClanCats/develop
Browse files Browse the repository at this point in the history
v1.3.3
  • Loading branch information
mario-deluna authored Apr 17, 2023
2 parents 279982c + a4b6a46 commit ca85b0c
Show file tree
Hide file tree
Showing 54 changed files with 276 additions and 100 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

A PHP Service Container featuring a simple meta-language with fast and compilable dependency injection.

[![Build Status](https://travis-ci.org/ClanCats/Container.svg?branch=master)](https://travis-ci.org/ClanCats/Container)
[![PHPUnit](https://github.com/ClanCats/Container/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/ClanCats/Container/actions/workflows/phpunit.yml)
[![PHPStan](https://github.com/ClanCats/Container/actions/workflows/phpstan.yml/badge.svg)](https://github.com/ClanCats/Container/actions/workflows/phpstan.yml)
[![Packagist](https://img.shields.io/packagist/dt/clancats/container.svg)](https://packagist.org/packages/clancats/container)
[![Packagist](https://img.shields.io/packagist/l/clancats/container.svg)](https://github.com/ClanCats/Container/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/clancats/container.svg)](https://github.com/ClanCats/Container/releases)
Expand Down
2 changes: 1 addition & 1 deletion src/ComposerContainerFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container;
Expand Down
87 changes: 79 additions & 8 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container;
Expand Down Expand Up @@ -459,6 +459,77 @@ public function get(string $serviceName)
}
}

/**
* Returns the class name for a given service name.
* The goal of this function is to retrieve the class name without actually loading the service.
* This function heavly relies on reflection in cached containers. So keep that in mind.
*
* **Warning:**
* This is not always possible and will return `null` if the class name cannot be determined.
* For example if the service resolver is a closure there is no way to determine the class name
* without actually loading the service.
*
* This won't throw an exception if the service is not found and simply returns null.
*
* @param string $serviceName
* @return null|class-string
*/
public function getClassForService(string $serviceName): ?string
{
if (!isset($this->serviceResolverType[$serviceName])) {
return null;
}

// if the service has already been resolved, we can simply return the class name
if (isset($this->resolvedSharedServices[$serviceName])) {
return get_class($this->resolvedSharedServices[$serviceName]) ?: null;
}

$serviceResolverType = $this->serviceResolverType[$serviceName];

// if we have a method resolver we have to rely on reflection to get the class name
if ($serviceResolverType === static::RESOLVE_METHOD)
{
$reflection = new \ReflectionMethod($this, $this->resolverMethods[$serviceName]);
$returnType = $reflection->getReturnType();
if (!$returnType instanceof \ReflectionNamedType) {
return null;
}

// if its a builtin type we cannot determine the class name
if ($returnType->isBuiltin()) {
return null;
}

return $returnType->getName(); // @phpstan-ignore-line
}
// if the services relies on a different service provider it needs to explicity
// support the class name lookup interface for this to work.
elseif ($serviceResolverType === static::RESOLVE_PROVIDER) {
$provider = $this->serviceProviders[$serviceName];

if ($provider instanceof ServiceProviderClassLookupInterface) {
return $provider->lookupClassName($serviceName, $this);
}
}
// handles services definition based resolvers
// if the resolver is not a "ServiceDefinitionInterface" we cannot determine the class name
else if ($serviceResolverType === static::RESOLVE_FACTORY || $serviceResolverType === static::RESOLVE_SHARED) {
$factory = $this->resolverFactories[$serviceName];

if ($factory instanceof ServiceDefinitionInterface) {
return $factory->getClassName();
}
}

// if its an alias, we just forward the call to the aliased service
elseif ($serviceResolverType === static::RESOLVE_ALIAS) {
return $this->getClassForService($this->serviceAliases[$serviceName]);
}

return null;
}

/**
* Resolve a service instance from the given factory object.
*
Expand Down Expand Up @@ -552,16 +623,16 @@ public function register(ServiceProviderInterface $provider)
* $container->bind('router', '\\Routing\\Router')
* ->addDependencyArgument('config');
*
* @param string $name The service name.
* @param mixed $factory The service factory instance, the closure or the classname as string
* @param bool $shared Should the service be shared inside the container.
* @param string $name The service name.
* @param mixed|class-string $factory The service factory instance, the closure or the classname as string
* @param bool $shared Should the service be shared inside the container.
*
* @return ServiceFactory|void The given or generated service factory.
*/
public function bind(string $name, $factory, bool $shared = true)
{
if (is_string($factory)) {
return $this->bindClass($name, $factory, [], $shared);
return $this->bindClass($name, $factory, [], $shared); // @phpstan-ignore-line
} elseif ($shared) {
$this->bindFactoryShared($name, $factory);
} else {
Expand All @@ -573,15 +644,15 @@ public function bind(string $name, $factory, bool $shared = true)
* Creates and binds a service factory by class name and arguments.
*
* @param string $name The service name.
* @param string $factory The service class name.
* @param class-string $className The service class name.
* @param array<mixed> $arguments An array of arguments.
* @param bool $shared Should the service be shared inside the container.
*
* @return ServiceFactory The created service factory
*/
public function bindClass(string $name, string $factory, array $arguments = [], bool $shared = true) : ServiceFactory
public function bindClass(string $name, string $className, array $arguments = [], bool $shared = true) : ServiceFactory
{
$factory = new ServiceFactory($factory, $arguments);
$factory = new ServiceFactory($className, $arguments);

if ($shared) {
$this->bindFactoryShared($name, $factory);
Expand Down
4 changes: 2 additions & 2 deletions src/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container;
Expand Down Expand Up @@ -168,7 +168,7 @@ public function getSharedNames() : array
* Add a service by string and arguments array.
*
* @param string $serviceName
* @param string $serviceClass
* @param class-string $serviceClass
* @param array<mixed> $serviceArguments
* @param bool $isShared
* @return ServiceDefinition
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/ContainerInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/ContainerLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/ContainerParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/ArgumentArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/ArrayElementNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/ArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/AssignableNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/ConstructionActionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/MetaDataAssignmentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/ParameterDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/ParameterReferenceNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/ScopeImportNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/ScopeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
12 changes: 7 additions & 5 deletions src/ContainerParser/Nodes/ServiceDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand All @@ -29,7 +29,7 @@ class ServiceDefinitionNode extends BaseNode
/**
* The services class name
*
* @var string
* @var class-string
*/
protected $className;

Expand Down Expand Up @@ -85,8 +85,8 @@ class ServiceDefinitionNode extends BaseNode
/**
* Service definition constructor
*
* @param string $name
* @param string $className
* @param string $name
* @param class-string $className
*/
public function __construct(string $name = null, string $className = null)
{
Expand Down Expand Up @@ -118,7 +118,7 @@ public function setName(string $name)
/**
* Get the services class name
*
* @return string
* @return class-string
*/
public function getClassName() : string
{
Expand All @@ -127,6 +127,8 @@ public function getClassName() : string

/**
* Set the services class name
*
* @param class-string $className
*/
public function setClassName(string $className): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/ServiceMethodCallNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/ServiceReferenceNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Nodes/ValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Nodes;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Parser/ArgumentArrayParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Parser;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerParser/Parser/ArrayParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2022 Mario Döring
* @copyright Copyright (c) 2016-2023 Mario Döring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container\ContainerParser\Parser;
Expand Down
Loading

0 comments on commit ca85b0c

Please sign in to comment.