Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert @Parameters annotation to "parameters" attribute of @Bean annotation #100

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions docs/basic/bean-parameters.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Bean parameters

Bean instances can be parametrized by a given configuration array. To
access configuration elements add both the ```@Parameters``` and the
```@Parameter``` annotation to your bean configuration method.
access configuration elements add an ```parameters``` attribute to your
bean configuration method which holds a collection of ```@Parameter```
annotations.

The ```@Parameters``` annotation acts as a collection holding one or more
```@Parameter``` annotations. The ```@Parameter``` annotation requires
at least a name which will be used as key to look for in the configuration
array.
The ```@Parameter``` annotation requires at least a name which will be
used as key 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
Expand All @@ -18,6 +17,7 @@ method:
<?php

use bitExpert\Disco\Annotations\Bean;
use bitExpert\Disco\Annotations\Parameter;
use bitExpert\Disco\Annotations\Configuration;
use bitExpert\Disco\Helper\SampleService;

Expand All @@ -27,9 +27,10 @@ use bitExpert\Disco\Helper\SampleService;
class MyConfiguration
{
/**
* @Bean
* @Parameters({
* @Parameter({"name" = "test"})
* @Bean({
* "parameters"={
* @Parameter({"name" = "test"})
* }
* })
*/
public function mySampleService($test = '') : SampleService
Expand Down Expand Up @@ -64,6 +65,7 @@ configuration method instead:
<?php

use bitExpert\Disco\Annotations\Bean;
use bitExpert\Disco\Annotations\Parameter;
use bitExpert\Disco\Annotations\Configuration;
use bitExpert\Disco\Helper\SampleService;

Expand All @@ -73,9 +75,10 @@ use bitExpert\Disco\Helper\SampleService;
class MyConfiguration
{
/**
* @Bean
* @Parameters({
* @Parameter({"name" = "test", "default" = "Some default value"})
* @Bean({
* "parameters"={
* @Parameter({"name" = "test", "default" = "Some default value"})
* }
* })
*/
public function mySampleService($test = '') : SampleService
Expand Down Expand Up @@ -117,6 +120,7 @@ Use the '.' notation to access the nested elements:
<?php

use bitExpert\Disco\Annotations\Bean;
use bitExpert\Disco\Annotations\Parameter;
use bitExpert\Disco\Annotations\Configuration;
use bitExpert\Disco\Helper\SampleService;

Expand All @@ -126,9 +130,10 @@ use bitExpert\Disco\Helper\SampleService;
class MyConfiguration
{
/**
* @Bean
* @Parameters({
* @Parameter({"name" = "nested.key"})
* @Bean({
* "parameters"={
* @Parameter({"name" = "nested.key"})
* }
* })
*/
public function mySampleService($test = '') : SampleService
Expand Down
21 changes: 21 additions & 0 deletions src/bitExpert/Disco/Annotations/Bean.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @Attribute("singleton", type = "bool"),
* @Attribute("lazy", type = "bool"),
* @Attribute("aliases", type = "array<\bitExpert\Disco\Annotations\Alias>"),
* @Attribute("parameters", type = "array<\bitExpert\Disco\Annotations\Parameter>")
* })
*/
final class Bean
Expand All @@ -46,6 +47,10 @@ final class Bean
* @var Alias[]
*/
protected $aliases;
/**
* @var Parameter[]
*/
protected $parameters;

/**
* Creates a new {@link \bitExpert\Disco\Annotations\Bean}.
Expand All @@ -60,6 +65,7 @@ public function __construct(array $attributes = [])
$this->singleton = true;
$this->lazy = false;
$this->aliases = [];
$this->parameters = [];

if (isset($attributes['value'])) {
if (isset($attributes['value']['scope']) && (strtolower($attributes['value']['scope']) === 'session')) {
Expand All @@ -77,6 +83,10 @@ public function __construct(array $attributes = [])
if (isset($attributes['value']['aliases'])) {
$this->aliases = $attributes['value']['aliases'];
}

if (isset($attributes['value']['parameters'])) {
$this->parameters = $attributes['value']['parameters'];
}
}
}

Expand Down Expand Up @@ -122,10 +132,21 @@ public function isLazy() : bool

/**
* 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;
}

/**
* Returns the list of parameters for the bean instance. Returns an empty array when no parameters were set.
*
* @return Parameter[]
*/
public function getParameters(): array
{
return $this->parameters;
}
}
25 changes: 0 additions & 25 deletions src/bitExpert/Disco/Annotations/Parameters.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public function __construct()
AnnotationRegistry::registerFile(__DIR__ . '/../../Annotations/Alias.php');
AnnotationRegistry::registerFile(__DIR__ . '/../../Annotations/BeanPostProcessor.php');
AnnotationRegistry::registerFile(__DIR__ . '/../../Annotations/Configuration.php');
AnnotationRegistry::registerFile(__DIR__ . '/../../Annotations/Parameters.php');
AnnotationRegistry::registerFile(__DIR__ . '/../../Annotations/Parameter.php');
}

