Skip to content

Commit fda81d5

Browse files
authored
Merge branch 'master' into uuid-generator
2 parents ee78f6e + caa509f commit fda81d5

9 files changed

Lines changed: 144 additions & 119 deletions

File tree

README.md

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@ public function registerBundles()
2424
new Vivait\StringGeneratorBundle\VivaitStringGeneratorBundle(),
2525
}
2626
```
27-
## Configure
28-
29-
The default configuration is shown below:
30-
31-
```yaml
32-
vivait_string_generator:
33-
generators:
34-
string: vivait_generator.generator.string
35-
secure_bytes: vivait_generator.generator.secure_bytes
36-
secure_string: vivait_generator.generator.secure_string
37-
38-
# For use this generator you should require the package ramsey/uuid in your application.
39-
uuid_string: vivait_generator.generator.uuid_string
40-
```
4127

4228
## Bundled generators
4329

@@ -64,6 +50,8 @@ Generates a secure random byte string using the `Symfony\Component\Security\Core
6450
```
6551

6652
### `UuidStringGenerator`
53+
***For use this generator you should require the package ```ramsey/uuid``` in your application.***
54+
6755
For generate a UUID v4 (or v1):
6856

6957
```php
@@ -172,8 +160,14 @@ However, by setting `override` to false, only null properties will have a string
172160

173161

