From 1c12183b0e82563ec63e130ba6df5533b520d433 Mon Sep 17 00:00:00 2001 From: Zacharias Luiten Date: Mon, 16 Aug 2021 20:38:02 +0200 Subject: [PATCH 1/3] Rewrite of the project using native Attributes instead of annotations. --- README.md | 6 +- Upgrade.md | 10 +- composer.json | 6 +- composer.lock | 428 ++++++------------ docs/advanced/bean-post-processor.md | 4 +- docs/advanced/structure-config.md | 8 +- docs/basic/basic-concepts.md | 6 +- docs/basic/bean-config.md | 28 +- docs/basic/bean-parameters.md | 18 +- docs/basic/injecting-dependencies.md | 4 +- docs/intro/getting-started.md | 2 +- src/bitExpert/Disco/Annotations/Alias.php | 75 --- .../Annotations/AnnotationAttributeParser.php | 41 -- src/bitExpert/Disco/Annotations/Bean.php | 148 ------ .../Disco/Annotations/BeanPostProcessor.php | 41 -- src/bitExpert/Disco/Annotations/Parameter.php | 102 ----- .../Annotations/ParameterAwareAnnotation.php | 53 --- src/bitExpert/Disco/Attributes/Alias.php | 47 ++ src/bitExpert/Disco/Attributes/Bean.php | 86 ++++ .../Disco/Attributes/BeanPostProcessor.php | 23 + .../Configuration.php | 8 +- src/bitExpert/Disco/Attributes/Parameter.php | 91 ++++ .../Disco/Attributes/ReturnTypeAlias.php | 25 + .../Configuration/ConfigurationGenerator.php | 127 +++--- .../MethodGenerator/BeanMethod.php | 43 +- .../BeanPostProcessorMethod.php | 9 +- .../MethodGenerator/Constructor.php | 1 - .../MethodGenerator/GetAlias.php | 1 - .../MethodGenerator/GetParameter.php | 1 - .../MethodGenerator/HasAlias.php | 1 - .../ParameterAwareMethodGenerator.php | 4 +- .../MethodGenerator/WrapBeanAsLazy.php | 1 - .../Disco/Annotations/AliasUnitTest.php | 65 +-- .../AnnotationAttributeParserUnitTest.php | 54 --- .../Annotations/BeanPostProcessorUnitTest.php | 64 --- .../Disco/Annotations/BeanUnitTest.php | 170 +------ .../Disco/Annotations/ParameterUnitTest.php | 45 +- .../Disco/Config/BeanConfiguration.php | 48 +- .../Config/BeanConfigurationPersistence.php | 12 +- .../Config/BeanConfigurationSubclass.php | 12 +- .../Disco/Config/BeanConfigurationTrait.php | 6 +- .../Config/BeanConfigurationWithAliases.php | 48 +- ...eanConfigurationWithConflictingAliases.php | 30 +- ...ionWithConflictingAliasesInParentClass.php | 20 +- ...gurationWithParameterizedPostProcessor.php | 25 +- .../BeanConfigurationWithParameters.php | 80 +--- .../BeanConfigurationWithPostProcessor.php | 24 +- ...ostProcessorAndParameterizedDependency.php | 29 +- .../BeanConfigurationWithPrimitives.php | 37 +- .../BeanConfigurationWithProtectedMethod.php | 24 +- ...eanConfigurationOverwritingParentAlias.php | 20 +- .../Disco/Config/InterfaceConfiguration.php | 6 +- .../Disco/Config/InvalidConfiguration.php | 2 +- .../MissingBeanAnnotationConfiguration.php | 6 +- .../Config/MissingReturnTypeConfiguration.php | 12 +- .../NonExistentReturnTypeConfiguration.php | 12 +- .../Traits/NonSingletonNonLazyRequestBean.php | 6 +- .../Config/WrongReturnTypeConfiguration.php | 24 +- .../ConfigurationGeneratorUnitTest.php | 8 +- 59 files changed, 764 insertions(+), 1573 deletions(-) delete mode 100644 src/bitExpert/Disco/Annotations/Alias.php delete mode 100644 src/bitExpert/Disco/Annotations/AnnotationAttributeParser.php delete mode 100644 src/bitExpert/Disco/Annotations/Bean.php delete mode 100644 src/bitExpert/Disco/Annotations/BeanPostProcessor.php delete mode 100644 src/bitExpert/Disco/Annotations/Parameter.php delete mode 100644 src/bitExpert/Disco/Annotations/ParameterAwareAnnotation.php create mode 100644 src/bitExpert/Disco/Attributes/Alias.php create mode 100644 src/bitExpert/Disco/Attributes/Bean.php create mode 100644 src/bitExpert/Disco/Attributes/BeanPostProcessor.php rename src/bitExpert/Disco/{Annotations => Attributes}/Configuration.php (60%) create mode 100644 src/bitExpert/Disco/Attributes/Parameter.php create mode 100644 src/bitExpert/Disco/Attributes/ReturnTypeAlias.php delete mode 100644 tests/bitExpert/Disco/Annotations/AnnotationAttributeParserUnitTest.php delete mode 100644 tests/bitExpert/Disco/Annotations/BeanPostProcessorUnitTest.php diff --git a/README.md b/README.md index cff94f5..fa88bad 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Next up you need to create a configuration class `MyConfiguration` and document ```php =7.1" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.22-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "support": { + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.22.1" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2021-01-07T16:49:33+00:00" + }, { "name": "webimpress/safe-writer", "version": "2.1.0", @@ -652,6 +581,64 @@ } ], "time": "2020-08-25T07:21:11+00:00" + }, + { + "name": "webmozart/assert", + "version": "1.10.0", + "source": { + "type": "git", + "url": "https://github.com/webmozarts/assert.git", + "reference": "6964c76c7804814a842473e0c8fd15bab0f18e25" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webmozarts/assert/zipball/6964c76c7804814a842473e0c8fd15bab0f18e25", + "reference": "6964c76c7804814a842473e0c8fd15bab0f18e25", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0", + "symfony/polyfill-ctype": "^1.8" + }, + "conflict": { + "phpstan/phpstan": "<0.12.20", + "vimeo/psalm": "<4.6.1 || 4.6.2" + }, + "require-dev": { + "phpunit/phpunit": "^8.5.13" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.10-dev" + } + }, + "autoload": { + "psr-4": { + "Webmozart\\Assert\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Assertions to validate method input/output with nice error messages.", + "keywords": [ + "assert", + "check", + "validate" + ], + "support": { + "issues": "https://github.com/webmozarts/assert/issues", + "source": "https://github.com/webmozarts/assert/tree/1.10.0" + }, + "time": "2021-03-09T10:59:23+00:00" } ], "packages-dev": [ @@ -3347,85 +3334,6 @@ }, "time": "2020-10-23T02:01:07+00:00" }, - { - "name": "symfony/polyfill-ctype", - "version": "v1.22.1", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "c6c942b1ac76c82448322025e084cadc56048b4e" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/c6c942b1ac76c82448322025e084cadc56048b4e", - "reference": "c6c942b1ac76c82448322025e084cadc56048b4e", - "shasum": "" - }, - "require": { - "php": ">=7.1" - }, - "suggest": { - "ext-ctype": "For best performance" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-main": "1.22-dev" - }, - "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Ctype\\": "" - }, - "files": [ - "bootstrap.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Gert de Pagter", - "email": "BackEndTea@gmail.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for ctype functions", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "ctype", - "polyfill", - "portable" - ], - "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.22.1" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2021-01-07T16:49:33+00:00" - }, { "name": "theseer/tokenizer", "version": "1.2.0", @@ -3475,64 +3383,6 @@ } ], "time": "2020-07-12T23:59:07+00:00" - }, - { - "name": "webmozart/assert", - "version": "1.10.0", - "source": { - "type": "git", - "url": "https://github.com/webmozarts/assert.git", - "reference": "6964c76c7804814a842473e0c8fd15bab0f18e25" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/webmozarts/assert/zipball/6964c76c7804814a842473e0c8fd15bab0f18e25", - "reference": "6964c76c7804814a842473e0c8fd15bab0f18e25", - "shasum": "" - }, - "require": { - "php": "^7.2 || ^8.0", - "symfony/polyfill-ctype": "^1.8" - }, - "conflict": { - "phpstan/phpstan": "<0.12.20", - "vimeo/psalm": "<4.6.1 || 4.6.2" - }, - "require-dev": { - "phpunit/phpunit": "^8.5.13" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.10-dev" - } - }, - "autoload": { - "psr-4": { - "Webmozart\\Assert\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Bernhard Schussek", - "email": "bschussek@gmail.com" - } - ], - "description": "Assertions to validate method input/output with nice error messages.", - "keywords": [ - "assert", - "check", - "validate" - ], - "support": { - "issues": "https://github.com/webmozarts/assert/issues", - "source": "https://github.com/webmozarts/assert/tree/1.10.0" - }, - "time": "2021-03-09T10:59:23+00:00" } ], "aliases": [], @@ -3546,5 +3396,5 @@ "php": "^8.0.0" }, "platform-dev": [], - "plugin-api-version": "2.0.0" + "plugin-api-version": "2.1.0" } diff --git a/docs/advanced/bean-post-processor.md b/docs/advanced/bean-post-processor.md index 369792c..b4169e6 100644 --- a/docs/advanced/bean-post-processor.md +++ b/docs/advanced/bean-post-processor.md @@ -30,8 +30,8 @@ To register the `SampleServiceBeanPostProcessor` with Disco create a method in y ```php > $attributes - * @throws AnnotationException - */ - public function __construct(array $attributes = []) - { - $this->type = false; - - if (isset($attributes['value']['type'])) { - $this->type = AnnotationAttributeParser::parseBooleanValue($attributes['value']['type']); - } - - if (isset($attributes['value']['name'])) { - if ($this->type) { - throw new AnnotationException('Type alias should not have a name!'); - } - - $this->name = $attributes['value']['name']; - } - - if (!$this->type && (!is_string($this->name) || $this->name === '')) { - throw new AnnotationException('Alias should either be a named alias or a type alias!'); - } - } - - public function getName(): ?string - { - return $this->name; - } - - public function isTypeAlias(): bool - { - return $this->type; - } -} diff --git a/src/bitExpert/Disco/Annotations/AnnotationAttributeParser.php b/src/bitExpert/Disco/Annotations/AnnotationAttributeParser.php deleted file mode 100644 index e9b77cc..0000000 --- a/src/bitExpert/Disco/Annotations/AnnotationAttributeParser.php +++ /dev/null @@ -1,41 +0,0 @@ -"), - * @Attribute("parameters", type = "array<\bitExpert\Disco\Annotations\Parameter>") - * }) - */ -final class Bean extends ParameterAwareAnnotation -{ - const SCOPE_REQUEST = 1; - const SCOPE_SESSION = 2; - /** - * @var int - */ - protected $scope; - /** - * @var bool - */ - protected $singleton; - /** - * @var bool - */ - protected $lazy; - /** - * @var Alias[] - */ - protected $aliases; - - /** - * Creates a new {@link \bitExpert\Disco\Annotations\Bean}. - * - * @param array> $attributes - */ - public function __construct(array $attributes = []) - { - parent::__construct(); - - // initialize default values - $this->scope = self::SCOPE_REQUEST; - $this->singleton = true; - $this->lazy = false; - $this->aliases = []; - - if (isset($attributes['value'])) { - if (isset($attributes['value']['scope']) && \strtolower($attributes['value']['scope']) === 'session') { - $this->scope = self::SCOPE_SESSION; - } - - if (isset($attributes['value']['singleton'])) { - $this->singleton = AnnotationAttributeParser::parseBooleanValue($attributes['value']['singleton']); - } - - if (isset($attributes['value']['lazy'])) { - $this->lazy = AnnotationAttributeParser::parseBooleanValue($attributes['value']['lazy']); - } - - if (isset($attributes['value']['aliases']) && \is_array($attributes['value']['aliases'])) { - $this->setAliases(...$attributes['value']['aliases']); - } - - if (isset($attributes['value']['parameters']) && \is_array($attributes['value']['parameters'])) { - $this->setParameters(...$attributes['value']['parameters']); - } - } - } - - /** - * Helper methd to ensure that the passed aliases are of {@link \bitExpert\Disco\Annotations\Alias} type. - * - * @param Alias ...$aliases - */ - private function setAliases(Alias ...$aliases): void - { - $this->aliases = $aliases; - } - - /** - * Returns true if the current scope if of type Scope::REQUEST. - * - * @return bool - */ - public function isRequest(): bool - { - return $this->scope === self::SCOPE_REQUEST; - } - - /** - * Returns true if the current scope if of type Scope::SESSION. - * - * @return bool - */ - public function isSession(): bool - { - return $this->scope === self::SCOPE_SESSION; - } - - /** - * Returns true if the Bean should be a singleton instance. - * - * @return bool - */ - public function isSingleton(): bool - { - return $this->singleton; - } - - /** - * Returns true if the Bean should be a lazily instantiated. - * - * @return bool - */ - public function isLazy(): bool - { - return $this->lazy; - } - - /** - * Returns the list of aliases for the bean instance. Returns an empty array when no alias was set. - * - * @return Alias[] - */ - public function getAliases(): array - { - return $this->aliases; - } -} diff --git a/src/bitExpert/Disco/Annotations/BeanPostProcessor.php b/src/bitExpert/Disco/Annotations/BeanPostProcessor.php deleted file mode 100644 index d22f3db..0000000 --- a/src/bitExpert/Disco/Annotations/BeanPostProcessor.php +++ /dev/null @@ -1,41 +0,0 @@ -") - * }) - */ -final class BeanPostProcessor extends ParameterAwareAnnotation -{ - /** - * Creates a new {@link \bitExpert\Disco\Annotations\BeanPostProcessor}. - * - * @param array> $attributes - */ - public function __construct(array $attributes = []) - { - parent::__construct(); - - if (isset($attributes['value']['parameters']) && \is_array($attributes['value']['parameters'])) { - $this->setParameters(...$attributes['value']['parameters']); - } - } -} diff --git a/src/bitExpert/Disco/Annotations/Parameter.php b/src/bitExpert/Disco/Annotations/Parameter.php deleted file mode 100644 index 60b8da7..0000000 --- a/src/bitExpert/Disco/Annotations/Parameter.php +++ /dev/null @@ -1,102 +0,0 @@ -> $attributes - * @throws AnnotationException - */ - public function __construct(array $attributes = []) - { - $this->required = true; - $this->name = ''; - - if (isset($attributes['value'])) { - if (isset($attributes['value']['name'])) { - $this->name = $attributes['value']['name']; - } - - if (isset($attributes['value']['default'])) { - $this->defaultValue = $attributes['value']['default']; - } - - if (isset($attributes['value']['required'])) { - $this->required = AnnotationAttributeParser::parseBooleanValue($attributes['value']['required']); - } - } - - if ($this->name === '') { - throw new AnnotationException('name attribute missing!'); - } - } - - /** - * Returns the name of the configuration value to use. - * - * @return string - */ - public function getName(): string - { - return $this->name; - } - - /** - * Returns the default value to use in case the configuration value is not defined. - * - * @return mixed - */ - public function getDefaultValue(): mixed - { - return $this->defaultValue; - } - - /** - * Returns true if the parameter is required, false for an optional parameter. - * - * @return bool - */ - public function isRequired(): bool - { - return $this->required; - } -} diff --git a/src/bitExpert/Disco/Annotations/ParameterAwareAnnotation.php b/src/bitExpert/Disco/Annotations/ParameterAwareAnnotation.php deleted file mode 100644 index b30007d..0000000 --- a/src/bitExpert/Disco/Annotations/ParameterAwareAnnotation.php +++ /dev/null @@ -1,53 +0,0 @@ -parameters = []; - } - - /** - * Returns the list of parameters for the bean post processor instance. Returns an empty array when no parameters - * were set. - * - * @return Parameter[] - */ - public function getParameters(): array - { - return $this->parameters; - } - - /** - * Helper methd to ensure that the passed parameters are of {@link \bitExpert\Disco\Annotations\Parameter} type. - * - * @param Parameter[] ...$parameters - */ - protected function setParameters(Parameter ...$parameters): void - { - $this->parameters = $parameters; - } -} diff --git a/src/bitExpert/Disco/Attributes/Alias.php b/src/bitExpert/Disco/Attributes/Alias.php new file mode 100644 index 0000000..3c43133 --- /dev/null +++ b/src/bitExpert/Disco/Attributes/Alias.php @@ -0,0 +1,47 @@ +name = $name; + } + + public function getName(): string + { + return $this->name; + } +} diff --git a/src/bitExpert/Disco/Attributes/Bean.php b/src/bitExpert/Disco/Attributes/Bean.php new file mode 100644 index 0000000..242ae4e --- /dev/null +++ b/src/bitExpert/Disco/Attributes/Bean.php @@ -0,0 +1,86 @@ +singleton = $singleton; + $this->lazy = $lazy; + $this->scope = $scope; + } + + /** + * Returns true if the current scope if of type Scope::REQUEST. + * + * @return bool + */ + public function isRequest(): bool + { + return $this->scope === self::SCOPE_REQUEST; + } + + /** + * Returns true if the current scope if of type Scope::SESSION. + * + * @return bool + */ + public function isSession(): bool + { + return $this->scope === self::SCOPE_SESSION; + } + + /** + * Returns true if the Bean should be a singleton instance. + * + * @return bool + */ + public function isSingleton(): bool + { + return $this->singleton; + } + + /** + * Returns true if the Bean should be a lazily instantiated. + * + * @return bool + */ + public function isLazy(): bool + { + return $this->lazy; + } +} diff --git a/src/bitExpert/Disco/Attributes/BeanPostProcessor.php b/src/bitExpert/Disco/Attributes/BeanPostProcessor.php new file mode 100644 index 0000000..5609cdf --- /dev/null +++ b/src/bitExpert/Disco/Attributes/BeanPostProcessor.php @@ -0,0 +1,23 @@ +name = $name; + $this->key = $key; + $this->defaultValue = $default; + $this->required = $required; + } + + /** + * Return the name of the parameter + * + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * Returns the key of the configuration value to use. + * + * @return string + */ + public function getKey(): string + { + return $this->key; + } + + /** + * Returns the default value to use in case the configuration value is not defined. + * + * @return mixed + */ + public function getDefaultValue(): mixed + { + return $this->defaultValue; + } + + /** + * Returns true if the parameter is required, false for an optional parameter. + * + * @return bool + */ + public function isRequired(): bool + { + return $this->required; + } +} diff --git a/src/bitExpert/Disco/Attributes/ReturnTypeAlias.php b/src/bitExpert/Disco/Attributes/ReturnTypeAlias.php new file mode 100644 index 0000000..bf642f0 --- /dev/null +++ b/src/bitExpert/Disco/Attributes/ReturnTypeAlias.php @@ -0,0 +1,25 @@ +getClassAnnotation($originalClass, Configuration::class); - } catch (Exception $e) { - throw new InvalidProxiedClassException($e->getMessage(), $e->getCode(), $e); - } + $configurationAttribute = $originalClass->getAttributes(Configuration::class)[0] ?? null; - if (null === $annotation) { + if (null === $configurationAttribute) { throw new InvalidProxiedClassException( sprintf( - '"%s" seems not to be a valid configuration class. @Configuration annotation missing!', + '"%s" seems not to be a valid configuration class. #[Configuration] attribute missing!', $originalClass->name ) ); @@ -112,22 +95,26 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe $localAliases = []; $methods = $originalClass->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED); foreach ($methods as $method) { - $methodReflection = new MethodReflection( + $reflectionMethod = new MethodReflection( $method->class, $method->name ); - /** @var \bitExpert\Disco\Annotations\Bean|null $beanAnnotation */ - $beanAnnotation = $reader->getMethodAnnotation($method, Bean::class); - if (null === $beanAnnotation) { - /** @var \bitExpert\Disco\Annotations\BeanPostProcessor|null $beanAnnotation */ - $beanAnnotation = $reader->getMethodAnnotation($method, BeanPostProcessor::class); - if ($beanAnnotation instanceof BeanPostProcessor) { + /** @var Bean|null $beanAttribute */ + $beanAttribute = ($reflectionMethod->getAttributes(Bean::class)[0] ?? null)?->newInstance(); + /** @var Parameter[] $parameterAttributes */ + $parameterAttributes = \array_map( + fn($attributeRefl) => $attributeRefl->newInstance(), + $reflectionMethod->getAttributes(Parameter::class) + ); + if (null === $beanAttribute) { + $postProcessorAttribute = $reflectionMethod->getAttributes(BeanPostProcessor::class)[0] ?? null; + if (null !== $postProcessorAttribute) { $postProcessorMethods[] = $method->name; $proxyMethod = BeanPostProcessorMethod::generateMethod( - $methodReflection, - $beanAnnotation, + $reflectionMethod, + $parameterAttributes, $getParameterMethod ); $classGenerator->addMethodFromGenerator($proxyMethod); @@ -138,32 +125,69 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe continue; } - // every method needs either @Bean or @PostPostprocessor annotation + // every method needs either #[Bean] or #[PostPostprocessor] attribute throw new InvalidProxiedClassException( sprintf( - 'Method "%s" on "%s" is missing the @Bean annotation!', + 'Method "%s" on "%s" is missing the #[Bean] (or #[BeanPostProcessor]) attribute ' + . 'or its scope must be protected!', $method->name, $originalClass->name ) ); } - foreach ($beanAnnotation->getAliases() as $beanAlias) { - $alias = $beanAlias->isTypeAlias() ? (string) $method->getReturnType() : $beanAlias->getName(); + $beanAliases = []; + + /** @var \bitExpert\Disco\Attributes\ReturnTypeAlias $returnTypeAlias */ + $returnTypeAlias = ($reflectionMethod->getAttributes(ReturnTypeAlias::class)[0] ?? null) + ?->newInstance(); + if (null !== $returnTypeAlias) { + $returnTypeRefl = $method->getReturnType(); + + if ($returnTypeRefl instanceof ReflectionUnionType) { + throw new InvalidProxiedClassException( + sprintf( + 'Method "%s" on "%s" uses the unsupported union type.', + $method->name, + $originalClass->name + ) + ); + } + + if (null === $returnTypeRefl || $returnTypeRefl->allowsNull() || $returnTypeRefl->isBuiltin()) { + throw new InvalidProxiedClassException( + sprintf( + 'Cannot use #[ReturnTypeAlias] on method "%s" on "%s" because it\'s returning a ' + . 'builtin type ("%s").', + $method->name, + $originalClass->name, + $returnTypeRefl === null || $returnTypeRefl->allowsNull() + ? 'null' + : $returnTypeRefl->getName() + ) + ); + } + + $beanAliases[] = $returnTypeRefl->getName(); + } + + $beanAliases = [...$beanAliases, ...\array_map( + fn($attr) => $attr->newInstance()->getName(), + $reflectionMethod->getAttributes(Alias::class) + )]; - $hasAlias = ''; + foreach ($beanAliases as $beanAlias) { if ($method->getDeclaringClass()->name === $originalClass->name) { - $hasAlias = $localAliases[$alias] ?? ''; + $hasAlias = $localAliases[$beanAlias] ?? ''; } else { - $hasAlias= $parentAliases[$alias] ?? ''; + $hasAlias = $parentAliases[$beanAlias] ?? ''; } if ($hasAlias !== '') { throw new InvalidProxiedClassException( sprintf( - 'Alias "%s" of method "%s" on "%s" is already used by method "%s" of another Bean!' - . ' Did you use a type alias twice?', - $alias, + 'Alias "%s" of method "%s" on "%s" is already used by method "%s" of another Bean!', + $beanAlias, $method->name, $originalClass->name, $hasAlias @@ -172,15 +196,16 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe } if ($method->getDeclaringClass()->name === $originalClass->name) { - $localAliases[$alias] = $method->name; + $localAliases[$beanAlias] = $method->name; } else { - $parentAliases[$alias] = $method->name; + $parentAliases[$beanAlias] = $method->name; } } $proxyMethod = BeanMethod::generateMethod( - $methodReflection, - $beanAnnotation, + $reflectionMethod, + $beanAttribute, + $parameterAttributes, $method->getReturnType(), $forceLazyInitProperty, $sessionBeansProperty, diff --git a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanMethod.php b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanMethod.php index 683d2c8..b924d69 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanMethod.php +++ b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanMethod.php @@ -12,7 +12,8 @@ namespace bitExpert\Disco\Proxy\Configuration\MethodGenerator; -use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Parameter; use bitExpert\Disco\BeanException; use bitExpert\Disco\InitializedBean; use bitExpert\Disco\Proxy\Configuration\PropertyGenerator\BeanFactoryConfigurationProperty; @@ -22,7 +23,7 @@ use bitExpert\Disco\Proxy\LazyBean\LazyBeanFactory; use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Proxy\LazyLoadingInterface; -use ReflectionType; +use ReflectionNamedType; use Laminas\Code\Generator\MethodGenerator; use Laminas\Code\Generator\ParameterGenerator; use Laminas\Code\Reflection\MethodReflection; @@ -39,8 +40,9 @@ class BeanMethod extends ParameterAwareMethodGenerator * Creates a new {@link \bitExpert\Disco\Proxy\Configuration\MethodGenerator\BeanMethod}. * * @param MethodReflection $originalMethod - * @param Bean $beanMetadata - * @param ReflectionType|null $beanType + * @param \bitExpert\Disco\Attributes\Bean $beanMetadata + * @param \bitExpert\Disco\Attributes\Parameter[] $parameters + * @param ReflectionNamedType|null $beanTypeName * @param ForceLazyInitProperty $forceLazyInitProperty * @param SessionBeansProperty $sessionBeansProperty * @param BeanPostProcessorsProperty $postProcessorsProperty @@ -52,15 +54,16 @@ class BeanMethod extends ParameterAwareMethodGenerator * @throws \ProxyManager\Exception\InvalidProxiedClassException */ public static function generateMethod( - MethodReflection $originalMethod, - Bean $beanMetadata, - ?ReflectionType $beanType, - ForceLazyInitProperty $forceLazyInitProperty, - SessionBeansProperty $sessionBeansProperty, - BeanPostProcessorsProperty $postProcessorsProperty, + MethodReflection $originalMethod, + Bean $beanMetadata, + array $parameters, + ?ReflectionNamedType $beanType, + ForceLazyInitProperty $forceLazyInitProperty, + SessionBeansProperty $sessionBeansProperty, + BeanPostProcessorsProperty $postProcessorsProperty, BeanFactoryConfigurationProperty $beanFactoryConfigurationProperty, - GetParameter $parameterValuesMethod, - WrapBeanAsLazy $wrapBeanAsLazy + GetParameter $parameterValuesMethod, + WrapBeanAsLazy $wrapBeanAsLazy ): MethodGenerator { if (null === $beanType) { throw new InvalidProxiedClassException( @@ -71,22 +74,22 @@ public static function generateMethod( ) ); } - $beanType = (string) $beanType; + $beanTypeName = $beanType->getName(); $method = static::fromReflection($originalMethod); - $methodParams = static::convertMethodParamsToString($beanMetadata->getParameters(), $parameterValuesMethod); + $methodParams = static::convertMethodParamsToString($parameters, $parameterValuesMethod); $beanId = $originalMethod->name; $body = ''; - if (in_array($beanType, ['array', 'callable', 'bool', 'float', 'int', 'string'], true)) { + if ($beanType->isBuiltin()) { // return type is a primitive, simply call parent method and return immediately $body .= 'return parent::' . $beanId . '(' . $methodParams . ');' . PHP_EOL; - } elseif (class_exists($beanType) || interface_exists($beanType)) { + } elseif (class_exists($beanTypeName) || interface_exists($beanTypeName)) { if ($beanMetadata->isLazy()) { $body = static::generateLazyBeanCode( '', $beanId, - $beanType, + $beanTypeName, $beanMetadata, $methodParams, $forceLazyInitProperty, @@ -98,7 +101,7 @@ public static function generateMethod( $body = static::generateNonLazyBeanCode( '', $beanId, - $beanType, + $beanTypeName, $beanMetadata, $methodParams, $forceLazyInitProperty, @@ -154,7 +157,7 @@ public static function fromReflection(MethodReflection $reflectionMethod): Metho * @param string $padding * @param string $beanId * @param string $beanType - * @param Bean $beanMetadata + * @param \bitExpert\Disco\Attributes\Bean $beanMetadata * @param string $methodParams * @param ForceLazyInitProperty $forceLazyInitProperty * @param SessionBeansProperty $sessionBeansProperty @@ -279,7 +282,7 @@ protected static function generateBeanCreationCode( * @param string $padding * @param string $beanId * @param string $beanType - * @param Bean $beanMetadata + * @param \bitExpert\Disco\Attributes\Bean $beanMetadata * @param string $methodParams * @param ForceLazyInitProperty $forceLazyInitProperty * @param SessionBeansProperty $sessionBeansProperty diff --git a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanPostProcessorMethod.php b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanPostProcessorMethod.php index ed49b09..8e58f9e 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanPostProcessorMethod.php +++ b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanPostProcessorMethod.php @@ -12,7 +12,8 @@ namespace bitExpert\Disco\Proxy\Configuration\MethodGenerator; -use bitExpert\Disco\Annotations\BeanPostProcessor; +use bitExpert\Disco\Attributes\BeanPostProcessor; +use bitExpert\Disco\Attributes\Parameter; use Laminas\Code\Generator\MethodGenerator; use Laminas\Code\Reflection\MethodReflection; @@ -22,20 +23,20 @@ class BeanPostProcessorMethod extends ParameterAwareMethodGenerator * Creates a new {@link \bitExpert\Disco\Proxy\Configuration\MethodGenerator\BeanPostProcessorMethod}. * * @param MethodReflection $originalMethod - * @param BeanPostProcessor $beanPostProcessorMetadata + * @param \bitExpert\Disco\Attributes\Parameter[] $beanPostProcessorParameters * @param GetParameter $parameterValuesMethod * @return MethodGenerator * @throws \Laminas\Code\Generator\Exception\InvalidArgumentException */ public static function generateMethod( MethodReflection $originalMethod, - BeanPostProcessor $beanPostProcessorMetadata, + array $beanPostProcessorParameters, GetParameter $parameterValuesMethod ): MethodGenerator { $method = static::fromReflection($originalMethod); $methodParams = static::convertMethodParamsToString( - $beanPostProcessorMetadata->getParameters(), + $beanPostProcessorParameters, $parameterValuesMethod ); $beanId = $originalMethod->name; diff --git a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/Constructor.php b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/Constructor.php index b47c820..2fd5ec8 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/Constructor.php +++ b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/Constructor.php @@ -18,7 +18,6 @@ use bitExpert\Disco\Proxy\Configuration\PropertyGenerator\ParameterValuesProperty; use bitExpert\Disco\Proxy\Configuration\PropertyGenerator\SessionBeansProperty; use ProxyManager\Generator\MethodGenerator; -use ReflectionClass; use Laminas\Code\Generator\ParameterGenerator; /** diff --git a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/GetAlias.php b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/GetAlias.php index ff8b992..bc025ab 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/GetAlias.php +++ b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/GetAlias.php @@ -15,7 +15,6 @@ use bitExpert\Disco\BeanNotFoundException; use bitExpert\Disco\Proxy\Configuration\PropertyGenerator\AliasesProperty; use ProxyManager\Generator\MethodGenerator; -use ReflectionClass; use Laminas\Code\Generator\Exception\InvalidArgumentException; use Laminas\Code\Generator\ParameterGenerator; diff --git a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/GetParameter.php b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/GetParameter.php index 271362a..c665d69 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/GetParameter.php +++ b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/GetParameter.php @@ -15,7 +15,6 @@ use bitExpert\Disco\Proxy\Configuration\PropertyGenerator\ParameterValuesProperty; use ProxyManager\Generator\MethodGenerator; use ProxyManager\Generator\Util\UniqueIdentifierGenerator; -use ReflectionClass; use Laminas\Code\Generator\Exception\InvalidArgumentException; use Laminas\Code\Generator\ParameterGenerator; diff --git a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/HasAlias.php b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/HasAlias.php index eb54b64..05ba987 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/HasAlias.php +++ b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/HasAlias.php @@ -14,7 +14,6 @@ use bitExpert\Disco\Proxy\Configuration\PropertyGenerator\AliasesProperty; use ProxyManager\Generator\MethodGenerator; -use ReflectionClass; use Laminas\Code\Generator\Exception\InvalidArgumentException; use Laminas\Code\Generator\ParameterGenerator; diff --git a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/ParameterAwareMethodGenerator.php b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/ParameterAwareMethodGenerator.php index b8c73c6..8f3b119 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/ParameterAwareMethodGenerator.php +++ b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/ParameterAwareMethodGenerator.php @@ -12,7 +12,7 @@ namespace bitExpert\Disco\Proxy\Configuration\MethodGenerator; -use bitExpert\Disco\Annotations\Parameter; +use bitExpert\Disco\Attributes\Parameter; use Laminas\Code\Generator\MethodGenerator; /** @@ -53,7 +53,7 @@ protected static function convertMethodParamsToString( $template = ($defaultValue === '') ? '$this->%s("%s", %s)' : '$this->%s("%s", %s, %s)'; $required = $methodParameter->isRequired() ? 'true' : 'false'; $methodName = $parameterValuesMethod->getName(); - $parameters[] = \sprintf($template, $methodName, $methodParameter->getName(), $required, $defaultValue); + $parameters[] = \sprintf($template, $methodName, $methodParameter->getKey(), $required, $defaultValue); } return \implode(', ', $parameters); diff --git a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/WrapBeanAsLazy.php b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/WrapBeanAsLazy.php index 878150b..c826071 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/WrapBeanAsLazy.php +++ b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/WrapBeanAsLazy.php @@ -15,7 +15,6 @@ use bitExpert\Disco\Proxy\Configuration\PropertyGenerator\BeanFactoryConfigurationProperty; use ProxyManager\Generator\MethodGenerator; use ProxyManager\Generator\Util\UniqueIdentifierGenerator; -use ReflectionClass; use Laminas\Code\Generator\ParameterGenerator; /** diff --git a/tests/bitExpert/Disco/Annotations/AliasUnitTest.php b/tests/bitExpert/Disco/Annotations/AliasUnitTest.php index 73ccb69..412659a 100644 --- a/tests/bitExpert/Disco/Annotations/AliasUnitTest.php +++ b/tests/bitExpert/Disco/Annotations/AliasUnitTest.php @@ -12,11 +12,12 @@ namespace bitExpert\Disco\Annotations; -use Doctrine\Common\Annotations\AnnotationException; +use bitExpert\Disco\Attributes\Alias; use PHPUnit\Framework\TestCase; +use Webmozart\Assert\InvalidArgumentException; /** - * Unit tests for {@link \bitExpert\Disco\Annotations\Alias}. + * Unit tests for {@link \bitExpert\Disco\Attributes\Alias}. */ class AliasUnitTest extends TestCase { @@ -25,70 +26,18 @@ class AliasUnitTest extends TestCase */ public function aliasCanBeNamedAlias(): void { - $namedAlias = new Alias(['value' => ['name' => 'someAliasName']]); + $namedAlias = new Alias(name: 'someAliasName'); self::assertSame('someAliasName', $namedAlias->getName()); - self::assertFalse($namedAlias->isTypeAlias()); } /** * @test */ - public function aliasCannotBeNamedAliasAndTypeAlias(): void + public function aliasNameCannotBeEmpty(): void { - $this->expectException(AnnotationException::class); - $this->expectExceptionMessage('Type alias should not have a name!'); + $this->expectException(InvalidArgumentException::class); - new Alias(['value' => ['name' => 'someAliasName', 'type' => true]]); - } - - /** - * @test - */ - public function aliasCanBeTypeAlias(): void - { - $typeAlias = new Alias(['value' => ['type' => true]]); - - self::assertTrue($typeAlias->isTypeAlias()); - self::assertNull($typeAlias->getName()); - } - - /** - * @test - */ - public function aliasShouldBeNamedOrTypeAlias(): void - { - $this->expectException(AnnotationException::class); - $this->expectExceptionMessage('Alias should either be a named alias or a type alias!'); - - new Alias(); - } - - /** - * @test - * @dataProvider invalidNameProvider - * @param mixed $name - */ - public function aliasNameCannotBeEmpty(mixed $name): void - { - $this->expectException(AnnotationException::class); - $this->expectExceptionMessage('Alias should either be a named alias or a type alias!'); - - new Alias(['value' => ['name' => $name, 'type' => false]]); - } - - /** - * @return array - */ - public function invalidNameProvider(): array - { - return [ - [''], - [0], - [0.0], - [false], - [null], - [[]], - ]; + new Alias(name: ''); } } diff --git a/tests/bitExpert/Disco/Annotations/AnnotationAttributeParserUnitTest.php b/tests/bitExpert/Disco/Annotations/AnnotationAttributeParserUnitTest.php deleted file mode 100644 index 4f9b14b..0000000 --- a/tests/bitExpert/Disco/Annotations/AnnotationAttributeParserUnitTest.php +++ /dev/null @@ -1,54 +0,0 @@ - - */ - public function requireDataProvider(): array - { - $callable = function (): void { - }; - - return [ - [true, true], - [false, false], - ['true', true], - ['false', false], - ['anything else', false], - [1, true], - [0, false], - [new \stdClass(), false], - [[], false], - [$callable, false] - ]; - } -} diff --git a/tests/bitExpert/Disco/Annotations/BeanPostProcessorUnitTest.php b/tests/bitExpert/Disco/Annotations/BeanPostProcessorUnitTest.php deleted file mode 100644 index c65ae3a..0000000 --- a/tests/bitExpert/Disco/Annotations/BeanPostProcessorUnitTest.php +++ /dev/null @@ -1,64 +0,0 @@ - [ - 'parameters' => [ - new Parameter(['value' => ['name' => 'parameterName']]), - new Parameter(['value' => ['name' => 'yetAnotherParameter']]) - ] - ] - ]); - - self::assertEquals( - array_map( - function (Parameter $parameter): string { - return $parameter->getName(); - }, - $bean->getParameters() - ), - ['parameterName', 'yetAnotherParameter'] - ); - } - - /** - * @test - */ - public function throwsExceptionIfParameterTypeDoesNotMatch(): void - { - $this->expectException(TypeError::class); - - $bean = new Bean([ - 'value' => [ - 'parameters' => [ - new SampleService() - ] - ] - ]); - } -} diff --git a/tests/bitExpert/Disco/Annotations/BeanUnitTest.php b/tests/bitExpert/Disco/Annotations/BeanUnitTest.php index ed69c8e..ddab97c 100644 --- a/tests/bitExpert/Disco/Annotations/BeanUnitTest.php +++ b/tests/bitExpert/Disco/Annotations/BeanUnitTest.php @@ -12,12 +12,12 @@ namespace bitExpert\Disco\Annotations; -use bitExpert\Disco\Helper\SampleService; +use bitExpert\Disco\Attributes\Bean; use PHPUnit\Framework\TestCase; -use TypeError; +use Webmozart\Assert\InvalidArgumentException; /** - * Unit tests for {@link \bitExpert\Disco\Annotations\Bean}. + * Unit tests for {@link \bitExpert\Disco\Attributes\Bean}. */ class BeanUnitTest extends TestCase { @@ -32,8 +32,6 @@ public function emptyAttributesArraySetsDefaultValues(): void self::assertFalse($bean->isSession()); self::assertTrue($bean->isSingleton()); self::assertFalse($bean->isLazy()); - self::assertEmpty($bean->getAliases()); - self::assertEmpty($bean->getParameters()); } /** @@ -41,7 +39,7 @@ public function emptyAttributesArraySetsDefaultValues(): void */ public function markingBeanWithSessionScope(): void { - $bean = new Bean(['value' => ['scope' => 'session']]); + $bean = new Bean(scope: Bean::SCOPE_SESSION); self::assertTrue($bean->isSession()); self::assertFalse($bean->isRequest()); @@ -52,7 +50,7 @@ public function markingBeanWithSessionScope(): void */ public function markingBeanWithRequestScope(): void { - $bean = new Bean(['value' => ['scope' => 'request']]); + $bean = new Bean(scope: Bean::SCOPE_REQUEST); self::assertTrue($bean->isRequest()); self::assertFalse($bean->isSession()); @@ -63,7 +61,7 @@ public function markingBeanWithRequestScope(): void */ public function markingBeanAsSingleton(): void { - $bean = new Bean(['value' => ['singleton' => true]]); + $bean = new Bean(singleton: true); self::assertTrue($bean->isSingleton()); } @@ -73,17 +71,7 @@ public function markingBeanAsSingleton(): void */ public function markingBeanAsSingletonWithString(): void { - $bean = new Bean(['value' => ['singleton' => 'true']]); - - self::assertTrue($bean->isSingleton()); - } - - /** - * @test - */ - public function markingBeanAsSingletonWithInt(): void - { - $bean = new Bean(['value' => ['singleton' => 1]]); + $bean = new Bean(singleton: true); self::assertTrue($bean->isSingleton()); } @@ -93,27 +81,7 @@ public function markingBeanAsSingletonWithInt(): void */ public function markingBeanAsNonSingleton(): void { - $bean = new Bean(['value' => ['singleton' => false]]); - - self::assertFalse($bean->isSingleton()); - } - - /** - * @test - */ - public function markingBeanAsNonSingletonWithString(): void - { - $bean = new Bean(['value' => ['singleton' => 'false']]); - - self::assertFalse($bean->isSingleton()); - } - - /** - * @test - */ - public function markingBeanAsNonSingletonWithInt(): void - { - $bean = new Bean(['value' => ['singleton' => 0]]); + $bean = new Bean(singleton: false); self::assertFalse($bean->isSingleton()); } @@ -123,27 +91,7 @@ public function markingBeanAsNonSingletonWithInt(): void */ public function markingBeanAsLazy(): void { - $bean = new Bean(['value' => ['lazy' => true]]); - - self::assertTrue($bean->isLazy()); - } - - /** - * @test - */ - public function markingBeanAsLazyWithString(): void - { - $bean = new Bean(['value' => ['lazy' => 'true']]); - - self::assertTrue($bean->isLazy()); - } - - /** - * @test - */ - public function markingBeanAsLazyWithInt(): void - { - $bean = new Bean(['value' => ['lazy' => 1]]); + $bean = new Bean(lazy: true); self::assertTrue($bean->isLazy()); } @@ -153,17 +101,7 @@ public function markingBeanAsLazyWithInt(): void */ public function markingBeanAsNonLazy(): void { - $bean = new Bean(['value' => ['lazy' => false]]); - - self::assertFalse($bean->isLazy()); - } - - /** - * @test - */ - public function markingBeanAsNonLazyWithString(): void - { - $bean = new Bean(['value' => ['lazy' => 'false']]); + $bean = new Bean(lazy: false); self::assertFalse($bean->isLazy()); } @@ -171,92 +109,10 @@ public function markingBeanAsNonLazyWithString(): void /** * @test */ - public function markingBeanAsNonLazyWithInt(): void - { - $bean = new Bean(['value' => ['lazy' => 0]]); - - self::assertFalse($bean->isLazy()); - } - - /** - * @test - */ - public function configuredAliasesGetReturned(): void - { - $bean = new Bean([ - 'value' => [ - 'aliases' => [ - new Alias(['value' => ['name' => 'someAlias']]), - new Alias(['value' => ['name' => 'yetAnotherAlias']]) - ] - ] - ]); - - self::assertEquals( - array_map( - function (Alias $alias): ?string { - return $alias->getName(); - }, - $bean->getAliases() - ), - ['someAlias', 'yetAnotherAlias'] - ); - } - - /** - * @test - */ - public function throwsExceptionIfAliasTypeDoesNotMatch(): void - { - $this->expectException(TypeError::class); - - $bean = new Bean([ - 'value' => [ - 'aliases' => [ - new SampleService() - ] - ] - ]); - } - - /** - * @test - */ - public function configuredParametersGetReturned(): void - { - $bean = new Bean([ - 'value' => [ - 'parameters' => [ - new Parameter(['value' => ['name' => 'parameterName']]), - new Parameter(['value' => ['name' => 'yetAnotherParameter']]) - ] - ] - ]); - - self::assertEquals( - array_map( - function (Parameter $parameter): string { - return $parameter->getName(); - }, - $bean->getParameters() - ), - ['parameterName', 'yetAnotherParameter'] - ); - } - - /** - * @test - */ - public function throwsExceptionIfParameterTypeDoesNotMatch(): void + public function throwsExceptionIfScopeIsInvalid(): void { - $this->expectException(TypeError::class); + $this->expectException(InvalidArgumentException::class); - $bean = new Bean([ - 'value' => [ - 'parameters' => [ - new SampleService() - ] - ] - ]); + new Bean(scope: 3); } } diff --git a/tests/bitExpert/Disco/Annotations/ParameterUnitTest.php b/tests/bitExpert/Disco/Annotations/ParameterUnitTest.php index d958735..f04c0ed 100644 --- a/tests/bitExpert/Disco/Annotations/ParameterUnitTest.php +++ b/tests/bitExpert/Disco/Annotations/ParameterUnitTest.php @@ -12,32 +12,53 @@ namespace bitExpert\Disco\Annotations; -use Doctrine\Common\Annotations\AnnotationException; +use bitExpert\Disco\Attributes\Parameter; use PHPUnit\Framework\TestCase; +use Webmozart\Assert\InvalidArgumentException; /** - * Unit tests for {@link \bitExpert\Disco\Annotations\Parameter}. + * Unit tests for {@link \bitExpert\Disco\Attributes\Parameter}. */ class ParameterUnitTest extends TestCase { /** * @test */ - public function missingNameWillThrowAnnotationException(): void + public function emptyNameWillThrowAnnotationException(): void { - $this->expectException(AnnotationException::class); + $this->expectException(InvalidArgumentException::class); - new Parameter(); + new Parameter('', 'myParam'); } /** * @test */ - public function parameterNameIsParsed(): void + public function emptyKeyWillThrowAnnotationException(): void { - $parameter = new Parameter(['value' => ['name' => 'myParam']]); + $this->expectException(InvalidArgumentException::class); - self::assertSame('myParam', $parameter->getName()); + new Parameter('name', ''); + } + + /** + * @test + */ + public function nameIsSet(): void + { + $parameter = new Parameter(name: 'paramName', key: 'key'); + + self::assertSame('paramName', $parameter->getName()); + } + + /** + * @test + */ + public function keyIsSet(): void + { + $parameter = new Parameter(name: 'paramName', key: 'key'); + + self::assertSame('key', $parameter->getKey()); } /** @@ -45,7 +66,7 @@ public function parameterNameIsParsed(): void */ public function defaultValueDefaultsToNull(): void { - $parameter = new Parameter(['value' => ['name' => 'myParam']]); + $parameter = new Parameter(name: 'paramName', key: 'myParam'); self::assertNull($parameter->getDefaultValue()); } @@ -57,7 +78,7 @@ public function defaultValueDefaultsToNull(): void */ public function defaultValueIsParsed(mixed $defaultValue): void { - $parameter = new Parameter(['value' => ['name' => 'myParam', 'default' => $defaultValue]]); + $parameter = new Parameter(name: 'paramName', key: 'myParam', default: $defaultValue); self::assertSame($defaultValue, $parameter->getDefaultValue()); } @@ -67,7 +88,7 @@ public function defaultValueIsParsed(mixed $defaultValue): void */ public function requireDefaultsToTrue(): void { - $parameter = new Parameter(['value' => ['name' => 'myParam']]); + $parameter = new Parameter(name: 'paramName', key: 'myParam'); self::assertTrue($parameter->isRequired()); } @@ -79,7 +100,7 @@ public function requireDefaultsToTrue(): void */ public function requireIsParsed(bool $requireValue): void { - $parameter = new Parameter(['value' => ['name' => 'myParam', 'required' => $requireValue]]); + $parameter = new Parameter(name: 'paramName', key: 'myParam', required: $requireValue); self::assertSame($requireValue, $parameter->isRequired()); } diff --git a/tests/bitExpert/Disco/Config/BeanConfiguration.php b/tests/bitExpert/Disco/Config/BeanConfiguration.php index b20b447..0bc175e 100644 --- a/tests/bitExpert/Disco/Config/BeanConfiguration.php +++ b/tests/bitExpert/Disco/Config/BeanConfiguration.php @@ -12,92 +12,70 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Configuration; use bitExpert\Disco\Helper\InitializedService; use bitExpert\Disco\Helper\MasterService; use bitExpert\Disco\Helper\SampleService; -/** - * @Configuration - */ +#[Configuration] class BeanConfiguration { - /** - * @Bean({"singleton"=false, "lazy"=false, "scope"="session"}) - */ + #[Bean(singleton: false, lazy: false, scope: Bean::SCOPE_SESSION)] public function nonSingletonNonLazySessionBean(): MasterService { return new MasterService($this->nonSingletonNonLazyRequestBean()); } - /** - * @Bean({"singleton"=false, "lazy"=false, "scope"="request"}) - */ + #[Bean(singleton: false, lazy: false, scope: Bean::SCOPE_REQUEST)] public function nonSingletonNonLazyRequestBean(): SampleService { return new SampleService(); } - /** - * @Bean({"singleton"=false, "lazy"=true, "scope"="session"}) - */ + #[Bean(singleton: false, lazy: true, scope: Bean::SCOPE_SESSION)] public function nonSingletonLazySessionBean(): MasterService { return new MasterService($this->nonSingletonLazyRequestBean()); } - /** - * @Bean({"singleton"=false, "lazy"=true, "scope"="request"}) - */ + #[Bean(singleton: false, lazy: true, scope: Bean::SCOPE_REQUEST)] public function nonSingletonLazyRequestBean(): SampleService { return new SampleService(); } - /** - * @Bean({"singleton"=true, "lazy"=false, "scope"="session"}) - */ + #[Bean(singleton: true, lazy: false, scope: Bean::SCOPE_SESSION)] public function singletonNonLazySessionBean(): MasterService { return new MasterService($this->singletonNonLazyRequestBean()); } - /** - * @Bean({"singleton"=true, "lazy"=false, "scope"="request"}) - */ + #[Bean(singleton: true, lazy: false, scope: Bean::SCOPE_REQUEST)] public function singletonNonLazyRequestBean(): SampleService { return new SampleService(); } - /** - * @Bean({"singleton"=true, "lazy"=true, "scope"="session"}) - */ + #[Bean(singleton: true, lazy: true, scope: Bean::SCOPE_SESSION)] public function singletonLazySessionBean(): MasterService { return new MasterService($this->singletonLazyRequestBean()); } - /** - * @Bean({"singleton"=true, "lazy"=true, "scope"="request"}) - */ + #[Bean(singleton: true, lazy: true, scope: Bean::SCOPE_REQUEST)] public function singletonLazyRequestBean(): SampleService { return new SampleService(); } - /** - * @Bean({"singleton"=true, "lazy"=false, "scope"="request"}) - */ + #[Bean(singleton: true, lazy: false, scope: Bean::SCOPE_REQUEST)] public function singletonInitializedService(): InitializedService { return new InitializedService(); } - /** - * @Bean({"singleton"=true, "lazy"=true, "scope"="request"}) - */ + #[Bean(singleton: true, lazy: true, scope: Bean::SCOPE_REQUEST)] public function singletonLazyInitializedService(): InitializedService { return new InitializedService(); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationPersistence.php b/tests/bitExpert/Disco/Config/BeanConfigurationPersistence.php index d297c98..ebc088b 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationPersistence.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationPersistence.php @@ -12,18 +12,14 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Configuration; use bitExpert\Disco\Helper\SampleService; -/** - * @Configuration - */ +#[Configuration] class BeanConfigurationPersistence { - /** - * @Bean - */ + #[Bean] public function sampleService(): SampleService { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationSubclass.php b/tests/bitExpert/Disco/Config/BeanConfigurationSubclass.php index 65fffec..dff83ed 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationSubclass.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationSubclass.php @@ -12,18 +12,14 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Configuration; use bitExpert\Disco\Helper\MasterService; -/** - * @Configuration - */ +#[Configuration] class BeanConfigurationSubclass extends BeanConfiguration { - /** - * @Bean({"singleton"=true, "lazy"=false, "scope"="session"}) - */ + #[Bean(singleton: true, lazy: false, scope: Bean::SCOPE_SESSION)] public function singletonNonLazySessionBeanInSubclass(): MasterService { return new MasterService($this->singletonNonLazyRequestBean()); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationTrait.php b/tests/bitExpert/Disco/Config/BeanConfigurationTrait.php index a95347c..aa6fde3 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationTrait.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationTrait.php @@ -12,12 +12,10 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Configuration; use bitExpert\Disco\Config\Traits\NonSingletonNonLazyRequestBean; -/** - * @Configuration - */ +#[Configuration] class BeanConfigurationTrait extends BeanConfiguration { use NonSingletonNonLazyRequestBean; diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithAliases.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithAliases.php index dae3437..1897635 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithAliases.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithAliases.php @@ -12,54 +12,36 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Alias; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Alias; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Attributes\ReturnTypeAlias; use bitExpert\Disco\Helper\SampleService; use bitExpert\Disco\Helper\SampleServiceInterface; -/** - * @Configuration - */ +#[Configuration] class BeanConfigurationWithAliases { - /** - * @Bean({ - * "singelton"=true, - * "aliases"={ - * @Alias({"name" = "\my\Custom\Namespace"}), - * @Alias({"name" = "my::Custom::Namespace"}), - * @Alias({"name" = "Alias_With_Underscore"}), - * @Alias({"name" = "123456"}), - * @Alias({"type" = true}) - * } - * }) - */ + #[Bean(singleton: true)] + #[Alias(name: '\my\Custom\Namespace')] + #[Alias(name: 'my::Custom::Namespace')] + #[Alias(name: 'Alias_With_Underscore')] + #[Alias(name: '123456')] + #[ReturnTypeAlias] public function sampleServiceWithAliases(): SampleService { return new SampleService(); } - /** - * @Bean({ - * "aliases"={ - * @Alias({"type"=true}) - * } - * }) - * @return SampleServiceInterface - */ + #[Bean] + #[ReturnTypeAlias] public function sampleServiceWithInterfaceReturnTypeAlias(): SampleServiceInterface { return new SampleService(); } - /** - * @Bean({ - * "aliases"={ - * @Alias({"name"="aliasIsPublicForInternalService"}) - * } - * }) - */ + #[Bean] + #[Alias(name: 'aliasIsPublicForInternalService')] protected function internalServiceWithAlias(): SampleService { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliases.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliases.php index bcd36bd..e66ce31 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliases.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliases.php @@ -12,38 +12,24 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Alias; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Attributes\ReturnTypeAlias; use bitExpert\Disco\Helper\SampleService; use bitExpert\Disco\Helper\SampleServiceInterface; -/** - * @Configuration - */ +#[Configuration] class BeanConfigurationWithConflictingAliases { - /** - * @Bean({ - * "aliases"={ - * @Alias({"type"=true}) - * } - * }) - * @return SampleServiceInterface - */ + #[Bean] + #[ReturnTypeAlias] public function sampleService1(): SampleServiceInterface { return new SampleService(); } - /** - * @Bean({ - * "aliases"={ - * @Alias({"type"=true}) - * } - * }) - * @return SampleServiceInterface - */ + #[Bean] + #[ReturnTypeAlias] public function sampleService2(): SampleServiceInterface { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliasesInParentClass.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliasesInParentClass.php index 346ae25..e65952c 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliasesInParentClass.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliasesInParentClass.php @@ -12,25 +12,17 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Alias; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Alias; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Configuration; use bitExpert\Disco\Helper\SampleService; use bitExpert\Disco\Helper\SampleServiceInterface; -/** - * @Configuration - */ +#[Configuration] class BeanConfigurationWithConflictingAliasesInParentClass extends BeanConfigurationWithConflictingAliases { - /** - * @Bean({ - * "aliases"={ - * @Alias({"name"="SampleService3Alias"}) - * } - * }) - * @return SampleServiceInterface - */ + #[Bean] + #[Alias(name: 'SampleService3Alias')] public function sampleService3(): SampleServiceInterface { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithParameterizedPostProcessor.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithParameterizedPostProcessor.php index f60ab6a..f42053b 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithParameterizedPostProcessor.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithParameterizedPostProcessor.php @@ -12,33 +12,24 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\BeanPostProcessor; -use bitExpert\Disco\Annotations\Configuration; -use bitExpert\Disco\Annotations\Parameter; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\BeanPostProcessor; +use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Attributes\Parameter; use bitExpert\Disco\Helper\ParameterizedSampleServiceBeanPostProcessor; use bitExpert\Disco\Helper\SampleService; -/** - * @Configuration - */ +#[Configuration] class BeanConfigurationWithParameterizedPostProcessor { - /** - * @BeanPostProcessor({ - * "parameters"={ - * @Parameter({"name" = "test"}) - * } - * }) - */ + #[BeanPostProcessor] + #[Parameter(name: 'test', key: 'test')] public function sampleServiceBeanPostProcessor($test = ''): ParameterizedSampleServiceBeanPostProcessor { return new ParameterizedSampleServiceBeanPostProcessor($test); } - /** - * @Bean - */ + #[Bean] public function nonSingletonNonLazyRequestBean(): SampleService { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php index 5b8a1d8..3b7c697 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php @@ -12,24 +12,16 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\Configuration; -use bitExpert\Disco\Annotations\Parameter; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Attributes\Parameter; use bitExpert\Disco\Helper\SampleService; -/** - * @Configuration - */ +#[Configuration] class BeanConfigurationWithParameters { - /** - * @Bean({ - * "singleton"=false, - * "parameters"={ - * @Parameter({"name" = "test"}) - * } - * }) - */ + #[Bean(singleton: false)] + #[Parameter(name: 'test', key: 'test')] public function sampleServiceWithParam($test = ''): SampleService { $service = new SampleService(); @@ -37,14 +29,8 @@ public function sampleServiceWithParam($test = ''): SampleService return $service; } - /** - * @Bean({ - * "singleton"=false, - * "parameters"={ - * @Parameter({"name" = "test", "default" = null}) - * } - * }) - */ + #[Bean(singleton: false)] + #[Parameter(name: 'test', key: 'test', default: null)] public function sampleServiceWithParamNull($test = ''): SampleService { $service = new SampleService(); @@ -52,14 +38,8 @@ public function sampleServiceWithParamNull($test = ''): SampleService return $service; } - /** - * @Bean({ - * "singleton"=false, - * "parameters"={ - * @Parameter({"name" = "test", "default" = true}) - * } - * }) - */ + #[Bean(singleton: false)] + #[Parameter(name: 'test', key: 'test', default: true)] public function sampleServiceWithParamBool($test = ''): SampleService { $service = new SampleService(); @@ -67,14 +47,8 @@ public function sampleServiceWithParamBool($test = ''): SampleService return $service; } - /** - * @Bean({ - * "singleton"=false, - * "parameters"={ - * @Parameter({"name" = "test", "default" = 0}) - * } - * }) - */ + #[Bean(singleton: false)] + #[Parameter(name: 'test', key: 'test', default: 0)] public function sampleServiceWithParamEmpty($test = ''): SampleService { $service = new SampleService(); @@ -82,14 +56,8 @@ public function sampleServiceWithParamEmpty($test = ''): SampleService return $service; } - /** - * @Bean({ - * "singleton"=false, - * "parameters"={ - * @Parameter({"name" = "test.nested.key"}) - * } - * }) - */ + #[Bean(singleton: false)] + #[Parameter(name: 'test', key: 'test.nested.key')] public function sampleServiceWithNestedParamKey($test = ''): SampleService { $service = new SampleService(); @@ -97,14 +65,8 @@ public function sampleServiceWithNestedParamKey($test = ''): SampleService return $service; } - /** - * @Bean({ - * "singleton"=false, - * "parameters"={ - * @Parameter({"name" = "test", "default" = "myDefaultValue"}) - * } - * }) - */ + #[Bean(singleton: false)] + #[Parameter(name: 'test', key: 'test', default: 'myDefaultValue')] public function sampleServiceWithParamDefaultValue($test = ''): SampleService { $service = new SampleService(); @@ -112,14 +74,8 @@ public function sampleServiceWithParamDefaultValue($test = ''): SampleService return $service; } - /** - * @Bean({ - * "singleton"=false, - * "parameters"={ - * @Parameter({"name" = "test", "required" = false}) - * } - * }) - */ + #[Bean(singleton: false)] + #[Parameter(name: 'test', key: 'test', required: false)] public function sampleServiceWithoutRequiredParam($test = ''): SampleService { $service = new SampleService(); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessor.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessor.php index 1cb945b..58bcd92 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessor.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessor.php @@ -12,38 +12,28 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\BeanPostProcessor; -use bitExpert\Disco\Annotations\Configuration; -use bitExpert\Disco\BeanFactoryPostProcessor; -use bitExpert\Disco\Helper\BeanFactoryAwareService; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\BeanPostProcessor; +use bitExpert\Disco\Attributes\Configuration; use bitExpert\Disco\Helper\SampleService; use bitExpert\Disco\Helper\SampleServiceBeanPostProcessor; -/** - * @Configuration - */ +#[Configuration] class BeanConfigurationWithPostProcessor { - /** - * @BeanPostProcessor - */ + #[BeanPostProcessor] public function sampleServiceBeanPostProcessor(): SampleServiceBeanPostProcessor { return new SampleServiceBeanPostProcessor(); } - /** - * @Bean - */ + #[Bean] public function nonSingletonNonLazyRequestBean(): SampleService { return new SampleService(); } - /** - * @Bean({"lazy"=true}) - */ + #[Bean(lazy: true)] public function nonSingletonLazyRequestBean(): SampleService { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessorAndParameterizedDependency.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessorAndParameterizedDependency.php index 449a710..3f75596 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessorAndParameterizedDependency.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessorAndParameterizedDependency.php @@ -12,33 +12,24 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\BeanPostProcessor; -use bitExpert\Disco\Annotations\Configuration; -use bitExpert\Disco\Annotations\Parameter; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\BeanPostProcessor; +use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Attributes\Parameter; use bitExpert\Disco\Helper\ParameterizedSampleServiceBeanPostProcessor; use bitExpert\Disco\Helper\SampleService; -/** - * @Configuration - */ +#[Configuration] class BeanConfigurationWithPostProcessorAndParameterizedDependency { - /** - * @BeanPostProcessor - */ + #[BeanPostProcessor] public function sampleServiceBeanPostProcessor(): ParameterizedSampleServiceBeanPostProcessor { return new ParameterizedSampleServiceBeanPostProcessor($this->dependency()); } - /** - * @Bean({ - * "parameters"={ - * @Parameter({"name" = "test"}) - * } - * }) - */ + #[Bean] + #[Parameter(name: 'test', key: 'test')] public function dependency($test = ''): \stdClass { $object = new \stdClass(); @@ -46,9 +37,7 @@ public function dependency($test = ''): \stdClass return $object; } - /** - * @Bean - */ + #[Bean] public function nonSingletonNonLazyRequestBean(): SampleService { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithPrimitives.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithPrimitives.php index 19c262d..59c3ee5 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithPrimitives.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithPrimitives.php @@ -12,60 +12,45 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\Configuration; -use bitExpert\Disco\Annotations\Parameters; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Configuration; use bitExpert\Disco\Helper\SampleService; -/** - * @Configuration - */ +#[Configuration] class BeanConfigurationWithPrimitives { - /** - * @Bean - */ + #[Bean] public function arrayPrimitive(): array { return []; } - /** - * @Bean - */ + #[Bean] public function callablePrimitive(): callable { return function () { }; } - /** - * @Bean - */ + #[Bean] public function boolPrimitive(): bool { return true; } - /** - * @Bean - */ + #[Bean] public function floatPrimitive(): float { return 1.23; } - /** - * @Bean - */ + #[Bean] public function intPrimitive(): int { return 5; } - /** - * @Bean - */ + #[Bean] public function serviceWithStringInjected(): SampleService { $service = new SampleService(); @@ -73,9 +58,7 @@ public function serviceWithStringInjected(): SampleService return $service; } - /** - * @Bean - */ + #[Bean] public function stringPrimitive(): string { return 'Disco'; diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithProtectedMethod.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithProtectedMethod.php index 4f7caa5..c157052 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithProtectedMethod.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithProtectedMethod.php @@ -12,43 +12,33 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Configuration; use bitExpert\Disco\Helper\MasterService; use bitExpert\Disco\Helper\SampleService; -/** - * @Configuration - */ +#[Configuration] class BeanConfigurationWithProtectedMethod { - /** - * @Bean({"singleton"=false}) - */ + #[Bean(singleton: false)] public function masterServiceWithSingletonDependency(): MasterService { return new MasterService($this->singletonDependency()); } - /** - * @Bean({"singleton"=true}) - */ + #[Bean(singleton: true)] protected function singletonDependency(): SampleService { return new SampleService(); } - /** - * @Bean({"singleton"=false}) - */ + #[Bean(singleton: false)] public function masterServiceWithNonSingletonDependency(): MasterService { return new MasterService($this->nonSingletonDependency()); } - /** - * @Bean({"singleton"=false}) - */ + #[Bean(singleton: false)] protected function nonSingletonDependency(): SampleService { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/ExtendedBeanConfigurationOverwritingParentAlias.php b/tests/bitExpert/Disco/Config/ExtendedBeanConfigurationOverwritingParentAlias.php index 4a5467a..5d0bc1f 100644 --- a/tests/bitExpert/Disco/Config/ExtendedBeanConfigurationOverwritingParentAlias.php +++ b/tests/bitExpert/Disco/Config/ExtendedBeanConfigurationOverwritingParentAlias.php @@ -12,25 +12,17 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Alias; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Attributes\ReturnTypeAlias; use bitExpert\Disco\Helper\SampleService; use bitExpert\Disco\Helper\SampleServiceInterface; -/** - * @Configuration - */ +#[Configuration] class ExtendedBeanConfigurationOverwritingParentAlias extends BeanConfigurationWithAliases { - /** - * @Bean({ - * "aliases"={ - * @Alias({"type"=true}) - * } - * }) - * @return SampleServiceInterface - */ + #[Bean] + #[ReturnTypeAlias] public function extendedSampleServiceWithInterfaceReturnTypeAlias(): SampleServiceInterface { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/InterfaceConfiguration.php b/tests/bitExpert/Disco/Config/InterfaceConfiguration.php index 0c2a01b..46b6282 100644 --- a/tests/bitExpert/Disco/Config/InterfaceConfiguration.php +++ b/tests/bitExpert/Disco/Config/InterfaceConfiguration.php @@ -12,11 +12,9 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Configuration; -/** - * @Configuration - */ +#[Configuration] interface InterfaceConfiguration { } diff --git a/tests/bitExpert/Disco/Config/InvalidConfiguration.php b/tests/bitExpert/Disco/Config/InvalidConfiguration.php index f87ca68..4c6db1d 100644 --- a/tests/bitExpert/Disco/Config/InvalidConfiguration.php +++ b/tests/bitExpert/Disco/Config/InvalidConfiguration.php @@ -13,7 +13,7 @@ namespace bitExpert\Disco\Config; /** - * Configuration class missing the needed annotation. + * Configuration class missing the needed attribute. */ class InvalidConfiguration { diff --git a/tests/bitExpert/Disco/Config/MissingBeanAnnotationConfiguration.php b/tests/bitExpert/Disco/Config/MissingBeanAnnotationConfiguration.php index 4e8a7e4..66fbc45 100644 --- a/tests/bitExpert/Disco/Config/MissingBeanAnnotationConfiguration.php +++ b/tests/bitExpert/Disco/Config/MissingBeanAnnotationConfiguration.php @@ -12,12 +12,10 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Configuration; use bitExpert\Disco\Helper\SampleService; -/** - * @Configuration - */ +#[Configuration] class MissingBeanAnnotationConfiguration { public function nonSingletonNonLazyRequestBean() diff --git a/tests/bitExpert/Disco/Config/MissingReturnTypeConfiguration.php b/tests/bitExpert/Disco/Config/MissingReturnTypeConfiguration.php index 72c385f..6f9ab04 100644 --- a/tests/bitExpert/Disco/Config/MissingReturnTypeConfiguration.php +++ b/tests/bitExpert/Disco/Config/MissingReturnTypeConfiguration.php @@ -12,18 +12,14 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Configuration; use bitExpert\Disco\Helper\SampleService; -/** - * @Configuration - */ +#[Configuration] class MissingReturnTypeConfiguration { - /** - * @Bean({"singleton"=false, "lazy"=false, "scope"="request"}) - */ + #[Bean(singleton: false, lazy: false, scope: Bean::SCOPE_REQUEST)] public function nonSingletonNonLazyRequestBean() { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/NonExistentReturnTypeConfiguration.php b/tests/bitExpert/Disco/Config/NonExistentReturnTypeConfiguration.php index 514c641..4ed0891 100644 --- a/tests/bitExpert/Disco/Config/NonExistentReturnTypeConfiguration.php +++ b/tests/bitExpert/Disco/Config/NonExistentReturnTypeConfiguration.php @@ -12,18 +12,14 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Configuration; use bitExpert\Disco\Helper\SampleService; -/** - * @Configuration - */ +#[Configuration] class NonExistentReturnTypeConfiguration { - /** - * @Bean({"singleton"=false, "lazy"=false, "scope"="request"}) - */ + #[Bean(singleton: false, lazy: false, scope: Bean::SCOPE_REQUEST)] public function nonSingletonNonLazyRequestBean(): \MyOtherClass { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/Traits/NonSingletonNonLazyRequestBean.php b/tests/bitExpert/Disco/Config/Traits/NonSingletonNonLazyRequestBean.php index 8bd2df4..521036d 100644 --- a/tests/bitExpert/Disco/Config/Traits/NonSingletonNonLazyRequestBean.php +++ b/tests/bitExpert/Disco/Config/Traits/NonSingletonNonLazyRequestBean.php @@ -12,14 +12,12 @@ namespace bitExpert\Disco\Config\Traits; -use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Attributes\Bean; use bitExpert\Disco\Helper\SampleService; trait NonSingletonNonLazyRequestBean { - /** - * @Bean({"singleton"=false, "lazy"=false, "scope"="request"}) - */ + #[Bean(singleton: false, lazy: false, scope: Bean::SCOPE_REQUEST)] public function nonSingletonNonLazyRequestBeanInTrait(): SampleService { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/WrongReturnTypeConfiguration.php b/tests/bitExpert/Disco/Config/WrongReturnTypeConfiguration.php index 8bf7d79..5e90562 100644 --- a/tests/bitExpert/Disco/Config/WrongReturnTypeConfiguration.php +++ b/tests/bitExpert/Disco/Config/WrongReturnTypeConfiguration.php @@ -12,41 +12,31 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Annotations\Bean; -use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Attributes\Configuration; use bitExpert\Disco\Helper\MasterService; use bitExpert\Disco\Helper\SampleService; -/** - * @Configuration - */ +#[Configuration] class WrongReturnTypeConfiguration { - /** - * @Bean({"singleton"=false, "lazy"=false, "scope"="request"}) - */ + #[Bean(singleton: false, lazy: false, scope: Bean::SCOPE_REQUEST)] public function nonLazyBeanNotReturningAnything(): SampleService { } - /** - * @Bean({"singleton"=false, "lazy"=false, "scope"="request"}) - */ + #[Bean(singleton: false, lazy: false, scope: Bean::SCOPE_REQUEST)] public function nonLazyBeanReturningSomethingWrong(): SampleService { return new MasterService(new SampleService()); } - /** - * @Bean({"singleton"=false, "lazy"=true, "scope"="request"}) - */ + #[Bean(singleton: false, lazy: true, scope: Bean::SCOPE_REQUEST)] public function lazyBeanNotReturningAnything(): SampleService { } - /** - * @Bean({"singleton"=false, "lazy"=true, "scope"="request"}) - */ + #[Bean(singleton: false, lazy: true, scope: Bean::SCOPE_REQUEST)] public function lazyBeanReturningSomethingWrong(): SampleService { return new MasterService(new SampleService()); diff --git a/tests/bitExpert/Disco/Proxy/Configuration/ConfigurationGeneratorUnitTest.php b/tests/bitExpert/Disco/Proxy/Configuration/ConfigurationGeneratorUnitTest.php index 4c43e76..b701110 100644 --- a/tests/bitExpert/Disco/Proxy/Configuration/ConfigurationGeneratorUnitTest.php +++ b/tests/bitExpert/Disco/Proxy/Configuration/ConfigurationGeneratorUnitTest.php @@ -16,14 +16,12 @@ use bitExpert\Disco\Config\BeanConfigurationWithConflictingAliases; use bitExpert\Disco\Config\BeanConfigurationWithConflictingAliasesInParentClass; use bitExpert\Disco\Config\ExtendedBeanConfigurationOverwritingParentAlias; -use bitExpert\Disco\Config\BeanConfigurationWithNativeTypeAlias; use bitExpert\Disco\Config\InterfaceConfiguration; use bitExpert\Disco\Config\InvalidConfiguration; use bitExpert\Disco\Config\MissingBeanAnnotationConfiguration; use bitExpert\Disco\Config\MissingReturnTypeConfiguration; use bitExpert\Disco\Config\NonExistentReturnTypeConfiguration; use PHPUnit\Framework\TestCase; -use PHPUnit_Framework_MockObject_MockObject; use ProxyManager\Exception\InvalidProxiedClassException; use Laminas\Code\Generator\ClassGenerator; @@ -124,10 +122,10 @@ public function sameAliasUsedForMultipleBeansThrowsException(): void /** * @test */ - public function unknownAnnotationThrowsException(): void + public function missingConfigurationAttributeThrowsException(): void { $this->expectException(InvalidProxiedClassException::class); - $this->expectExceptionMessageMatches('/^\[Semantical Error\] The annotation "@foo"/'); + $this->expectExceptionMessageMatches('/#\[Configuration\] attribute missing!/'); /** * @foo @@ -158,7 +156,7 @@ public function parsingConfigurationWithoutAnyErrorsSucceeds(): void /** * @test */ - public function subclassedConfigurationIsAllowedToOverrwriteParentAlias(): void + public function subclassedConfigurationIsAllowedToOverwriteParentAlias(): void { $this->classGenerator->expects(self::atLeastOnce()) ->method('addMethodFromGenerator'); From 0efd86c959f3ab9c08bdbfabd6349f05df140ff9 Mon Sep 17 00:00:00 2001 From: Zacharias Luiten Date: Tue, 17 Aug 2021 12:37:22 +0200 Subject: [PATCH 2/3] - Termporary? revert attribute namespace to \bitExpert\Disco\Annotations (for clearer diff) - Updated docs - Implement v2 of psr/container --- README.md | 18 +- Upgrade.md | 8 +- composer.json | 5 +- composer.lock | 486 ++++++------------ docs/advanced/bean-post-processor.md | 18 +- docs/advanced/structure-config.md | 32 +- docs/basic/basic-concepts.md | 45 +- docs/basic/bean-config.md | 116 ++--- docs/basic/bean-parameters.md | 88 ++-- docs/basic/injecting-dependencies.md | 4 +- docs/intro/getting-started.md | 13 +- docs/intro/what-is-disco.md | 2 +- src/bitExpert/Disco/AnnotationBeanFactory.php | 8 +- .../{Attributes => Annotations}/Alias.php | 3 +- .../{Attributes => Annotations}/Bean.php | 2 +- .../BeanPostProcessor.php | 2 +- .../Configuration.php | 2 +- .../{Attributes => Annotations}/Parameter.php | 2 +- .../TypeAlias.php} | 4 +- .../Disco/BeanFactoryConfiguration.php | 17 +- .../Configuration/ConfigurationGenerator.php | 45 +- .../MethodGenerator/BeanMethod.php | 21 +- .../BeanPostProcessorMethod.php | 8 +- .../ParameterAwareMethodGenerator.php | 2 +- .../Disco/Annotations/AliasUnitTest.php | 4 +- .../Disco/Annotations/BeanUnitTest.php | 4 +- .../Disco/Annotations/ParameterUnitTest.php | 4 +- .../Disco/Config/BeanConfiguration.php | 4 +- .../Config/BeanConfigurationPersistence.php | 4 +- .../Config/BeanConfigurationSubclass.php | 4 +- .../Disco/Config/BeanConfigurationTrait.php | 2 +- .../Config/BeanConfigurationWithAliases.php | 12 +- ...eanConfigurationWithConflictingAliases.php | 10 +- ...ionWithConflictingAliasesInParentClass.php | 8 +- ...gurationWithParameterizedPostProcessor.php | 8 +- .../BeanConfigurationWithParameters.php | 6 +- .../BeanConfigurationWithPostProcessor.php | 6 +- ...ostProcessorAndParameterizedDependency.php | 8 +- .../BeanConfigurationWithPrimitives.php | 4 +- .../BeanConfigurationWithProtectedMethod.php | 4 +- ...eanConfigurationOverwritingParentAlias.php | 8 +- .../Disco/Config/InterfaceConfiguration.php | 2 +- .../MissingBeanAnnotationConfiguration.php | 2 +- .../Config/MissingReturnTypeConfiguration.php | 4 +- .../NonExistentReturnTypeConfiguration.php | 4 +- .../Traits/NonSingletonNonLazyRequestBean.php | 2 +- .../Config/WrongReturnTypeConfiguration.php | 4 +- 47 files changed, 429 insertions(+), 640 deletions(-) rename src/bitExpert/Disco/{Attributes => Annotations}/Alias.php (88%) rename src/bitExpert/Disco/{Attributes => Annotations}/Bean.php (97%) rename src/bitExpert/Disco/{Attributes => Annotations}/BeanPostProcessor.php (91%) rename src/bitExpert/Disco/{Attributes => Annotations}/Configuration.php (91%) rename src/bitExpert/Disco/{Attributes => Annotations}/Parameter.php (98%) rename src/bitExpert/Disco/{Attributes/ReturnTypeAlias.php => Annotations/TypeAlias.php} (87%) diff --git a/README.md b/README.md index fa88bad..2306675 100644 --- a/README.md +++ b/README.md @@ -32,11 +32,9 @@ Next up you need to create a configuration class `MyConfiguration` and document ```php =7.2.0" }, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, "autoload": { "psr-4": { "Psr\\Container\\": "src/" @@ -390,22 +263,22 @@ ], "support": { "issues": "https://github.com/php-fig/container/issues", - "source": "https://github.com/php-fig/container/tree/1.1.1" + "source": "https://github.com/php-fig/container/tree/2.0.1" }, - "time": "2021-03-05T17:36:06+00:00" + "time": "2021-03-24T13:40:57+00:00" }, { "name": "psr/log", - "version": "1.1.3", + "version": "1.1.4", "source": { "type": "git", "url": "https://github.com/php-fig/log.git", - "reference": "0f73288fd15629204f9d42b7055f72dacbe811fc" + "reference": "d49695b909c3b7628b6289db5479a1c204601f11" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/log/zipball/0f73288fd15629204f9d42b7055f72dacbe811fc", - "reference": "0f73288fd15629204f9d42b7055f72dacbe811fc", + "url": "https://api.github.com/repos/php-fig/log/zipball/d49695b909c3b7628b6289db5479a1c204601f11", + "reference": "d49695b909c3b7628b6289db5479a1c204601f11", "shasum": "" }, "require": { @@ -429,7 +302,7 @@ "authors": [ { "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" + "homepage": "https://www.php-fig.org/" } ], "description": "Common interface for logging libraries", @@ -440,22 +313,22 @@ "psr-3" ], "support": { - "source": "https://github.com/php-fig/log/tree/1.1.3" + "source": "https://github.com/php-fig/log/tree/1.1.4" }, - "time": "2020-03-23T09:12:05+00:00" + "time": "2021-05-03T11:20:27+00:00" }, { "name": "symfony/polyfill-ctype", - "version": "v1.22.1", + "version": "v1.23.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "c6c942b1ac76c82448322025e084cadc56048b4e" + "reference": "46cd95797e9df938fdd2b03693b5fca5e64b01ce" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/c6c942b1ac76c82448322025e084cadc56048b4e", - "reference": "c6c942b1ac76c82448322025e084cadc56048b4e", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/46cd95797e9df938fdd2b03693b5fca5e64b01ce", + "reference": "46cd95797e9df938fdd2b03693b5fca5e64b01ce", "shasum": "" }, "require": { @@ -467,7 +340,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "1.22-dev" + "dev-main": "1.23-dev" }, "thanks": { "name": "symfony/polyfill", @@ -505,7 +378,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.22.1" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.23.0" }, "funding": [ { @@ -521,35 +394,35 @@ "type": "tidelift" } ], - "time": "2021-01-07T16:49:33+00:00" + "time": "2021-02-19T12:13:01+00:00" }, { "name": "webimpress/safe-writer", - "version": "2.1.0", + "version": "2.2.0", "source": { "type": "git", "url": "https://github.com/webimpress/safe-writer.git", - "reference": "5cfafdec5873c389036f14bf832a5efc9390dcdd" + "reference": "9d37cc8bee20f7cb2f58f6e23e05097eab5072e6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webimpress/safe-writer/zipball/5cfafdec5873c389036f14bf832a5efc9390dcdd", - "reference": "5cfafdec5873c389036f14bf832a5efc9390dcdd", + "url": "https://api.github.com/repos/webimpress/safe-writer/zipball/9d37cc8bee20f7cb2f58f6e23e05097eab5072e6", + "reference": "9d37cc8bee20f7cb2f58f6e23e05097eab5072e6", "shasum": "" }, "require": { - "php": "^7.2 || ^8.0" + "php": "^7.3 || ^8.0" }, "require-dev": { - "phpunit/phpunit": "^8.5.8 || ^9.3.7", - "vimeo/psalm": "^3.14.2", - "webimpress/coding-standard": "^1.1.5" + "phpunit/phpunit": "^9.5.4", + "vimeo/psalm": "^4.7", + "webimpress/coding-standard": "^1.2.2" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "2.1.x-dev", - "dev-develop": "2.2.x-dev", + "dev-master": "2.2.x-dev", + "dev-develop": "2.3.x-dev", "dev-release-1.0": "1.0.x-dev" } }, @@ -572,7 +445,7 @@ ], "support": { "issues": "https://github.com/webimpress/safe-writer/issues", - "source": "https://github.com/webimpress/safe-writer/tree/master" + "source": "https://github.com/webimpress/safe-writer/tree/2.2.0" }, "funding": [ { @@ -580,7 +453,7 @@ "type": "github" } ], - "time": "2020-08-25T07:21:11+00:00" + "time": "2021-04-19T16:34:45+00:00" }, { "name": "webmozart/assert", @@ -834,23 +707,22 @@ }, { "name": "bookdown/bookdown", - "version": "1.x-dev", + "version": "2.x-dev", "source": { "type": "git", "url": "https://github.com/bookdown/Bookdown.Bookdown.git", - "reference": "12037a2460ef13d37e0681430af9a55c851d84e5" + "reference": "853534f823fe86802b4108dc0fb902a660261650" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/bookdown/Bookdown.Bookdown/zipball/12037a2460ef13d37e0681430af9a55c851d84e5", - "reference": "12037a2460ef13d37e0681430af9a55c851d84e5", + "url": "https://api.github.com/repos/bookdown/Bookdown.Bookdown/zipball/853534f823fe86802b4108dc0fb902a660261650", + "reference": "853534f823fe86802b4108dc0fb902a660261650", "shasum": "" }, "require": { "aura/cli": "~2.0", "aura/html": "~2.0", "aura/view": "~2.0", - "bookdown/themes": "~1.0", "league/commonmark": "~0.0", "php": ">=5.6.0", "psr/log": "~1.0" @@ -861,7 +733,6 @@ "webuni/commonmark-attributes-extension": "~0.5", "webuni/commonmark-table-extension": "~0.6" }, - "default-branch": true, "bin": [ "bin/bookdown" ], @@ -892,54 +763,9 @@ ], "support": { "issues": "https://github.com/bookdown/Bookdown.Bookdown/issues", - "source": "https://github.com/bookdown/Bookdown.Bookdown/tree/1.1.1" - }, - "time": "2019-04-23T15:53:49+00:00" - }, - { - "name": "bookdown/themes", - "version": "1.1.4", - "source": { - "type": "git", - "url": "https://github.com/bookdown/Bookdown.Themes.git", - "reference": "cf4986fcef40df4db1504bb62369d37a9e8bf6e7" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/bookdown/Bookdown.Themes/zipball/cf4986fcef40df4db1504bb62369d37a9e8bf6e7", - "reference": "cf4986fcef40df4db1504bb62369d37a9e8bf6e7", - "shasum": "" - }, - "type": "library", - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Bookdown Bootswatch Templates Contributors", - "homepage": "https://github.com/bookdown/Bookdown.Themes/contributors" - } - ], - "description": "Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting", - "homepage": "https://github.com/bookdown/Bookdown.Themes", - "keywords": [ - "bookdown", - "docbook", - "docs", - "documentation", - "highlighting", - "manual", - "markdown", - "static site", - "syntax", - "templates" - ], - "support": { - "issues": "https://github.com/bookdown/Bookdown.Themes/issues", - "source": "https://github.com/bookdown/Bookdown.Themes/tree/master" + "source": "https://github.com/bookdown/Bookdown.Bookdown/tree/1.x" }, - "time": "2018-08-10T15:21:32+00:00" + "time": "2017-07-29T18:05:10+00:00" }, { "name": "doctrine/instantiator", @@ -1087,16 +913,16 @@ }, { "name": "mikey179/vfsstream", - "version": "v1.6.8", + "version": "v1.6.9", "source": { "type": "git", "url": "https://github.com/bovigo/vfsStream.git", - "reference": "231c73783ebb7dd9ec77916c10037eff5a2b6efe" + "reference": "2257e326dc3d0f50e55d0a90f71e37899f029718" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/bovigo/vfsStream/zipball/231c73783ebb7dd9ec77916c10037eff5a2b6efe", - "reference": "231c73783ebb7dd9ec77916c10037eff5a2b6efe", + "url": "https://api.github.com/repos/bovigo/vfsStream/zipball/2257e326dc3d0f50e55d0a90f71e37899f029718", + "reference": "2257e326dc3d0f50e55d0a90f71e37899f029718", "shasum": "" }, "require": { @@ -1134,20 +960,20 @@ "source": "https://github.com/bovigo/vfsStream/tree/master", "wiki": "https://github.com/bovigo/vfsStream/wiki" }, - "time": "2019-10-30T15:31:00+00:00" + "time": "2021-07-16T08:08:02+00:00" }, { "name": "monolog/monolog", - "version": "2.2.0", + "version": "2.3.2", "source": { "type": "git", "url": "https://github.com/Seldaek/monolog.git", - "reference": "1cb1cde8e8dd0f70cc0fe51354a59acad9302084" + "reference": "71312564759a7db5b789296369c1a264efc43aad" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Seldaek/monolog/zipball/1cb1cde8e8dd0f70cc0fe51354a59acad9302084", - "reference": "1cb1cde8e8dd0f70cc0fe51354a59acad9302084", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/71312564759a7db5b789296369c1a264efc43aad", + "reference": "71312564759a7db5b789296369c1a264efc43aad", "shasum": "" }, "require": { @@ -1166,7 +992,7 @@ "php-amqplib/php-amqplib": "~2.4", "php-console/php-console": "^3.1.3", "phpspec/prophecy": "^1.6.1", - "phpstan/phpstan": "^0.12.59", + "phpstan/phpstan": "^0.12.91", "phpunit/phpunit": "^8.5", "predis/predis": "^1.1", "rollbar/rollbar": "^1.3", @@ -1218,7 +1044,7 @@ ], "support": { "issues": "https://github.com/Seldaek/monolog/issues", - "source": "https://github.com/Seldaek/monolog/tree/2.2.0" + "source": "https://github.com/Seldaek/monolog/tree/2.3.2" }, "funding": [ { @@ -1230,7 +1056,7 @@ "type": "tidelift" } ], - "time": "2020-12-14T13:15:25+00:00" + "time": "2021-07-23T07:42:52+00:00" }, { "name": "myclabs/deep-copy", @@ -1292,16 +1118,16 @@ }, { "name": "nikic/php-parser", - "version": "v4.10.4", + "version": "v4.12.0", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "c6d052fc58cb876152f89f532b95a8d7907e7f0e" + "reference": "6608f01670c3cc5079e18c1dab1104e002579143" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/c6d052fc58cb876152f89f532b95a8d7907e7f0e", - "reference": "c6d052fc58cb876152f89f532b95a8d7907e7f0e", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/6608f01670c3cc5079e18c1dab1104e002579143", + "reference": "6608f01670c3cc5079e18c1dab1104e002579143", "shasum": "" }, "require": { @@ -1342,22 +1168,22 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.10.4" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.12.0" }, - "time": "2020-12-20T10:01:03+00:00" + "time": "2021-07-21T10:44:31+00:00" }, { "name": "phar-io/manifest", - "version": "2.0.1", + "version": "2.0.3", "source": { "type": "git", "url": "https://github.com/phar-io/manifest.git", - "reference": "85265efd3af7ba3ca4b2a2c34dbfc5788dd29133" + "reference": "97803eca37d319dfa7826cc2437fc020857acb53" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phar-io/manifest/zipball/85265efd3af7ba3ca4b2a2c34dbfc5788dd29133", - "reference": "85265efd3af7ba3ca4b2a2c34dbfc5788dd29133", + "url": "https://api.github.com/repos/phar-io/manifest/zipball/97803eca37d319dfa7826cc2437fc020857acb53", + "reference": "97803eca37d319dfa7826cc2437fc020857acb53", "shasum": "" }, "require": { @@ -1402,9 +1228,9 @@ "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", "support": { "issues": "https://github.com/phar-io/manifest/issues", - "source": "https://github.com/phar-io/manifest/tree/master" + "source": "https://github.com/phar-io/manifest/tree/2.0.3" }, - "time": "2020-06-27T14:33:11+00:00" + "time": "2021-07-20T11:28:43+00:00" }, { "name": "phar-io/version", @@ -1617,16 +1443,16 @@ }, { "name": "phpspec/prophecy", - "version": "1.12.2", + "version": "1.13.0", "source": { "type": "git", "url": "https://github.com/phpspec/prophecy.git", - "reference": "245710e971a030f42e08f4912863805570f23d39" + "reference": "be1996ed8adc35c3fd795488a653f4b518be70ea" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/245710e971a030f42e08f4912863805570f23d39", - "reference": "245710e971a030f42e08f4912863805570f23d39", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/be1996ed8adc35c3fd795488a653f4b518be70ea", + "reference": "be1996ed8adc35c3fd795488a653f4b518be70ea", "shasum": "" }, "require": { @@ -1678,9 +1504,9 @@ ], "support": { "issues": "https://github.com/phpspec/prophecy/issues", - "source": "https://github.com/phpspec/prophecy/tree/1.12.2" + "source": "https://github.com/phpspec/prophecy/tree/1.13.0" }, - "time": "2020-12-19T10:15:11+00:00" + "time": "2021-03-17T13:42:18+00:00" }, { "name": "phpstan/extension-installer", @@ -1729,16 +1555,16 @@ }, { "name": "phpstan/phpstan", - "version": "0.12.81", + "version": "0.12.94", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "0dd5b0ebeff568f7000022ea5f04aa86ad3124b8" + "reference": "3d0ba4c198a24e3c3fc489f3ec6ac9612c4be5d6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/0dd5b0ebeff568f7000022ea5f04aa86ad3124b8", - "reference": "0dd5b0ebeff568f7000022ea5f04aa86ad3124b8", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/3d0ba4c198a24e3c3fc489f3ec6ac9612c4be5d6", + "reference": "3d0ba4c198a24e3c3fc489f3ec6ac9612c4be5d6", "shasum": "" }, "require": { @@ -1769,13 +1595,17 @@ "description": "PHPStan - PHP Static Analysis Tool", "support": { "issues": "https://github.com/phpstan/phpstan/issues", - "source": "https://github.com/phpstan/phpstan/tree/0.12.81" + "source": "https://github.com/phpstan/phpstan/tree/0.12.94" }, "funding": [ { "url": "https://github.com/ondrejmirtes", "type": "github" }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, { "url": "https://www.patreon.com/phpstan", "type": "patreon" @@ -1785,34 +1615,33 @@ "type": "tidelift" } ], - "time": "2021-03-08T22:03:02+00:00" + "time": "2021-07-30T09:05:27+00:00" }, { "name": "phpstan/phpstan-phpunit", - "version": "0.12.18", + "version": "0.12.22", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan-phpunit.git", - "reference": "ab44aec7cfb5cb267b8bc30a8caea86dd50d1f72" + "reference": "7c01ef93bf128b4ac8bdad38c54b2a4fd6b0b3cc" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan-phpunit/zipball/ab44aec7cfb5cb267b8bc30a8caea86dd50d1f72", - "reference": "ab44aec7cfb5cb267b8bc30a8caea86dd50d1f72", + "url": "https://api.github.com/repos/phpstan/phpstan-phpunit/zipball/7c01ef93bf128b4ac8bdad38c54b2a4fd6b0b3cc", + "reference": "7c01ef93bf128b4ac8bdad38c54b2a4fd6b0b3cc", "shasum": "" }, "require": { "php": "^7.1 || ^8.0", - "phpstan/phpstan": "^0.12.60" + "phpstan/phpstan": "^0.12.92" }, "conflict": { "phpunit/phpunit": "<7.0" }, "require-dev": { - "phing/phing": "^2.16.3", "php-parallel-lint/php-parallel-lint": "^1.2", "phpstan/phpstan-strict-rules": "^0.12.6", - "phpunit/phpunit": "^7.5.20" + "phpunit/phpunit": "^9.5" }, "type": "phpstan-extension", "extra": { @@ -1838,22 +1667,22 @@ "description": "PHPUnit extensions and rules for PHPStan", "support": { "issues": "https://github.com/phpstan/phpstan-phpunit/issues", - "source": "https://github.com/phpstan/phpstan-phpunit/tree/0.12.18" + "source": "https://github.com/phpstan/phpstan-phpunit/tree/0.12.22" }, - "time": "2021-03-06T11:51:27+00:00" + "time": "2021-08-12T10:53:43+00:00" }, { "name": "phpstan/phpstan-strict-rules", - "version": "0.12.9", + "version": "0.12.10", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan-strict-rules.git", - "reference": "0705fefc7c9168529fd130e341428f5f10f4f01d" + "reference": "ce82a050f9da4a32e438272907e38cdd04806223" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan-strict-rules/zipball/0705fefc7c9168529fd130e341428f5f10f4f01d", - "reference": "0705fefc7c9168529fd130e341428f5f10f4f01d", + "url": "https://api.github.com/repos/phpstan/phpstan-strict-rules/zipball/ce82a050f9da4a32e438272907e38cdd04806223", + "reference": "ce82a050f9da4a32e438272907e38cdd04806223", "shasum": "" }, "require": { @@ -1861,10 +1690,9 @@ "phpstan/phpstan": "^0.12.66" }, "require-dev": { - "phing/phing": "^2.16.3", "php-parallel-lint/php-parallel-lint": "^1.2", "phpstan/phpstan-phpunit": "^0.12.16", - "phpunit/phpunit": "^7.5.20" + "phpunit/phpunit": "^9.5" }, "type": "phpstan-extension", "extra": { @@ -1889,22 +1717,22 @@ "description": "Extra strict and opinionated rules for PHPStan", "support": { "issues": "https://github.com/phpstan/phpstan-strict-rules/issues", - "source": "https://github.com/phpstan/phpstan-strict-rules/tree/0.12.9" + "source": "https://github.com/phpstan/phpstan-strict-rules/tree/0.12.10" }, - "time": "2021-01-13T08:50:28+00:00" + "time": "2021-07-04T14:57:39+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "9.2.5", + "version": "9.2.6", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "f3e026641cc91909d421802dd3ac7827ebfd97e1" + "reference": "f6293e1b30a2354e8428e004689671b83871edde" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/f3e026641cc91909d421802dd3ac7827ebfd97e1", - "reference": "f3e026641cc91909d421802dd3ac7827ebfd97e1", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/f6293e1b30a2354e8428e004689671b83871edde", + "reference": "f6293e1b30a2354e8428e004689671b83871edde", "shasum": "" }, "require": { @@ -1960,7 +1788,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.5" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.6" }, "funding": [ { @@ -1968,7 +1796,7 @@ "type": "github" } ], - "time": "2020-11-28T06:44:49+00:00" + "time": "2021-03-28T07:26:59+00:00" }, { "name": "phpunit/php-file-iterator", @@ -2213,16 +2041,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.5.2", + "version": "9.5.8", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "f661659747f2f87f9e72095bb207bceb0f151cb4" + "reference": "191768ccd5c85513b4068bdbe99bb6390c7d54fb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f661659747f2f87f9e72095bb207bceb0f151cb4", - "reference": "f661659747f2f87f9e72095bb207bceb0f151cb4", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/191768ccd5c85513b4068bdbe99bb6390c7d54fb", + "reference": "191768ccd5c85513b4068bdbe99bb6390c7d54fb", "shasum": "" }, "require": { @@ -2234,7 +2062,7 @@ "ext-xml": "*", "ext-xmlwriter": "*", "myclabs/deep-copy": "^1.10.1", - "phar-io/manifest": "^2.0.1", + "phar-io/manifest": "^2.0.3", "phar-io/version": "^3.0.2", "php": ">=7.3", "phpspec/prophecy": "^1.12.1", @@ -2252,7 +2080,7 @@ "sebastian/global-state": "^5.0.1", "sebastian/object-enumerator": "^4.0.3", "sebastian/resource-operations": "^3.0.3", - "sebastian/type": "^2.3", + "sebastian/type": "^2.3.4", "sebastian/version": "^3.0.2" }, "require-dev": { @@ -2300,7 +2128,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.2" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.8" }, "funding": [ { @@ -2312,7 +2140,7 @@ "type": "github" } ], - "time": "2021-02-02T14:45:58+00:00" + "time": "2021-07-31T15:17:34+00:00" }, { "name": "sebastian/cli-parser", @@ -2820,16 +2648,16 @@ }, { "name": "sebastian/global-state", - "version": "5.0.2", + "version": "5.0.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "a90ccbddffa067b51f574dea6eb25d5680839455" + "reference": "23bd5951f7ff26f12d4e3242864df3e08dec4e49" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/a90ccbddffa067b51f574dea6eb25d5680839455", - "reference": "a90ccbddffa067b51f574dea6eb25d5680839455", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/23bd5951f7ff26f12d4e3242864df3e08dec4e49", + "reference": "23bd5951f7ff26f12d4e3242864df3e08dec4e49", "shasum": "" }, "require": { @@ -2872,7 +2700,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/global-state/issues", - "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.2" + "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.3" }, "funding": [ { @@ -2880,7 +2708,7 @@ "type": "github" } ], - "time": "2020-10-26T15:55:19+00:00" + "time": "2021-06-11T13:31:12+00:00" }, { "name": "sebastian/lines-of-code", @@ -3171,16 +2999,16 @@ }, { "name": "sebastian/type", - "version": "2.3.1", + "version": "2.3.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/type.git", - "reference": "81cd61ab7bbf2de744aba0ea61fae32f721df3d2" + "reference": "b8cd8a1c753c90bc1a0f5372170e3e489136f914" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/type/zipball/81cd61ab7bbf2de744aba0ea61fae32f721df3d2", - "reference": "81cd61ab7bbf2de744aba0ea61fae32f721df3d2", + "url": "https://api.github.com/repos/sebastianbergmann/type/zipball/b8cd8a1c753c90bc1a0f5372170e3e489136f914", + "reference": "b8cd8a1c753c90bc1a0f5372170e3e489136f914", "shasum": "" }, "require": { @@ -3215,7 +3043,7 @@ "homepage": "https://github.com/sebastianbergmann/type", "support": { "issues": "https://github.com/sebastianbergmann/type/issues", - "source": "https://github.com/sebastianbergmann/type/tree/2.3.1" + "source": "https://github.com/sebastianbergmann/type/tree/2.3.4" }, "funding": [ { @@ -3223,7 +3051,7 @@ "type": "github" } ], - "time": "2020-10-26T13:18:59+00:00" + "time": "2021-06-15T12:49:02+00:00" }, { "name": "sebastian/version", @@ -3280,16 +3108,16 @@ }, { "name": "squizlabs/php_codesniffer", - "version": "3.5.8", + "version": "3.6.0", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "9d583721a7157ee997f235f327de038e7ea6dac4" + "reference": "ffced0d2c8fa8e6cdc4d695a743271fab6c38625" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/9d583721a7157ee997f235f327de038e7ea6dac4", - "reference": "9d583721a7157ee997f235f327de038e7ea6dac4", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/ffced0d2c8fa8e6cdc4d695a743271fab6c38625", + "reference": "ffced0d2c8fa8e6cdc4d695a743271fab6c38625", "shasum": "" }, "require": { @@ -3332,20 +3160,20 @@ "source": "https://github.com/squizlabs/PHP_CodeSniffer", "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" }, - "time": "2020-10-23T02:01:07+00:00" + "time": "2021-04-09T00:54:41+00:00" }, { "name": "theseer/tokenizer", - "version": "1.2.0", + "version": "1.2.1", "source": { "type": "git", "url": "https://github.com/theseer/tokenizer.git", - "reference": "75a63c33a8577608444246075ea0af0d052e452a" + "reference": "34a41e998c2183e22995f158c581e7b5e755ab9e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/theseer/tokenizer/zipball/75a63c33a8577608444246075ea0af0d052e452a", - "reference": "75a63c33a8577608444246075ea0af0d052e452a", + "url": "https://api.github.com/repos/theseer/tokenizer/zipball/34a41e998c2183e22995f158c581e7b5e755ab9e", + "reference": "34a41e998c2183e22995f158c581e7b5e755ab9e", "shasum": "" }, "require": { @@ -3374,7 +3202,7 @@ "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", "support": { "issues": "https://github.com/theseer/tokenizer/issues", - "source": "https://github.com/theseer/tokenizer/tree/master" + "source": "https://github.com/theseer/tokenizer/tree/1.2.1" }, "funding": [ { @@ -3382,7 +3210,7 @@ "type": "github" } ], - "time": "2020-07-12T23:59:07+00:00" + "time": "2021-07-28T10:34:58+00:00" } ], "aliases": [], diff --git a/docs/advanced/bean-post-processor.md b/docs/advanced/bean-post-processor.md index b4169e6..825f63e 100644 --- a/docs/advanced/bean-post-processor.md +++ b/docs/advanced/bean-post-processor.md @@ -10,7 +10,9 @@ First of all you need to create a class that implements the ```php A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. -This led to introducing aliases for beans. Each bean can have multiple aliases and two types of aliases are possible. In case of collisions (the same alias is used for different beans) Disco will throw an exception. +This led to introducing aliases for beans. Each bean can have multiple aliases and two types of aliases are possible: +a name alias `#[Alias]` or a type alias `#[TypeAlias]`. + +In case of collisions (the same alias is used for different beans) Disco will throw an exception. You're asked to avoid/resolve such conflicts. Since version 0.10.0 of Disco collision detection will only take the configuration class into account the alias is defined in, this allows you to overwrite aliases in child configuration. -Simply add the `aliases` attribute to the `@Bean` annotation to define a list of `@Alias`: +Simply attribute the Bean with `#[Alias(name: 'some_alias')]` to define a named alias or `#[TypeAlias]` to define the return type as an aliases. +Multiple name aliases are allowed. ```php get(SampleService::class)`. diff --git a/docs/basic/bean-parameters.md b/docs/basic/bean-parameters.md index 72ea0e6..1b7ed00 100644 --- a/docs/basic/bean-parameters.md +++ b/docs/basic/bean-parameters.md @@ -1,32 +1,26 @@ # Bean Parameters -Bean instances can be parameterized by a given configuration. To access the configuration add a `parameters` attribute to your bean configuration method, which holds a collection of `@Parameter` annotations. +Bean instances can be parameterized by a given configuration. To access the configuration add a `#[Parameter]` attribute to your Bean method. -The `@Parameter` annotation requires at least a name which will be used as key to look for in the configuration array. +The `#[Parameter(name: 'paramName', key: 'config.key')]` attribute requires at least the `param` (which must match a param name of the Bean method) and the `key` which will be used to look for in the configuration array. -In the following example the value of configuration key `test` gets passed to the bean configuration method as method parameter. The configuration parameters are passed in the same order as noted to the bean configuration method: +In the following example the value of configuration key `configKey` gets passed to the bean configuation for the argument named `$test`. +Configuration parameters are passed to the bean configuration method using [named arguments](https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments): ```php setTest($test); @@ -40,13 +34,17 @@ The configuration array gets passed to the `\bitExpert\Disco\AnnotationBeanFacto ```php 'This is a test.']; +$parameters = ['configKey' => 'This is a test.']; $beanFactory = new \bitExpert\Disco\AnnotationBeanFactory( MyConfiguration::class, $parameters ); \bitExpert\Disco\BeanFactoryRegistry::register($beanFactory); + +$sampleService = $beanFactory->get('mySampleService'); + +echo $sampleService->test; // Output: This is a test. ``` ## Default Parameter Values @@ -56,30 +54,35 @@ Whenever a requested configuration key is not found — the key does not exist o ```php setTest($test); return $service; } } + +$parameters = []; // empty + +$beanFactory = new \bitExpert\Disco\AnnotationBeanFactory( + MyConfiguration::class, + $parameters +); +\bitExpert\Disco\BeanFactoryRegistry::register($beanFactory); + +$sampleService = $beanFactory->get('mySampleService'); + +echo $sampleService->test; // Output: Some default value. ``` ## Default Bean Configuration Method Parameter Values @@ -108,24 +111,17 @@ Use the '.' notation to access the nested elements: ```php setTest($test); diff --git a/docs/basic/injecting-dependencies.md b/docs/basic/injecting-dependencies.md index 3274194..231d9bd 100644 --- a/docs/basic/injecting-dependencies.md +++ b/docs/basic/injecting-dependencies.md @@ -5,8 +5,8 @@ To inject a bean as a dependency simply call the respective method of the config ```php $parameters * @param BeanFactoryConfiguration $config */ - public function __construct($configClassName, array $parameters = [], BeanFactoryConfiguration $config = null) + public function __construct( + string $configClassName, + array $parameters = [], + BeanFactoryConfiguration $config = null + ) { if ($config === null) { $config = new BeanFactoryConfiguration(sys_get_temp_dir()); @@ -79,7 +83,7 @@ public function get(string $id) /** * {@inheritDoc} */ - public function has(string $id) + public function has(string $id): bool { return is_callable([$this->beanConfig, $id]) || $this->beanConfig->hasAlias($id); } diff --git a/src/bitExpert/Disco/Attributes/Alias.php b/src/bitExpert/Disco/Annotations/Alias.php similarity index 88% rename from src/bitExpert/Disco/Attributes/Alias.php rename to src/bitExpert/Disco/Annotations/Alias.php index 3c43133..ae14ba0 100644 --- a/src/bitExpert/Disco/Attributes/Alias.php +++ b/src/bitExpert/Disco/Annotations/Alias.php @@ -10,7 +10,7 @@ */ declare(strict_types=1); -namespace bitExpert\Disco\Attributes; +namespace bitExpert\Disco\Annotations; use Attribute; use Webmozart\Assert\Assert; @@ -29,7 +29,6 @@ final class Alias private string $name; /** - * Creates a new {@link \bitExpert\Disco\Annotations\Alias}. * * @param string $name */ diff --git a/src/bitExpert/Disco/Attributes/Bean.php b/src/bitExpert/Disco/Annotations/Bean.php similarity index 97% rename from src/bitExpert/Disco/Attributes/Bean.php rename to src/bitExpert/Disco/Annotations/Bean.php index 242ae4e..dbcc5b9 100644 --- a/src/bitExpert/Disco/Attributes/Bean.php +++ b/src/bitExpert/Disco/Annotations/Bean.php @@ -10,7 +10,7 @@ */ declare(strict_types=1); -namespace bitExpert\Disco\Attributes; +namespace bitExpert\Disco\Annotations; use Attribute; use Webmozart\Assert\Assert; diff --git a/src/bitExpert/Disco/Attributes/BeanPostProcessor.php b/src/bitExpert/Disco/Annotations/BeanPostProcessor.php similarity index 91% rename from src/bitExpert/Disco/Attributes/BeanPostProcessor.php rename to src/bitExpert/Disco/Annotations/BeanPostProcessor.php index 5609cdf..6812dde 100644 --- a/src/bitExpert/Disco/Attributes/BeanPostProcessor.php +++ b/src/bitExpert/Disco/Annotations/BeanPostProcessor.php @@ -10,7 +10,7 @@ */ declare(strict_types=1); -namespace bitExpert\Disco\Attributes; +namespace bitExpert\Disco\Annotations; use Attribute; diff --git a/src/bitExpert/Disco/Attributes/Configuration.php b/src/bitExpert/Disco/Annotations/Configuration.php similarity index 91% rename from src/bitExpert/Disco/Attributes/Configuration.php rename to src/bitExpert/Disco/Annotations/Configuration.php index b3af012..8f58351 100644 --- a/src/bitExpert/Disco/Attributes/Configuration.php +++ b/src/bitExpert/Disco/Annotations/Configuration.php @@ -10,7 +10,7 @@ */ declare(strict_types=1); -namespace bitExpert\Disco\Attributes; +namespace bitExpert\Disco\Annotations; use Attribute; diff --git a/src/bitExpert/Disco/Attributes/Parameter.php b/src/bitExpert/Disco/Annotations/Parameter.php similarity index 98% rename from src/bitExpert/Disco/Attributes/Parameter.php rename to src/bitExpert/Disco/Annotations/Parameter.php index cc5f0df..94d4185 100644 --- a/src/bitExpert/Disco/Attributes/Parameter.php +++ b/src/bitExpert/Disco/Annotations/Parameter.php @@ -10,7 +10,7 @@ */ declare(strict_types=1); -namespace bitExpert\Disco\Attributes; +namespace bitExpert\Disco\Annotations; use Attribute; use Webmozart\Assert\Assert; diff --git a/src/bitExpert/Disco/Attributes/ReturnTypeAlias.php b/src/bitExpert/Disco/Annotations/TypeAlias.php similarity index 87% rename from src/bitExpert/Disco/Attributes/ReturnTypeAlias.php rename to src/bitExpert/Disco/Annotations/TypeAlias.php index bf642f0..3d1e255 100644 --- a/src/bitExpert/Disco/Attributes/ReturnTypeAlias.php +++ b/src/bitExpert/Disco/Annotations/TypeAlias.php @@ -10,7 +10,7 @@ */ declare(strict_types=1); -namespace bitExpert\Disco\Attributes; +namespace bitExpert\Disco\Annotations; use Attribute; @@ -20,6 +20,6 @@ * Used in conjunction with the #[Bean] attribute. */ #[Attribute(Attribute::TARGET_METHOD)] -final class ReturnTypeAlias +final class TypeAlias { } diff --git a/src/bitExpert/Disco/BeanFactoryConfiguration.php b/src/bitExpert/Disco/BeanFactoryConfiguration.php index add5622..82086ee 100644 --- a/src/bitExpert/Disco/BeanFactoryConfiguration.php +++ b/src/bitExpert/Disco/BeanFactoryConfiguration.php @@ -31,19 +31,22 @@ class BeanFactoryConfiguration /** * @var BeanStore */ - protected $sessionBeanStore; + protected BeanStore $sessionBeanStore; + /** * @var string */ - protected $proxyTargetDir; + protected string $proxyTargetDir; + /** - * @var ?GeneratorStrategyInterface + * @var GeneratorStrategyInterface */ - protected $proxyWriterGenerator; + protected GeneratorStrategyInterface $proxyWriterGenerator; + /** - * @var ?AutoloaderInterface + * @var AutoloaderInterface|null */ - protected $proxyAutoloader; + protected ?AutoloaderInterface $proxyAutoloader = null; /** * Creates a new {@link \bitExpert\Disco\BeanFactoryConfiguration}. @@ -51,7 +54,7 @@ class BeanFactoryConfiguration * @param string $proxyTargetDir * @throws InvalidArgumentException */ - public function __construct($proxyTargetDir) + public function __construct(string $proxyTargetDir) { try { $proxyFileLocator = new FileLocator($proxyTargetDir); diff --git a/src/bitExpert/Disco/Proxy/Configuration/ConfigurationGenerator.php b/src/bitExpert/Disco/Proxy/Configuration/ConfigurationGenerator.php index 4699152..fb3c726 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/ConfigurationGenerator.php +++ b/src/bitExpert/Disco/Proxy/Configuration/ConfigurationGenerator.php @@ -12,12 +12,13 @@ namespace bitExpert\Disco\Proxy\Configuration; -use bitExpert\Disco\Attributes\Alias; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\BeanPostProcessor; -use bitExpert\Disco\Attributes\Configuration; -use bitExpert\Disco\Attributes\Parameter; -use bitExpert\Disco\Attributes\ReturnTypeAlias; +use Attribute; +use bitExpert\Disco\Annotations\Alias; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\BeanPostProcessor; +use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Annotations\Parameter; +use bitExpert\Disco\Annotations\TypeAlias; use bitExpert\Disco\Proxy\Configuration\MethodGenerator\BeanMethod; use bitExpert\Disco\Proxy\Configuration\MethodGenerator\BeanPostProcessorMethod; use bitExpert\Disco\Proxy\Configuration\MethodGenerator\Constructor; @@ -42,6 +43,7 @@ use Laminas\Code\Generator\ClassGenerator; use Laminas\Code\Generator\Exception\InvalidArgumentException; use Laminas\Code\Reflection\MethodReflection; +use ReflectionNamedType; use ReflectionUnionType; /** @@ -95,6 +97,18 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe $localAliases = []; $methods = $originalClass->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED); foreach ($methods as $method) { + /** @var null|ReflectionUnionType|ReflectionNamedType $returnTypeRefl */ + $returnTypeRefl = $method->getReturnType(); + if ($returnTypeRefl instanceof ReflectionUnionType) { + throw new InvalidProxiedClassException( + sprintf( + 'Method "%s" on "%s" uses the unsupported union type.', + $method->name, + $originalClass->name + ) + ); + } + $reflectionMethod = new MethodReflection( $method->class, $method->name @@ -138,22 +152,10 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe $beanAliases = []; - /** @var \bitExpert\Disco\Attributes\ReturnTypeAlias $returnTypeAlias */ - $returnTypeAlias = ($reflectionMethod->getAttributes(ReturnTypeAlias::class)[0] ?? null) + /** @var TypeAlias|null $returnTypeAlias */ + $returnTypeAlias = ($reflectionMethod->getAttributes(TypeAlias::class)[0] ?? null) ?->newInstance(); if (null !== $returnTypeAlias) { - $returnTypeRefl = $method->getReturnType(); - - if ($returnTypeRefl instanceof ReflectionUnionType) { - throw new InvalidProxiedClassException( - sprintf( - 'Method "%s" on "%s" uses the unsupported union type.', - $method->name, - $originalClass->name - ) - ); - } - if (null === $returnTypeRefl || $returnTypeRefl->allowsNull() || $returnTypeRefl->isBuiltin()) { throw new InvalidProxiedClassException( sprintf( @@ -172,6 +174,7 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe } $beanAliases = [...$beanAliases, ...\array_map( + /** @phpstan-ignore-next-line */ fn($attr) => $attr->newInstance()->getName(), $reflectionMethod->getAttributes(Alias::class) )]; @@ -206,7 +209,7 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe $reflectionMethod, $beanAttribute, $parameterAttributes, - $method->getReturnType(), + $returnTypeRefl, $forceLazyInitProperty, $sessionBeansProperty, $postProcessorsProperty, diff --git a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanMethod.php b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanMethod.php index b924d69..80689be 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanMethod.php +++ b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanMethod.php @@ -12,8 +12,8 @@ namespace bitExpert\Disco\Proxy\Configuration\MethodGenerator; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Parameter; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Parameter; use bitExpert\Disco\BeanException; use bitExpert\Disco\InitializedBean; use bitExpert\Disco\Proxy\Configuration\PropertyGenerator\BeanFactoryConfigurationProperty; @@ -21,6 +21,7 @@ use bitExpert\Disco\Proxy\Configuration\PropertyGenerator\ForceLazyInitProperty; use bitExpert\Disco\Proxy\Configuration\PropertyGenerator\SessionBeansProperty; use bitExpert\Disco\Proxy\LazyBean\LazyBeanFactory; +use Laminas\Code\Generator\Exception\InvalidArgumentException; use ProxyManager\Exception\InvalidProxiedClassException; use ProxyManager\Proxy\LazyLoadingInterface; use ReflectionNamedType; @@ -40,9 +41,9 @@ class BeanMethod extends ParameterAwareMethodGenerator * Creates a new {@link \bitExpert\Disco\Proxy\Configuration\MethodGenerator\BeanMethod}. * * @param MethodReflection $originalMethod - * @param \bitExpert\Disco\Attributes\Bean $beanMetadata - * @param \bitExpert\Disco\Attributes\Parameter[] $parameters - * @param ReflectionNamedType|null $beanTypeName + * @param Bean $beanMetadata + * @param Parameter[] $parameters + * @param ReflectionNamedType|null $beanType * @param ForceLazyInitProperty $forceLazyInitProperty * @param SessionBeansProperty $sessionBeansProperty * @param BeanPostProcessorsProperty $postProcessorsProperty @@ -50,8 +51,8 @@ class BeanMethod extends ParameterAwareMethodGenerator * @param GetParameter $parameterValuesMethod * @param WrapBeanAsLazy $wrapBeanAsLazy * @return MethodGenerator - * @throws \Laminas\Code\Generator\Exception\InvalidArgumentException - * @throws \ProxyManager\Exception\InvalidProxiedClassException + * @throws InvalidArgumentException + * @throws InvalidProxiedClassException */ public static function generateMethod( MethodReflection $originalMethod, @@ -130,7 +131,7 @@ public static function generateMethod( * @override Enforces generation of \ProxyManager\Generator\MethodGenerator. * * {@inheritDoc} - * @throws \Laminas\Code\Generator\Exception\InvalidArgumentException + * @throws InvalidArgumentException */ public static function fromReflection(MethodReflection $reflectionMethod): MethodGenerator { @@ -157,7 +158,7 @@ public static function fromReflection(MethodReflection $reflectionMethod): Metho * @param string $padding * @param string $beanId * @param string $beanType - * @param \bitExpert\Disco\Attributes\Bean $beanMetadata + * @param Bean $beanMetadata * @param string $methodParams * @param ForceLazyInitProperty $forceLazyInitProperty * @param SessionBeansProperty $sessionBeansProperty @@ -282,7 +283,7 @@ protected static function generateBeanCreationCode( * @param string $padding * @param string $beanId * @param string $beanType - * @param \bitExpert\Disco\Attributes\Bean $beanMetadata + * @param Bean $beanMetadata * @param string $methodParams * @param ForceLazyInitProperty $forceLazyInitProperty * @param SessionBeansProperty $sessionBeansProperty diff --git a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanPostProcessorMethod.php b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanPostProcessorMethod.php index 8e58f9e..1f5c059 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanPostProcessorMethod.php +++ b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanPostProcessorMethod.php @@ -12,8 +12,8 @@ namespace bitExpert\Disco\Proxy\Configuration\MethodGenerator; -use bitExpert\Disco\Attributes\BeanPostProcessor; -use bitExpert\Disco\Attributes\Parameter; +use bitExpert\Disco\Annotations\Parameter; +use Laminas\Code\Generator\Exception\InvalidArgumentException; use Laminas\Code\Generator\MethodGenerator; use Laminas\Code\Reflection\MethodReflection; @@ -23,10 +23,10 @@ class BeanPostProcessorMethod extends ParameterAwareMethodGenerator * Creates a new {@link \bitExpert\Disco\Proxy\Configuration\MethodGenerator\BeanPostProcessorMethod}. * * @param MethodReflection $originalMethod - * @param \bitExpert\Disco\Attributes\Parameter[] $beanPostProcessorParameters + * @param Parameter[] $beanPostProcessorParameters * @param GetParameter $parameterValuesMethod * @return MethodGenerator - * @throws \Laminas\Code\Generator\Exception\InvalidArgumentException + * @throws InvalidArgumentException */ public static function generateMethod( MethodReflection $originalMethod, diff --git a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/ParameterAwareMethodGenerator.php b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/ParameterAwareMethodGenerator.php index 8f3b119..0d45fab 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/ParameterAwareMethodGenerator.php +++ b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/ParameterAwareMethodGenerator.php @@ -12,7 +12,7 @@ namespace bitExpert\Disco\Proxy\Configuration\MethodGenerator; -use bitExpert\Disco\Attributes\Parameter; +use bitExpert\Disco\Annotations\Parameter; use Laminas\Code\Generator\MethodGenerator; /** diff --git a/tests/bitExpert/Disco/Annotations/AliasUnitTest.php b/tests/bitExpert/Disco/Annotations/AliasUnitTest.php index 412659a..f0ef434 100644 --- a/tests/bitExpert/Disco/Annotations/AliasUnitTest.php +++ b/tests/bitExpert/Disco/Annotations/AliasUnitTest.php @@ -12,12 +12,12 @@ namespace bitExpert\Disco\Annotations; -use bitExpert\Disco\Attributes\Alias; +use bitExpert\Disco\Annotations\Alias; use PHPUnit\Framework\TestCase; use Webmozart\Assert\InvalidArgumentException; /** - * Unit tests for {@link \bitExpert\Disco\Attributes\Alias}. + * Unit tests for {@link \bitExpert\Disco\Annotations\NameAlias}. */ class AliasUnitTest extends TestCase { diff --git a/tests/bitExpert/Disco/Annotations/BeanUnitTest.php b/tests/bitExpert/Disco/Annotations/BeanUnitTest.php index ddab97c..bfb22a7 100644 --- a/tests/bitExpert/Disco/Annotations/BeanUnitTest.php +++ b/tests/bitExpert/Disco/Annotations/BeanUnitTest.php @@ -12,12 +12,12 @@ namespace bitExpert\Disco\Annotations; -use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Annotations\Bean; use PHPUnit\Framework\TestCase; use Webmozart\Assert\InvalidArgumentException; /** - * Unit tests for {@link \bitExpert\Disco\Attributes\Bean}. + * Unit tests for {@link \bitExpert\Disco\Annotations\Bean}. */ class BeanUnitTest extends TestCase { diff --git a/tests/bitExpert/Disco/Annotations/ParameterUnitTest.php b/tests/bitExpert/Disco/Annotations/ParameterUnitTest.php index f04c0ed..30f9180 100644 --- a/tests/bitExpert/Disco/Annotations/ParameterUnitTest.php +++ b/tests/bitExpert/Disco/Annotations/ParameterUnitTest.php @@ -12,12 +12,12 @@ namespace bitExpert\Disco\Annotations; -use bitExpert\Disco\Attributes\Parameter; +use bitExpert\Disco\Annotations\Parameter; use PHPUnit\Framework\TestCase; use Webmozart\Assert\InvalidArgumentException; /** - * Unit tests for {@link \bitExpert\Disco\Attributes\Parameter}. + * Unit tests for {@link \bitExpert\Disco\Annotations\Parameter}. */ class ParameterUnitTest extends TestCase { diff --git a/tests/bitExpert/Disco/Config/BeanConfiguration.php b/tests/bitExpert/Disco/Config/BeanConfiguration.php index 0bc175e..a4cd298 100644 --- a/tests/bitExpert/Disco/Config/BeanConfiguration.php +++ b/tests/bitExpert/Disco/Config/BeanConfiguration.php @@ -12,8 +12,8 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Configuration; use bitExpert\Disco\Helper\InitializedService; use bitExpert\Disco\Helper\MasterService; use bitExpert\Disco\Helper\SampleService; diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationPersistence.php b/tests/bitExpert/Disco/Config/BeanConfigurationPersistence.php index ebc088b..5f24492 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationPersistence.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationPersistence.php @@ -12,8 +12,8 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Configuration; use bitExpert\Disco\Helper\SampleService; #[Configuration] diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationSubclass.php b/tests/bitExpert/Disco/Config/BeanConfigurationSubclass.php index dff83ed..d488a3b 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationSubclass.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationSubclass.php @@ -12,8 +12,8 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Configuration; use bitExpert\Disco\Helper\MasterService; #[Configuration] diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationTrait.php b/tests/bitExpert/Disco/Config/BeanConfigurationTrait.php index aa6fde3..d3ae2c2 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationTrait.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationTrait.php @@ -12,7 +12,7 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Annotations\Configuration; use bitExpert\Disco\Config\Traits\NonSingletonNonLazyRequestBean; #[Configuration] diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithAliases.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithAliases.php index 1897635..79cb4e5 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithAliases.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithAliases.php @@ -12,10 +12,10 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Alias; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Configuration; -use bitExpert\Disco\Attributes\ReturnTypeAlias; +use bitExpert\Disco\Annotations\Alias; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Annotations\TypeAlias; use bitExpert\Disco\Helper\SampleService; use bitExpert\Disco\Helper\SampleServiceInterface; @@ -27,14 +27,14 @@ class BeanConfigurationWithAliases #[Alias(name: 'my::Custom::Namespace')] #[Alias(name: 'Alias_With_Underscore')] #[Alias(name: '123456')] - #[ReturnTypeAlias] + #[TypeAlias] public function sampleServiceWithAliases(): SampleService { return new SampleService(); } #[Bean] - #[ReturnTypeAlias] + #[TypeAlias] public function sampleServiceWithInterfaceReturnTypeAlias(): SampleServiceInterface { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliases.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliases.php index e66ce31..a344a9e 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliases.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliases.php @@ -12,9 +12,9 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Configuration; -use bitExpert\Disco\Attributes\ReturnTypeAlias; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Annotations\TypeAlias; use bitExpert\Disco\Helper\SampleService; use bitExpert\Disco\Helper\SampleServiceInterface; @@ -22,14 +22,14 @@ class BeanConfigurationWithConflictingAliases { #[Bean] - #[ReturnTypeAlias] + #[TypeAlias] public function sampleService1(): SampleServiceInterface { return new SampleService(); } #[Bean] - #[ReturnTypeAlias] + #[TypeAlias] public function sampleService2(): SampleServiceInterface { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliasesInParentClass.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliasesInParentClass.php index e65952c..194e971 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliasesInParentClass.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithConflictingAliasesInParentClass.php @@ -12,9 +12,9 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Alias; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Annotations\Alias; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Configuration; use bitExpert\Disco\Helper\SampleService; use bitExpert\Disco\Helper\SampleServiceInterface; @@ -22,7 +22,7 @@ class BeanConfigurationWithConflictingAliasesInParentClass extends BeanConfigurationWithConflictingAliases { #[Bean] - #[Alias(name: 'SampleService3Alias')] + #[NameAlias(name: 'SampleService3Alias')] public function sampleService3(): SampleServiceInterface { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithParameterizedPostProcessor.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithParameterizedPostProcessor.php index f42053b..ea660b0 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithParameterizedPostProcessor.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithParameterizedPostProcessor.php @@ -12,10 +12,10 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\BeanPostProcessor; -use bitExpert\Disco\Attributes\Configuration; -use bitExpert\Disco\Attributes\Parameter; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\BeanPostProcessor; +use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Annotations\Parameter; use bitExpert\Disco\Helper\ParameterizedSampleServiceBeanPostProcessor; use bitExpert\Disco\Helper\SampleService; diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php index 3b7c697..74478db 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php @@ -12,9 +12,9 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Configuration; -use bitExpert\Disco\Attributes\Parameter; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Annotations\Parameter; use bitExpert\Disco\Helper\SampleService; #[Configuration] diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessor.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessor.php index 58bcd92..3bb8a9b 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessor.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessor.php @@ -12,9 +12,9 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\BeanPostProcessor; -use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\BeanPostProcessor; +use bitExpert\Disco\Annotations\Configuration; use bitExpert\Disco\Helper\SampleService; use bitExpert\Disco\Helper\SampleServiceBeanPostProcessor; diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessorAndParameterizedDependency.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessorAndParameterizedDependency.php index 3f75596..eda3687 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessorAndParameterizedDependency.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessorAndParameterizedDependency.php @@ -12,10 +12,10 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\BeanPostProcessor; -use bitExpert\Disco\Attributes\Configuration; -use bitExpert\Disco\Attributes\Parameter; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\BeanPostProcessor; +use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Annotations\Parameter; use bitExpert\Disco\Helper\ParameterizedSampleServiceBeanPostProcessor; use bitExpert\Disco\Helper\SampleService; diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithPrimitives.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithPrimitives.php index 59c3ee5..63cc0e3 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithPrimitives.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithPrimitives.php @@ -12,8 +12,8 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Configuration; use bitExpert\Disco\Helper\SampleService; #[Configuration] diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithProtectedMethod.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithProtectedMethod.php index c157052..c21194d 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithProtectedMethod.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithProtectedMethod.php @@ -12,8 +12,8 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Configuration; use bitExpert\Disco\Helper\MasterService; use bitExpert\Disco\Helper\SampleService; diff --git a/tests/bitExpert/Disco/Config/ExtendedBeanConfigurationOverwritingParentAlias.php b/tests/bitExpert/Disco/Config/ExtendedBeanConfigurationOverwritingParentAlias.php index 5d0bc1f..6cc6dae 100644 --- a/tests/bitExpert/Disco/Config/ExtendedBeanConfigurationOverwritingParentAlias.php +++ b/tests/bitExpert/Disco/Config/ExtendedBeanConfigurationOverwritingParentAlias.php @@ -12,9 +12,9 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Configuration; -use bitExpert\Disco\Attributes\ReturnTypeAlias; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Configuration; +use bitExpert\Disco\Annotations\TypeAlias; use bitExpert\Disco\Helper\SampleService; use bitExpert\Disco\Helper\SampleServiceInterface; @@ -22,7 +22,7 @@ class ExtendedBeanConfigurationOverwritingParentAlias extends BeanConfigurationWithAliases { #[Bean] - #[ReturnTypeAlias] + #[TypeAlias] public function extendedSampleServiceWithInterfaceReturnTypeAlias(): SampleServiceInterface { return new SampleService(); diff --git a/tests/bitExpert/Disco/Config/InterfaceConfiguration.php b/tests/bitExpert/Disco/Config/InterfaceConfiguration.php index 46b6282..a9daff4 100644 --- a/tests/bitExpert/Disco/Config/InterfaceConfiguration.php +++ b/tests/bitExpert/Disco/Config/InterfaceConfiguration.php @@ -12,7 +12,7 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Annotations\Configuration; #[Configuration] interface InterfaceConfiguration diff --git a/tests/bitExpert/Disco/Config/MissingBeanAnnotationConfiguration.php b/tests/bitExpert/Disco/Config/MissingBeanAnnotationConfiguration.php index 66fbc45..354236b 100644 --- a/tests/bitExpert/Disco/Config/MissingBeanAnnotationConfiguration.php +++ b/tests/bitExpert/Disco/Config/MissingBeanAnnotationConfiguration.php @@ -12,7 +12,7 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Annotations\Configuration; use bitExpert\Disco\Helper\SampleService; #[Configuration] diff --git a/tests/bitExpert/Disco/Config/MissingReturnTypeConfiguration.php b/tests/bitExpert/Disco/Config/MissingReturnTypeConfiguration.php index 6f9ab04..4638966 100644 --- a/tests/bitExpert/Disco/Config/MissingReturnTypeConfiguration.php +++ b/tests/bitExpert/Disco/Config/MissingReturnTypeConfiguration.php @@ -12,8 +12,8 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Configuration; use bitExpert\Disco\Helper\SampleService; #[Configuration] diff --git a/tests/bitExpert/Disco/Config/NonExistentReturnTypeConfiguration.php b/tests/bitExpert/Disco/Config/NonExistentReturnTypeConfiguration.php index 4ed0891..367207e 100644 --- a/tests/bitExpert/Disco/Config/NonExistentReturnTypeConfiguration.php +++ b/tests/bitExpert/Disco/Config/NonExistentReturnTypeConfiguration.php @@ -12,8 +12,8 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Configuration; use bitExpert\Disco\Helper\SampleService; #[Configuration] diff --git a/tests/bitExpert/Disco/Config/Traits/NonSingletonNonLazyRequestBean.php b/tests/bitExpert/Disco/Config/Traits/NonSingletonNonLazyRequestBean.php index 521036d..c4c613b 100644 --- a/tests/bitExpert/Disco/Config/Traits/NonSingletonNonLazyRequestBean.php +++ b/tests/bitExpert/Disco/Config/Traits/NonSingletonNonLazyRequestBean.php @@ -12,7 +12,7 @@ namespace bitExpert\Disco\Config\Traits; -use bitExpert\Disco\Attributes\Bean; +use bitExpert\Disco\Annotations\Bean; use bitExpert\Disco\Helper\SampleService; trait NonSingletonNonLazyRequestBean diff --git a/tests/bitExpert/Disco/Config/WrongReturnTypeConfiguration.php b/tests/bitExpert/Disco/Config/WrongReturnTypeConfiguration.php index 5e90562..8476d07 100644 --- a/tests/bitExpert/Disco/Config/WrongReturnTypeConfiguration.php +++ b/tests/bitExpert/Disco/Config/WrongReturnTypeConfiguration.php @@ -12,8 +12,8 @@ namespace bitExpert\Disco\Config; -use bitExpert\Disco\Attributes\Bean; -use bitExpert\Disco\Attributes\Configuration; +use bitExpert\Disco\Annotations\Bean; +use bitExpert\Disco\Annotations\Configuration; use bitExpert\Disco\Helper\MasterService; use bitExpert\Disco\Helper\SampleService; From 48d452baa7d80c613dac2b431249a73371d1a843 Mon Sep 17 00:00:00 2001 From: Zacharias Luiten Date: Tue, 17 Aug 2021 15:57:25 +0200 Subject: [PATCH 3/3] Support both positional and named parameters --- docs/basic/bean-parameters.md | 28 +++++++-- src/bitExpert/Disco/AnnotationBeanFactory.php | 5 +- src/bitExpert/Disco/Annotations/Parameter.php | 20 +++---- .../Disco/BeanFactoryConfiguration.php | 5 +- .../ParameterAwareMethodGenerator.php | 31 ++++++++-- .../Disco/AnnotationBeanFactoryUnitTest.php | 57 +++++++++++++++++-- .../Disco/Annotations/ParameterUnitTest.php | 16 +++--- .../BeanConfigurationWithParameters.php | 47 ++++++++++++--- ...ostProcessorAndParameterizedDependency.php | 8 ++- .../bitExpert/Disco/Helper/SampleService.php | 11 ++++ .../ConfigurationGeneratorUnitTest.php | 10 +--- 11 files changed, 182 insertions(+), 56 deletions(-) diff --git a/docs/basic/bean-parameters.md b/docs/basic/bean-parameters.md index 1b7ed00..cca45fe 100644 --- a/docs/basic/bean-parameters.md +++ b/docs/basic/bean-parameters.md @@ -2,10 +2,16 @@ Bean instances can be parameterized by a given configuration. To access the configuration add a `#[Parameter]` attribute to your Bean method. -The `#[Parameter(name: 'paramName', key: 'config.key')]` attribute requires at least the `param` (which must match a param name of the Bean method) and the `key` which will be used to look for in the configuration array. +Configuration parameters can be passed to the Bean configuration method as [named](https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments) +or as positional arguments. A `Parameter` will be used as a named argument when the `name` argument is set. -In the following example the value of configuration key `configKey` gets passed to the bean configuation for the argument named `$test`. -Configuration parameters are passed to the bean configuration method using [named arguments](https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments): +So `#[Parameter(name: 'argName', key: 'config.key)]` is a named parameter whereas `#[Parameter(key: 'config.key')]` is a positional parameter. +As one might expect, the order of the positional parameters matter. Therefore, the recommended way of configuring parameters is using named parameters. +Named and positional parameters can be mixed. + +The `#[Parameter]` attribute requires at least `key` which will be used to look for in the configuration array. + +In the following example the value of configuration key `configKey` gets passed to the Bean configuration for the argument named `$test`. ```php setTest($test); return $service; } + + #[Bean] + #[Parameter(name: 'anotherTest', key: 'configKey2')] + #[Parameter(key: 'configKey1')] + public function mySampleService(string $test = '', string $anotherTest = '') : SampleService + { + $service = new SampleService(); + $service->setTest($test); + $service->setAnotherTest($anotherTest); + return $service; + } } ``` @@ -34,7 +51,7 @@ The configuration array gets passed to the `\bitExpert\Disco\AnnotationBeanFacto ```php 'This is a test.']; +$parameters = ['configKey1' => 'This is a test.' 'configKey2' => 'This is another test.']; $beanFactory = new \bitExpert\Disco\AnnotationBeanFactory( MyConfiguration::class, @@ -45,6 +62,7 @@ $beanFactory = new \bitExpert\Disco\AnnotationBeanFactory( $sampleService = $beanFactory->get('mySampleService'); echo $sampleService->test; // Output: This is a test. +echo $sampleService->anotherTest; // Output: This is another test. ``` ## Default Parameter Values diff --git a/src/bitExpert/Disco/AnnotationBeanFactory.php b/src/bitExpert/Disco/AnnotationBeanFactory.php index 9d30ec3..f569244 100644 --- a/src/bitExpert/Disco/AnnotationBeanFactory.php +++ b/src/bitExpert/Disco/AnnotationBeanFactory.php @@ -32,14 +32,13 @@ class AnnotationBeanFactory implements BeanFactory * * @param class-string $configClassName * @param array $parameters - * @param BeanFactoryConfiguration $config + * @param BeanFactoryConfiguration|null $config */ public function __construct( string $configClassName, array $parameters = [], BeanFactoryConfiguration $config = null - ) - { + ) { if ($config === null) { $config = new BeanFactoryConfiguration(sys_get_temp_dir()); } diff --git a/src/bitExpert/Disco/Annotations/Parameter.php b/src/bitExpert/Disco/Annotations/Parameter.php index 94d4185..d7affac 100644 --- a/src/bitExpert/Disco/Annotations/Parameter.php +++ b/src/bitExpert/Disco/Annotations/Parameter.php @@ -24,37 +24,37 @@ #[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] final class Parameter { - private string $name; - private string $key; + private ?string $name; + private mixed $defaultValue; private bool $required; /** - * @param string $name * @param string $key * @param bool $required - * @param mixed|null $default + * @param mixed $default + * @param string|null $name */ - public function __construct(string $name, string $key, bool $required = true, mixed $default = null) + public function __construct(string $key, bool $required = true, mixed $default = null, ?string $name = null) { - Assert::minLength($name, 1); Assert::minLength($key, 1); + Assert::nullOrMinLength($name, 1); - $this->name = $name; $this->key = $key; + $this->name = $name; $this->defaultValue = $default; $this->required = $required; } /** - * Return the name of the parameter + * Return the name of the argument or null in case of a positioned argument * - * @return string + * @return string|null */ - public function getName(): string + public function getName(): ?string { return $this->name; } diff --git a/src/bitExpert/Disco/BeanFactoryConfiguration.php b/src/bitExpert/Disco/BeanFactoryConfiguration.php index 82086ee..bed2202 100644 --- a/src/bitExpert/Disco/BeanFactoryConfiguration.php +++ b/src/bitExpert/Disco/BeanFactoryConfiguration.php @@ -144,10 +144,7 @@ public function getProxyManagerConfiguration(): Configuration { $proxyManagerConfiguration = new Configuration(); $proxyManagerConfiguration->setProxiesTargetDir($this->proxyTargetDir); - - if ($this->proxyWriterGenerator instanceof GeneratorStrategyInterface) { - $proxyManagerConfiguration->setGeneratorStrategy($this->proxyWriterGenerator); - } + $proxyManagerConfiguration->setGeneratorStrategy($this->proxyWriterGenerator); if ($this->proxyAutoloader instanceof AutoloaderInterface) { $proxyManagerConfiguration->setProxyAutoloader($this->proxyAutoloader); diff --git a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/ParameterAwareMethodGenerator.php b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/ParameterAwareMethodGenerator.php index 0d45fab..c197a11 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/ParameterAwareMethodGenerator.php +++ b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/ParameterAwareMethodGenerator.php @@ -32,9 +32,10 @@ protected static function convertMethodParamsToString( array $methodParameters, GetParameter $parameterValuesMethod ): string { - $parameters = []; + $positionalArgs = []; + $namedArgs = []; + foreach ($methodParameters as $methodParameter) { - /** @var Parameter $methodParameter */ $defaultValue = $methodParameter->getDefaultValue(); switch (\gettype($defaultValue)) { case 'string': @@ -50,12 +51,32 @@ protected static function convertMethodParamsToString( break; } - $template = ($defaultValue === '') ? '$this->%s("%s", %s)' : '$this->%s("%s", %s, %s)'; $required = $methodParameter->isRequired() ? 'true' : 'false'; $methodName = $parameterValuesMethod->getName(); - $parameters[] = \sprintf($template, $methodName, $methodParameter->getKey(), $required, $defaultValue); + $argName = $methodParameter->getName(); + + if (null === $argName) { + $template = ($defaultValue === '') ? '$this->%s("%s", %s)' : '$this->%s("%s", %s, %s)'; + $positionalArgs[] = \sprintf( + $template, + $methodName, + $methodParameter->getKey(), + $required, + $defaultValue + ); + } else { + $template = ($defaultValue === '') ? '%s: $this->%s("%s", %s)' : '%s: $this->%s("%s", %s, %s)'; + $namedArgs[] = \sprintf( + $template, + $argName, + $methodName, + $methodParameter->getKey(), + $required, + $defaultValue + ); + } } - return \implode(', ', $parameters); + return \implode(', ', [...$positionalArgs, ...$namedArgs]); } } diff --git a/tests/bitExpert/Disco/AnnotationBeanFactoryUnitTest.php b/tests/bitExpert/Disco/AnnotationBeanFactoryUnitTest.php index 04f02cb..7853378 100644 --- a/tests/bitExpert/Disco/AnnotationBeanFactoryUnitTest.php +++ b/tests/bitExpert/Disco/AnnotationBeanFactoryUnitTest.php @@ -260,14 +260,15 @@ public function beanFactoryPostProcessorCanBeConfiguredWithParameterizedDependen { $this->beanFactory = new AnnotationBeanFactory( BeanConfigurationWithPostProcessorAndParameterizedDependency::class, - ['test' => 'injectedValue'] + ['configKey1' => 'injectedValue1', 'configKey2' => 'injectedValue2'] ); BeanFactoryRegistry::register($this->beanFactory); /** @var SampleService $bean */ $bean = $this->beanFactory->get('nonSingletonNonLazyRequestBean'); self::assertInstanceOf(stdClass::class, $bean->test); - self::assertEquals('injectedValue', $bean->test->property); + self::assertEquals('injectedValue1', $bean->test->property1); + self::assertEquals('injectedValue2', $bean->test->property2); } /** @@ -277,7 +278,7 @@ public function parameterPassedToBeanFactoryGetsInjectedInBean(): void { $this->beanFactory = new AnnotationBeanFactory( BeanConfigurationWithParameters::class, - ['test' => 'injectedValue'] + ['configKey' => 'injectedValue'] ); BeanFactoryRegistry::register($this->beanFactory); @@ -285,6 +286,54 @@ public function parameterPassedToBeanFactoryGetsInjectedInBean(): void self::assertEquals('injectedValue', $bean->test); } + /** + * @test + */ + public function parametersPassedToBeanFactoryGetsInjectedInBeanWithPositionalParams(): void + { + $this->beanFactory = new AnnotationBeanFactory( + BeanConfigurationWithParameters::class, + ['configKey1' => 'injectedValue1', 'configKey2' => 'injectedValue2'] + ); + BeanFactoryRegistry::register($this->beanFactory); + + $bean = $this->beanFactory->get('sampleServiceWithPositionalParams'); + self::assertEquals('injectedValue1', $bean->test); + self::assertEquals('injectedValue2', $bean->anotherTest); + } + + /** + * @test + */ + public function parametersPassedToBeanFactoryGetsInjectedInBeanWithNamedParams(): void + { + $this->beanFactory = new AnnotationBeanFactory( + BeanConfigurationWithParameters::class, + ['configKey1' => 'injectedValue1', 'configKey2' => 'injectedValue2'] + ); + BeanFactoryRegistry::register($this->beanFactory); + + $bean = $this->beanFactory->get('sampleServiceWithNamedParams'); + self::assertEquals('injectedValue1', $bean->test); + self::assertEquals('injectedValue2', $bean->anotherTest); + } + + /** + * @test + */ + public function parametersPassedToBeanFactoryGetsInjectedInBeanWithMixedPositionalAndNamedParams(): void + { + $this->beanFactory = new AnnotationBeanFactory( + BeanConfigurationWithParameters::class, + ['configKey1' => 'injectedValue1', 'configKey2' => 'injectedValue2'] + ); + BeanFactoryRegistry::register($this->beanFactory); + + $bean = $this->beanFactory->get('sampleServiceWithMixedPositionalAndNamedParams'); + self::assertEquals('injectedValue1', $bean->test); + self::assertEquals('injectedValue2', $bean->anotherTest); + } + /** * @test */ @@ -293,7 +342,7 @@ public function nestedParameterKeyPassedToBeanFactoryGetsInjectedInBean(): void $this->beanFactory = new AnnotationBeanFactory( BeanConfigurationWithParameters::class, [ - 'test' => [ + 'config' => [ 'nested' => [ 'key' => 'injectedValue' ] diff --git a/tests/bitExpert/Disco/Annotations/ParameterUnitTest.php b/tests/bitExpert/Disco/Annotations/ParameterUnitTest.php index 30f9180..b1e1975 100644 --- a/tests/bitExpert/Disco/Annotations/ParameterUnitTest.php +++ b/tests/bitExpert/Disco/Annotations/ParameterUnitTest.php @@ -28,7 +28,7 @@ public function emptyNameWillThrowAnnotationException(): void { $this->expectException(InvalidArgumentException::class); - new Parameter('', 'myParam'); + new Parameter(key: 'myParam', name: ''); } /** @@ -38,7 +38,7 @@ public function emptyKeyWillThrowAnnotationException(): void { $this->expectException(InvalidArgumentException::class); - new Parameter('name', ''); + new Parameter(key: ''); } /** @@ -46,9 +46,9 @@ public function emptyKeyWillThrowAnnotationException(): void */ public function nameIsSet(): void { - $parameter = new Parameter(name: 'paramName', key: 'key'); + $parameter = new Parameter(name: 'argName', key: 'key'); - self::assertSame('paramName', $parameter->getName()); + self::assertSame('argName', $parameter->getName()); } /** @@ -56,7 +56,7 @@ public function nameIsSet(): void */ public function keyIsSet(): void { - $parameter = new Parameter(name: 'paramName', key: 'key'); + $parameter = new Parameter(name: 'argName', key: 'key'); self::assertSame('key', $parameter->getKey()); } @@ -78,7 +78,7 @@ public function defaultValueDefaultsToNull(): void */ public function defaultValueIsParsed(mixed $defaultValue): void { - $parameter = new Parameter(name: 'paramName', key: 'myParam', default: $defaultValue); + $parameter = new Parameter(name: 'argName', key: 'myParam', default: $defaultValue); self::assertSame($defaultValue, $parameter->getDefaultValue()); } @@ -88,7 +88,7 @@ public function defaultValueIsParsed(mixed $defaultValue): void */ public function requireDefaultsToTrue(): void { - $parameter = new Parameter(name: 'paramName', key: 'myParam'); + $parameter = new Parameter(name: 'argName', key: 'myParam'); self::assertTrue($parameter->isRequired()); } @@ -100,7 +100,7 @@ public function requireDefaultsToTrue(): void */ public function requireIsParsed(bool $requireValue): void { - $parameter = new Parameter(name: 'paramName', key: 'myParam', required: $requireValue); + $parameter = new Parameter(name: 'argName', key: 'myParam', required: $requireValue); self::assertSame($requireValue, $parameter->isRequired()); } diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php index 74478db..f0c5d08 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php @@ -21,7 +21,7 @@ class BeanConfigurationWithParameters { #[Bean(singleton: false)] - #[Parameter(name: 'test', key: 'test')] + #[Parameter(name: 'test', key: 'configKey')] public function sampleServiceWithParam($test = ''): SampleService { $service = new SampleService(); @@ -30,7 +30,40 @@ public function sampleServiceWithParam($test = ''): SampleService } #[Bean(singleton: false)] - #[Parameter(name: 'test', key: 'test', default: null)] + #[Parameter('configKey1')] + #[Parameter('configKey2')] + public function sampleServiceWithPositionalParams($test = '', $anotherTest = ''): SampleService + { + $service = new SampleService(); + $service->setTest($test); + $service->setAnotherTest($anotherTest); + return $service; + } + + #[Bean(singleton: false)] + #[Parameter(key: 'configKey2', name: 'anotherTest')] + #[Parameter(key: 'configKey1', name: 'test')] + public function sampleServiceWithNamedParams($test = '', $anotherTest = ''): SampleService + { + $service = new SampleService(); + $service->setTest($test); + $service->setAnotherTest($anotherTest); + return $service; + } + + #[Bean(singleton: false)] + #[Parameter(key: 'configKey2', name: 'anotherTest')] + #[Parameter(key: 'configKey1')] + public function sampleServiceWithMixedPositionalAndNamedParams($test = '', $anotherTest = ''): SampleService + { + $service = new SampleService(); + $service->setTest($test); + $service->setAnotherTest($anotherTest); + return $service; + } + + #[Bean(singleton: false)] + #[Parameter(name: 'test', key: 'configKey', default: null)] public function sampleServiceWithParamNull($test = ''): SampleService { $service = new SampleService(); @@ -39,7 +72,7 @@ public function sampleServiceWithParamNull($test = ''): SampleService } #[Bean(singleton: false)] - #[Parameter(name: 'test', key: 'test', default: true)] + #[Parameter(name: 'test', key: 'configKey', default: true)] public function sampleServiceWithParamBool($test = ''): SampleService { $service = new SampleService(); @@ -48,7 +81,7 @@ public function sampleServiceWithParamBool($test = ''): SampleService } #[Bean(singleton: false)] - #[Parameter(name: 'test', key: 'test', default: 0)] + #[Parameter(name: 'test', key: 'configKey', default: 0)] public function sampleServiceWithParamEmpty($test = ''): SampleService { $service = new SampleService(); @@ -57,7 +90,7 @@ public function sampleServiceWithParamEmpty($test = ''): SampleService } #[Bean(singleton: false)] - #[Parameter(name: 'test', key: 'test.nested.key')] + #[Parameter(name: 'test', key: 'config.nested.key')] public function sampleServiceWithNestedParamKey($test = ''): SampleService { $service = new SampleService(); @@ -66,7 +99,7 @@ public function sampleServiceWithNestedParamKey($test = ''): SampleService } #[Bean(singleton: false)] - #[Parameter(name: 'test', key: 'test', default: 'myDefaultValue')] + #[Parameter(name: 'test', key: 'configKey', default: 'myDefaultValue')] public function sampleServiceWithParamDefaultValue($test = ''): SampleService { $service = new SampleService(); @@ -75,7 +108,7 @@ public function sampleServiceWithParamDefaultValue($test = ''): SampleService } #[Bean(singleton: false)] - #[Parameter(name: 'test', key: 'test', required: false)] + #[Parameter(name: 'test', key: 'configKey', required: false)] public function sampleServiceWithoutRequiredParam($test = ''): SampleService { $service = new SampleService(); diff --git a/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessorAndParameterizedDependency.php b/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessorAndParameterizedDependency.php index eda3687..27a5b4f 100644 --- a/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessorAndParameterizedDependency.php +++ b/tests/bitExpert/Disco/Config/BeanConfigurationWithPostProcessorAndParameterizedDependency.php @@ -29,11 +29,13 @@ public function sampleServiceBeanPostProcessor(): ParameterizedSampleServiceBean } #[Bean] - #[Parameter(name: 'test', key: 'test')] - public function dependency($test = ''): \stdClass + #[Parameter(name: 'property1', key: 'configKey1')] + #[Parameter(name: 'property2', key: 'configKey2')] + public function dependency(string $property1 = '', string $property2 = ''): \stdClass { $object = new \stdClass(); - $object->property = $test; + $object->property1 = $property1; + $object->property2 = $property2; return $object; } diff --git a/tests/bitExpert/Disco/Helper/SampleService.php b/tests/bitExpert/Disco/Helper/SampleService.php index 23ef031..87a9239 100644 --- a/tests/bitExpert/Disco/Helper/SampleService.php +++ b/tests/bitExpert/Disco/Helper/SampleService.php @@ -15,6 +15,7 @@ class SampleService implements SampleServiceInterface { public $test; + public $anotherTest; /** * Setter method for the $test property. @@ -25,4 +26,14 @@ public function setTest($test) { $this->test = $test; } + + /** + * Setter method for the $anotherTest property. + * + * @param mixed $test + */ + public function setAnotherTest($test) + { + $this->anotherTest = $test; + } } diff --git a/tests/bitExpert/Disco/Proxy/Configuration/ConfigurationGeneratorUnitTest.php b/tests/bitExpert/Disco/Proxy/Configuration/ConfigurationGeneratorUnitTest.php index b701110..5d6d64e 100644 --- a/tests/bitExpert/Disco/Proxy/Configuration/ConfigurationGeneratorUnitTest.php +++ b/tests/bitExpert/Disco/Proxy/Configuration/ConfigurationGeneratorUnitTest.php @@ -21,6 +21,7 @@ use bitExpert\Disco\Config\MissingBeanAnnotationConfiguration; use bitExpert\Disco\Config\MissingReturnTypeConfiguration; use bitExpert\Disco\Config\NonExistentReturnTypeConfiguration; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use ProxyManager\Exception\InvalidProxiedClassException; use Laminas\Code\Generator\ClassGenerator; @@ -36,7 +37,7 @@ class ConfigurationGeneratorUnitTest extends TestCase private $configGenerator; /** - * @var ClassGenerator&\PHPUnit\Framework\MockObject\MockObject + * @var ClassGenerator&MockObject */ private $classGenerator; @@ -48,9 +49,7 @@ public function setUp(): void parent::setUp(); $this->configGenerator = new ConfigurationGenerator(); - /** @var ClassGenerator&\PHPUnit\Framework\MockObject\MockObject $mock */ - $mock = $this->createMock(ClassGenerator::class); - $this->classGenerator = $mock; + $this->classGenerator = $this->createMock(ClassGenerator::class); } /** @@ -127,9 +126,6 @@ public function missingConfigurationAttributeThrowsException(): void $this->expectException(InvalidProxiedClassException::class); $this->expectExceptionMessageMatches('/#\[Configuration\] attribute missing!/'); - /** - * @foo - */ $configObject = new class { public function foo(): string