Expand Down Expand Up @@ -125,12 +124,6 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe
);
}

/* @var \bitExpert\Disco\Annotations\Parameters $parametersAnnotation */
$parametersAnnotation = $reader->getMethodAnnotation($method, Parameters::class);
if (null === $parametersAnnotation) {
$parametersAnnotation = new Parameters();
}

$beanType = $method->getReturnType();
if (null === $beanType) {
throw new InvalidProxiedClassException(
Expand Down Expand Up @@ -182,7 +175,6 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe
$proxyMethod = BeanMethod::generateMethod(
$methodReflection,
$beanAnnotation,
$parametersAnnotation,
$method->getReturnType(),
$forceLazyInitProperty,
$sessionBeansProperty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class BeanMethod extends MethodGenerator
*
* @param MethodReflection $originalMethod
* @param Bean $beanMetadata
* @param Parameters $methodParameters
* @param ReflectionType|null $beanType
* @param ForceLazyInitProperty $forceLazyInitProperty
* @param SessionBeansProperty $sessionBeansProperty
Expand All @@ -51,11 +50,12 @@ class BeanMethod extends MethodGenerator
* @param GetParameter $parameterValuesMethod
* @param WrapBeanAsLazy $wrapBeanAsLazy
* @return BeanMethod|MethodGenerator
* @throws \Zend\Code\Generator\Exception\InvalidArgumentException
* @throws \ProxyManager\Exception\InvalidProxiedClassException
*/
public static function generateMethod(
MethodReflection $originalMethod,
Bean $beanMetadata,
Parameters $methodParameters,
?ReflectionType $beanType,
ForceLazyInitProperty $forceLazyInitProperty,
SessionBeansProperty $sessionBeansProperty,
Expand All @@ -76,7 +76,7 @@ public static function generateMethod(
$beanType = (string) $beanType;

$method = static::fromReflection($originalMethod);
$methodParams = static::convertMethodParamsToString($methodParameters, $parameterValuesMethod);
$methodParams = static::convertMethodParamsToString($beanMetadata->getParameters(), $parameterValuesMethod);
$beanId = $originalMethod->name;
$body = '';

Expand Down Expand Up @@ -159,11 +159,11 @@ public static function fromReflection(MethodReflection $reflectionMethod) : Meth
* @return string
*/
protected static function convertMethodParamsToString(
Parameters $methodParameters,
array $methodParameters,
GetParameter $parameterValuesMethod
) : string {
$parameters = [];
foreach ($methodParameters->value as $methodParameter) {
foreach ($methodParameters as $methodParameter) {
/** @var $methodParameter Parameter */
$name = $methodParameter->getName();
$defaultValue = $methodParameter->getDefaultValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use bitExpert\Disco\Annotations\BeanPostProcessor;
use bitExpert\Disco\Annotations\Configuration;
use bitExpert\Disco\Annotations\Parameter;
use bitExpert\Disco\Annotations\Parameters;
use bitExpert\Disco\Helper\ParameterizedSampleServiceBeanPostProcessor;
use bitExpert\Disco\Helper\SampleService;

Expand All @@ -42,9 +41,10 @@ public function nonSingletonNonLazyRequestBean() : SampleService
}

/**
* @Bean
* @Parameters({
* @Parameter({"name" = "test"})
* @Bean({
* "parameters"={
* @Parameter({"name" = "test"})
* }
* })
*/
public function dependency($test = '') : \stdClass
Expand Down
57 changes: 35 additions & 22 deletions tests/bitExpert/Disco/Config/BeanConfigurationWithParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use bitExpert\Disco\Annotations\Bean;
use bitExpert\Disco\Annotations\Configuration;
use bitExpert\Disco\Annotations\Parameter;
use bitExpert\Disco\Annotations\Parameters;
use bitExpert\Disco\Helper\SampleService;

/**
Expand All @@ -24,9 +23,11 @@
class BeanConfigurationWithParameters
{
/**
* @Bean({"singleton"=false})
* @Parameters({
* @Parameter({"name" = "test"})
* @Bean({
* "singleton"=false,
* "parameters"={
* @Parameter({"name" = "test"})
* }
* })
*/
public function sampleServiceWithParam($test = '') : SampleService
Expand All @@ -37,9 +38,11 @@ public function sampleServiceWithParam($test = '') : SampleService
}

/**
* @Bean({"singleton"=false})
* @Parameters({
* @Parameter({"name" = "test", "default" = null})
* @Bean({
* "singleton"=false,
* "parameters"={
* @Parameter({"name" = "test", "default" = null})
* }
* })
*/
public function sampleServiceWithParamNull($test = '') : SampleService
Expand All @@ -50,9 +53,11 @@ public function sampleServiceWithParamNull($test = '') : SampleService
}

/**
* @Bean({"singleton"=false})
* @Parameters({
* @Parameter({"name" = "test", "default" = true})
* @Bean({
* "singleton"=false,
* "parameters"={
* @Parameter({"name" = "test", "default" = true})
* }
* })
*/
public function sampleServiceWithParamBool($test = '') : SampleService
Expand All @@ -63,9 +68,11 @@ public function sampleServiceWithParamBool($test = '') : SampleService
}

/**
* @Bean({"singleton"=false})
* @Parameters({
* @Parameter({"name" = "test", "default" = 0})
* @Bean({
* "singleton"=false,
* "parameters"={
* @Parameter({"name" = "test", "default" = 0})
* }
* })
*/
public function sampleServiceWithParamEmpty($test = '') : SampleService
Expand All @@ -76,9 +83,11 @@ public function sampleServiceWithParamEmpty($test = '') : SampleService
}

/**
* @Bean({"singleton"=false})
* @Parameters({
* @Parameter({"name" = "test.nested.key"})
* @Bean({
* "singleton"=false,
* "parameters"={
* @Parameter({"name" = "test.nested.key"})
* }
* })
*/
public function sampleServiceWithNestedParamKey($test = '') : SampleService
Expand All @@ -89,9 +98,11 @@ public function sampleServiceWithNestedParamKey($test = '') : SampleService
}

/**
* @Bean({"singleton"=false})
* @Parameters({
* @Parameter({"name" = "test", "default" = "myDefaultValue"})
* @Bean({
* "singleton"=false,
* "parameters"={
* @Parameter({"name" = "test", "default" = "myDefaultValue"})
* }
* })
*/
public function sampleServiceWithParamDefaultValue($test = '') : SampleService
Expand All @@ -102,9 +113,11 @@ public function sampleServiceWithParamDefaultValue($test = '') : SampleService
}

/**
* @Bean({"singleton"=false})
* @Parameters({
* @Parameter({"name" = "test", "required" = false})
* @Bean({
* "singleton"=false,
* "parameters"={
* @Parameter({"name" = "test", "required" = false})
* }
* })
*/
public function sampleServiceWithoutRequiredParam($test = '') : SampleService
Expand Down