174162
## Custom generators
175-
You can use your own generators by implementing `GeneratorInterface` and defining the generator in the configuration,
176-
using either its service or classname.
163+
You can use your own generators by implementing `GeneratorInterface` and defining the generator in your `services.yml` file with the tag `vivait_generator.generator` and an `alias`, which will be used to identify the Generator in annotations.
164+
165+
```yaml
166+
vivait_generator.generator.secure_bytes:
167+
class: Vivait\StringGeneratorBundle\Generator\SecureBytesGenerator
168+
tags:
169+
- { name: vivait_generator.generator, alias: 'secure_bytes' }
170+
```
177171
178172
To create configurable generators, implement `ConfigurableGeneratorInterface`. This interface uses
179173
[`Symfony\Component\OptionsResolver\OptionsResolver`](http://symfony.com/doc/current/components/options_resolver.html) to set the generator configuration.
@@ -182,30 +176,30 @@ Set default options:
182176

183177
```php
184178
/**
185-
* @param OptionsResolver $resolver
186-
* @return mixed
187-
*/
179+
* @param OptionsResolver $resolver
180+
*/
188181
public function getDefaultOptions(OptionsResolver $resolver)
189182
{
190-
$resolver->setDefaults([
191-
'chars' => $this->chars,
192-
'length' => $this->length,
193-
'prefix' => $this->prefix,
194-
]);
195-
}
183+
$resolver->setDefaults(
184+
[
185+
'chars' => $this->chars,
186+
'length' => $this->length,
187+
'prefix' => $this->prefix,
188+
]
189+
);
190+
}
196191
```
197192

198193
Do something with options:
199194

200-
```php
201-
/**
202-
* @param array $options
203-
* @return mixed|void
204-
*/
205-
public function setOptions(array $options)
206-
{
207-
$this->chars = $options['chars'];
195+
```php
196+
/**
197+
* @param array $options
198+
*/
199+
public function setOptions(array $options)
200+
{
201+
$this->chars = $options['chars'];
208202
$this->length = $options['length'];
209203
$this->prefix = $options['prefix'];
210-
}
211-
```
204+
}
205+
```

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"symfony/polyfill-php70": "^1.3",
1818
"symfony/security": "^2.8|^3.0",
1919
"symfony/options-resolver": "^2.8|^3.0",
20-
"ircmaxell/random-lib": "~1.0"
20+
"ircmaxell/random-lib": "~1.0",
21+
"symfony/dependency-injection": "^2.7|^3.3",
22+
"symfony/config": "^2.7|^3.3"
2123
},
2224
"suggest": {
2325
"ramsey/uuid": "To use the UUID generator you should require this package"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace AppBundle\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Reference;
8+
9+
class GeneratorPass implements CompilerPassInterface
10+
{
11+
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public function process(ContainerBuilder $container)
16+
{
17+
if ( ! $container->has('vivait_generator.registry')) {
18+
return;
19+
}
20+
21+
$definition = $container->findDefinition('vivait_generator.registry');
22+
$allGenerators = $container->findTaggedServiceIds('vivait_generator.generator');
23+
24+
// Loop through all service IDs
25+
foreach ($allGenerators as $id => $tags) {
26+
// Loop through all of the individual tags
27+
foreach ($tags as $attributes) {
28+
$definition->addMethodCall(
29+
'addGenerator',
30+
[
31+
new Reference($id),
32+
$attributes['alias']
33+
]
34+
);
35+
}
36+
}
37+
}
38+
}

src/Vivait/StringGeneratorBundle/DependencyInjection/Configuration.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
66
use Symfony\Component\Config\Definition\ConfigurationInterface;
77

8-
/**
9-
* This is the class that validates and merges configuration from your app/config files
10-
*
11-
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
12-
*/
138
class Configuration implements ConfigurationInterface
149
{
10+
1511
/**
1612
* {@inheritDoc}
1713
*/
@@ -25,7 +21,8 @@ public function getConfigTreeBuilder()
2521
->children()
2622
->arrayNode('generators')
2723
->useAttributeAsKey('class')
28-
->prototype('scalar')->end()
24+
->prototype('scalar')
25+
->end()
2926
->end()
3027
->end();
3128

src/Vivait/StringGeneratorBundle/DependencyInjection/VivaitStringGeneratorExtension.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,33 @@
77
use Symfony\Component\DependencyInjection\Loader;
88
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
99

10-
/**
11-
* This is the class that loads and manages your bundle configuration
12-
*
13-
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
14-
*/
1510
class VivaitStringGeneratorExtension extends ConfigurableExtension
1611
{
12+
1713
/**
1814
* {@inheritdoc}
1915
*/
2016
public function loadInternal(array $mergedConfig, ContainerBuilder $container)
2117
{
22-
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
18+
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
2319
$loader->load('services.yml');
2420

25-
if($mergedConfig['generators'] && $container->hasDefinition('vivait_generator.registry')){
21+
if ($mergedConfig['generators'] && $container->hasDefinition('vivait_generator.registry')) {
22+
@trigger_error(
23+
'Defining Generators in config is deprecated since version 2.0.1 and will be removed in version 3.0. ' .
24+
'Use services tagged with "vivait_generator.generator" and an "alias" instead.',
25+
E_USER_DEPRECATED
26+
);
27+
2628
$registry = $container->findDefinition('vivait_generator.registry');
2729

28-
$registry->replaceArgument(1, $mergedConfig['generators']);
30+
$legacyGenerators = [];
31+
32+
foreach ($mergedConfig['generators'] as $alias => $generatorService) {
33+
$legacyGenerators[$alias] = $container->get($generatorService);
34+
}
35+
36+
$registry->addArgument($legacyGenerators);
2937
}
3038
}
3139
}

src/Vivait/StringGeneratorBundle/EventListener/GeneratorListener.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,29 @@ public function prePersist(LifecycleEventArgs $args)
7575
break;
7676
}
7777

78-
$string = $this->generateString($property->name, $annotation, $object);
78+
$string = $this->generateString($property->name, $annotation, $entity);
7979
$property->setValue($entity, $string);
8080
}
8181
}
8282
}
8383
}
8484

