Replaced the rest of the constructor injection references

This commit is contained in:
Christopher Allford 2020-08-05 21:28:06 -07:00
parent d178c7ff01
commit cbbfed4989
7 changed files with 39 additions and 33 deletions

View File

@ -16,14 +16,14 @@ use League\Container\Definition\DefinitionInterface;
* See the documentation of the original class this one is based on (https://container.thephpleague.com/3.x/service-providers)
* for basic usage details. What this class adds is:
*
* - The `add_with_auto_arguments` method that allows to register classes without having to specify the constructor arguments.
* - The `add_with_auto_arguments` method that allows to register classes without having to specify the injection method arguments.
* - The `share_with_auto_arguments` method, sibling of the above.
* - Convenience `add` and `share` methods that are just proxies for the same methods in `$this->getContainer()`.
*/
abstract class AbstractServiceProvider extends \League\Container\ServiceProvider\AbstractServiceProvider {
/**
* Register a class in the container and use reflection to guess the constructor arguments.
* Register a class in the container and use reflection to guess the injection method arguments.
*
* WARNING: this method uses reflection, so please have performance in mind when using it.
*
@ -33,7 +33,7 @@ abstract class AbstractServiceProvider extends \League\Container\ServiceProvider
*
* @return DefinitionInterface The generated container definition.
*
* @throws ContainerException Error when reflecting the class, or class constructor is not public, or an argument has no valid type hint.
* @throws ContainerException Error when reflecting the class, or class injection method is not public, or an argument has no valid type hint.
*/
protected function add_with_auto_arguments( string $class_name, $concrete = null, bool $shared = false ) : DefinitionInterface {
$definition = new Definition( $class_name, $concrete );
@ -65,13 +65,13 @@ abstract class AbstractServiceProvider extends \League\Container\ServiceProvider
/**
* Check if a combination of class name and concrete is valid for registration.
* Also return the class constructor if the concrete is either a class name or null (then use the supplied class name).
* Also return the class injection method if the concrete is either a class name or null (then use the supplied class name).
*
* @param string $class_name The class name to check.
* @param mixed $concrete The concrete to check.
*
* @return \ReflectionFunctionAbstract|null A reflection instance for the $class_name constructor or $concrete constructor or callable; null otherwise.
* @throws ContainerException Class has a private constructor, can't reflect class, or the concrete is invalid.
* @return \ReflectionFunctionAbstract|null A reflection instance for the $class_name injection method or $concrete injection method or callable; null otherwise.
* @throws ContainerException Class has a private injection method, can't reflect class, or the concrete is invalid.
*/
private function reflect_class_or_callable( string $class_name, $concrete ) {
if ( ! isset( $concrete ) || is_string( $concrete ) && class_exists( $concrete ) ) {
@ -97,7 +97,7 @@ abstract class AbstractServiceProvider extends \League\Container\ServiceProvider
}
/**
* Register a class in the container and use reflection to guess the constructor arguments.
* Register a class in the container and use reflection to guess the injection method arguments.
* The class is registered as shared, so `get` on the container always returns the same instance.
*
* WARNING: this method uses reflection, so please have performance in mind when using it.
@ -107,7 +107,7 @@ abstract class AbstractServiceProvider extends \League\Container\ServiceProvider
*
* @return DefinitionInterface The generated container definition.
*
* @throws ContainerException Error when reflecting the class, or class constructor is not public, or an argument has no valid type hint.
* @throws ContainerException Error when reflecting the class, or class injection method is not public, or an argument has no valid type hint.
*/
protected function share_with_auto_arguments( string $class_name, $concrete = null ) : DefinitionInterface {
return $this->add_with_auto_arguments( $class_name, $concrete, true );

View File

@ -7,7 +7,9 @@
namespace Automattic\WooCommerce\Proxies;
use Automattic\WooCommerce\Internal\DependencyManagement\Definition;
use \Psr\Container\ContainerInterface as Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Proxy class to access legacy WooCommerce functionality.
@ -38,7 +40,10 @@ class LegacyProxy {
*/
public function get_instance_of( string $class_name, ...$args ) {
if ( false !== strpos( $class_name, '\\' ) ) {
throw new \Exception( 'The LegacyProxy class is not intended for getting instances of classes in the src directory, please use method injection or the instance of \\Psr\\Container\\ContainerInterface for that.' );
throw new \Exception(
'The LegacyProxy class is not intended for getting instances of classes in the src directory, please use ' .
Definition::INJECTION_METHOD . ' method injection or the instance of ' . ContainerInterface::class . ' for that.'
);
}
// If a class has a dedicated method to obtain a instance, use it.

View File

@ -11,10 +11,10 @@ use Automattic\WooCommerce\Internal\DependencyManagement\AbstractServiceProvider
use Automattic\WooCommerce\Internal\DependencyManagement\ContainerException;
use Automattic\WooCommerce\Internal\DependencyManagement\Definition;
use Automattic\WooCommerce\Internal\DependencyManagement\ExtendedContainer;
use Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses\ClassWithConstructorArgumentWithoutTypeHint;
use Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses\ClassWithInjectionMethodArgumentWithoutTypeHint;
use Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses\ClassWithDependencies;
use Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses\ClassWithPrivateConstructor;
use Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses\ClassWithScalarConstructorArgument;
use Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses\ClassWithPrivateInjectionMethod;
use Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses\ClassWithScalarInjectionMethodArgument;
use Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses\DependencyClass;
use League\Container\Definition\DefinitionInterface;
@ -91,23 +91,23 @@ class AbstractServiceProviderTest extends \WC_Unit_Test_Case {
}
/**
* @testdox 'add_with_auto_arguments' should throw an exception if the passed class has a private method.
* @testdox 'add_with_auto_arguments' should throw an exception if the passed class has a private injection method.
*/
public function test_add_with_auto_arguments_throws_on_class_private_method_injection() {
$this->expectException( ContainerException::class );
$this->expectExceptionMessage( "Method '" . Definition::INJECTION_METHOD . "' of class '" . ClassWithPrivateConstructor::class . "' isn't public, instances can't be created." );
$this->expectExceptionMessage( "Method '" . Definition::INJECTION_METHOD . "' of class '" . ClassWithPrivateInjectionMethod::class . "' isn't public, instances can't be created." );
$this->sut->add_with_auto_arguments( ClassWithPrivateConstructor::class );
$this->sut->add_with_auto_arguments( ClassWithPrivateInjectionMethod::class );
}
/**
* @testdox 'add_with_auto_arguments' should throw an exception if the passed concrete is a class with a private method.
* @testdox 'add_with_auto_arguments' should throw an exception if the passed concrete is a class with a private injection method.
*/
public function test_add_with_auto_arguments_throws_on_concrete_private_method_injection() {
$this->expectException( ContainerException::class );
$this->expectExceptionMessage( "Method '" . Definition::INJECTION_METHOD . "' of class '" . ClassWithPrivateConstructor::class . "' isn't public, instances can't be created." );
$this->expectExceptionMessage( "Method '" . Definition::INJECTION_METHOD . "' of class '" . ClassWithPrivateInjectionMethod::class . "' isn't public, instances can't be created." );
$this->sut->add_with_auto_arguments( ClassWithDependencies::class, ClassWithPrivateConstructor::class );
$this->sut->add_with_auto_arguments( ClassWithDependencies::class, ClassWithPrivateInjectionMethod::class );
}
/**
@ -115,9 +115,9 @@ class AbstractServiceProviderTest extends \WC_Unit_Test_Case {
*/
public function test_add_with_auto_arguments_throws_on_method_argument_without_type_hint() {
$this->expectException( ContainerException::class );
$this->expectExceptionMessage( "Argument 'argument_without_type_hint' of class '" . ClassWithConstructorArgumentWithoutTypeHint::class . "' doesn't have a type hint or has one that doesn't specify a class." );
$this->expectExceptionMessage( "Argument 'argument_without_type_hint' of class '" . ClassWithInjectionMethodArgumentWithoutTypeHint::class . "' doesn't have a type hint or has one that doesn't specify a class." );
$this->sut->add_with_auto_arguments( ClassWithConstructorArgumentWithoutTypeHint::class );
$this->sut->add_with_auto_arguments( ClassWithInjectionMethodArgumentWithoutTypeHint::class );
}
/**
@ -125,9 +125,9 @@ class AbstractServiceProviderTest extends \WC_Unit_Test_Case {
*/
public function test_add_with_auto_arguments_throws_on_method_argument_with_scalar_type_hint() {
$this->expectException( ContainerException::class );
$this->expectExceptionMessage( "Argument 'scalar_argument_without_default_value' of class '" . ClassWithScalarConstructorArgument::class . "' doesn't have a type hint or has one that doesn't specify a class." );
$this->expectExceptionMessage( "Argument 'scalar_argument_without_default_value' of class '" . ClassWithScalarInjectionMethodArgument::class . "' doesn't have a type hint or has one that doesn't specify a class." );
$this->sut->add_with_auto_arguments( ClassWithScalarConstructorArgument::class );
$this->sut->add_with_auto_arguments( ClassWithScalarInjectionMethodArgument::class );
}
/**
@ -154,7 +154,7 @@ class AbstractServiceProviderTest extends \WC_Unit_Test_Case {
// Arguments with default values are honored.
$this->assertEquals( ClassWithDependencies::SOME_NUMBER, $resolved->some_number );
// Constructor arguments are filled as expected.
// Method arguments are filled as expected.
$this->assertSame( $this->container->get( DependencyClass::class ), $resolved->dependency_class );
}

View File

@ -1,6 +1,6 @@
<?php
/**
* ClassWithConstructorArgumentWithoutTypeHint class file.
* ClassWithInjectionMethodArgumentWithoutTypeHint class file.
*
* @package Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses
*/
@ -8,9 +8,9 @@
namespace Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses;
/**
* An example class that has a constructor argument without type hint.
* An example class that has a injector method argument without type hint.
*/
class ClassWithConstructorArgumentWithoutTypeHint {
class ClassWithInjectionMethodArgumentWithoutTypeHint {
/**
* Sets class dependencies.

View File

@ -1,6 +1,6 @@
<?php
/**
* ClassWithPrivateConstructor class file.
* ClassWithPrivateInjectionMethod class file.
*
* @package Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses
*/
@ -8,9 +8,9 @@
namespace Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses;
/**
* An example of a class with a private constructor.
* An example of a class with a private injection method.
*/
class ClassWithPrivateConstructor {
class ClassWithPrivateInjectionMethod {
/**
* Sets class dependencies.

View File

@ -1,6 +1,6 @@
<?php
/**
* ClassWithConstructorArgumentWithoutTypeHint class file.
* ClassWithScalarInjectionMethodArgument class file.
*
* @package Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses
*/
@ -8,9 +8,9 @@
namespace Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses;
/**
* An example class that has a constructor argument with a scalar type but without a default value.
* An example class that has an injector method argument with a scalar type but without a default value.
*/
class ClassWithScalarConstructorArgument {
class ClassWithScalarInjectionMethodArgument {
// phpcs:disable Squiz.Commenting.FunctionComment.InvalidTypeHint

View File

@ -7,6 +7,7 @@
namespace Automattic\WooCommerce\Tests\Proxies;
use Automattic\WooCommerce\Internal\DependencyManagement\Definition;
use Automattic\WooCommerce\Proxies\LegacyProxy;
use Automattic\WooCommerce\Tests\Internal\DependencyManagement\ExampleClasses\DependencyClass;
@ -33,7 +34,7 @@ class LegacyProxyTest extends \WC_Unit_Test_Case {
*/
public function test_get_instance_of_throws_when_trying_to_get_a_namespaced_class() {
$this->expectException( \Exception::class );
$this->expectExceptionMessage( 'The LegacyProxy class is not intended for getting instances of classes in the src directory, please use method injection or the instance of \Psr\Container\ContainerInterface for that.' );
$this->expectExceptionMessage( 'The LegacyProxy class is not intended for getting instances of classes in the src directory, please use ' . Definition::INJECTION_METHOD . ' method injection or the instance of \Psr\Container\ContainerInterface for that.' );
$this->sut->get_instance_of( DependencyClass::class );
}