8585
/**
86-
* @param $property
86+
* @param string $property
8787
* @param GeneratorAnnotation $annotation
88-
* @param \ReflectionObject $object
88+
* @param object $entity
89+
*
8990
* @return string
9091
*/
91-
private function generateString($property, GeneratorAnnotation $annotation, $object)
92+
private function generateString($property, GeneratorAnnotation $annotation, $entity)
9293
{
9394
/** @var GeneratorInterface|ConfigurableGeneratorInterface $generator */
9495
$generator = $this->getRegistry()->get($annotation->generator);
9596

9697
$generator->setLength($annotation->length);
9798

9899
if(!empty($annotation->callbacks)){
99-
$this->performCallbacks($generator, $annotation, $object);
100+
$this->performCallbacks($generator, $annotation, $entity);
100101
}
101102

102103
if($generator instanceof ConfigurableGeneratorInterface){
@@ -110,7 +111,7 @@ private function generateString($property, GeneratorAnnotation $annotation, $obj
110111
}
111112

112113
if ($this->repo->findOneBy([$property => $str])) {
113-
return $this->generateString($property, $annotation, $object);
114+
return $this->generateString($property, $annotation, $entity);
114115
} else {
115116
return $str;
116117
}
@@ -133,25 +134,26 @@ public function configureGenerator(ConfigurableGeneratorInterface $generator, $o
133134
}
134135

135136
/**
136-
* @param GeneratorInterface $generator
137+
* @param GeneratorInterface $generator
137138
* @param GeneratorAnnotation $annotation
138-
* @param \ReflectionObject $object
139+
* @param object $object
139140
*/
140141
public function performCallbacks(GeneratorInterface $generator, GeneratorAnnotation $annotation, $object)
141142
{
142143
foreach($annotation->callbacks as $callback => $value){
143-
if($this->isMethod($generator, $callback)){
144-
145-
if($this->isMethod($object, $value)){
144+
if($this->isMethod($generator, $callback)) {
145+
if($this->isMethod($object, $value)) {
146146
$value = $object->$value();
147147
}
148+
148149
$generator->$callback($value);
149-
}
150-
else{
151-
throw new \InvalidArgumentException(sprintf(
152-
'Callback "%s" does not exist in class "%s"',
153-
$callback,
154-
get_class($generator))
150+
} else {
151+
throw new \InvalidArgumentException(
152+
sprintf(
153+
'Callback "%s" does not exist in class "%s"',
154+
$callback,
155+
get_class($generator)
156+
)
155157
);
156158
}
157159
}

src/Vivait/StringGeneratorBundle/Registry/Registry.php

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,50 @@
22

33
namespace Vivait\StringGeneratorBundle\Registry;
44

5-
use Symfony\Component\DependencyInjection\ContainerInterface;
65
use Vivait\StringGeneratorBundle\Model\GeneratorInterface;
76

87
class Registry
98
{
9+
1010
/**
1111
* @var array
1212
*/
1313
private $generators;
1414

1515
/**
16-
* @var ContainerInterface
17-
*/
18-
private $container;
19-
20-
/**
21-
* @param ContainerInterface $container
22-
* @param array $generators
16+
* @param array $legacyGenerators
2317
*/
24-
function __construct(ContainerInterface $container, array $generators = [])
18+
public function __construct(array $legacyGenerators = [])
2519
{
26-
$this->container = $container;
27-
$this->addAll($generators);
20+
$this->generators = $legacyGenerators;
2821
}
2922

3023
/**
31-
* @param $field
32-
* @return GeneratorInterface
33-
* @throws \OutOfBoundsException
24+
* @param GeneratorInterface $generator
25+
* @param string $alias
3426
*/
35-
public function get($field)
27+
public function addGenerator(GeneratorInterface $generator, $alias)
3628
{
37-
if (isset($this->generators[$field])) {
38-
return $this->generators[$field];
29+
if (array_key_exists($alias, $this->generators)) {
30+
throw new \InvalidArgumentException("The alias {$alias} is already a registered Generator.");
3931
}
4032

41-
throw new \OutOfBoundsException(sprintf('Field "%s" not found in registry', $field));
42-
}
43-
44-
/**
45-
* @param $field
46-
* @param $class
47-
* @return $this
48-
*/
49-
public function add($field, $class)
50-
{
51-
$this->generators[$field] = $this->resolveGeneratorType($class);
52-
return $this;
53-
}
54-
55-
/**
56-
* @param array $generators
57-
* @return $this
58-
*/
59-
public function addAll(array $generators)
60-
{
61-
foreach ($generators as $field => $class) {
62-
$this->add($field, $class);
63-
}
64-
return $this;
33+
$this->generators[$alias] = $generator;
6534
}
6635

6736
/**
68-
* @param $class
37+
* @param string $field
38+
*
39+
* @throws \OutOfBoundsException
40+
*
6941
* @return GeneratorInterface
7042
*/
71-
private function resolveGeneratorType($class)
43+
public function get($field)
7244
{
73-
if ($class instanceof GeneratorInterface) {
74-
return $class;
75-
} elseif (($service = $this->container->get($class, ContainerInterface::NULL_ON_INVALID_REFERENCE))) {
76-
return $service;
77-
} elseif (is_a($class, 'Vivait\StringGeneratorBundle\Generator\GeneratorInterface', true)) {
78-
return new $class;
45+
if (isset($this->generators[$field])) {
46+
return $this->generators[$field];
7947
}
8048

81-
throw new \InvalidArgumentException('Invalid Generator');
49+
throw new \OutOfBoundsException(sprintf('Field "%s" not found in registry', $field));
8250
}
8351
}

0 commit comments

Comments
 (0)