Skip to content

Commit a640e69

Browse files
author
Robin Cawser
committed
Allow custom generator classes to be used
1 parent 7cad21c commit a640e69

7 files changed

Lines changed: 56 additions & 18 deletions

File tree

DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public function getConfigTreeBuilder()
1919
{
2020
$treeBuilder = new TreeBuilder();
2121
$rootNode = $treeBuilder->root('vivait_string_generator');
22+
$rootNode
23+
->children()
24+
->scalarNode('generator_class')->defaultValue('Vivait\StringGeneratorBundle\Generator\StringGenerator')->end()
25+
;
2226

2327

2428
// Here you should define the parameters that are allowed to

DependencyInjection/VivaitStringGeneratorExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ public function load(array $configs, ContainerBuilder $container)
2424

2525
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
2626
$loader->load('services.yml');
27+
$container->setParameter('vivait_string_generator.generator_class', $config['generator_class']);
2728
}
2829
}
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
use Doctrine\Common\Annotations\Reader;
66
use Doctrine\ORM\Event\LifecycleEventArgs;
77
use Vivait\StringGeneratorBundle\Annotation as Vivait;
8-
use Vivait\StringGeneratorBundle\Generator\StringGenerator;
9-
use Vivait\StringGeneratorBundle\Model\StringGeneratorInterface;
8+
use Vivait\StringGeneratorBundle\Model\GeneratorInterface;
109

11-
class IdGeneratorListener
10+
class StringGeneratorListener
1211
{
1312
private $reader;
1413
private $repo;
14+
/**
15+
* @var GeneratorInterface
16+
*/
17+
private $generator;
1518

16-
public function __construct(Reader $reader)
19+
public function __construct(Reader $reader, GeneratorInterface $generator)
1720
{
1821
$this->reader = $reader;
22+
$this->generator = $generator;
1923
}
2024

2125
/**
@@ -58,22 +62,23 @@ public function prePersist(LifecycleEventArgs $args)
5862
*/
5963
private function generateId($property, Vivait\StringGenerator $annotation)
6064
{
61-
$generator = new StringGenerator();
62-
$generator
65+
$this->generator
6366
->setAlphabet($annotation->alphabet)
6467
->setLength($annotation->length);
6568

66-
$id = $generator->generate();
69+
$str = $this->generator->generate();
6770

6871
if ($annotation->prefix) {
69-
$id = sprintf("%s%s%s", $annotation->prefix, $annotation->separator, $id);
72+
$str = sprintf("%s%s%s", $annotation->prefix, $annotation->separator, $str);
7073
}
7174

72-
if ($this->repo->findOneBy([$property => $id])) {
75+
if ($this->repo->findOneBy([$property => $str])) {
7376
$this->generateId($property, $annotation);
7477
} else {
75-
return $id;
78+
return $str;
7679
}
80+
81+
7782
}
7883

7984

Generator/StringGenerator.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,43 @@
22

33
namespace Vivait\StringGeneratorBundle\Generator;
44

5-
use Vivait\StringGeneratorBundle\Model\StringGeneratorInterface;
5+
use Vivait\StringGeneratorBundle\Model\GeneratorInterface;
66

7-
class StringGenerator implements StringGeneratorInterface
7+
class StringGenerator implements GeneratorInterface
88
{
99

1010
private $alphabet;
1111
private $length;
1212

13+
14+
/**
15+
* Set the characters that can be used to generate the string
16+
*
17+
* @param $alphabet
18+
* @return $this
19+
*/
1320
public function setAlphabet($alphabet)
1421
{
1522
$this->alphabet = $alphabet;
1623
return $this;
1724
}
1825

26+
/**
27+
* Set the length of the generated string
28+
* @param $length
29+
* @return $this
30+
*/
1931
public function setLength($length)
2032
{
2133
$this->length = $length;
2234
return $this;
2335
}
2436

37+
/**
38+
* Creates a random string based on a length and alphabet
39+
*
40+
* @return string
41+
*/
2542
public function generate()
2643
{
2744
$str = [];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Vivait\StringGeneratorBundle\Model;
44

5-
interface StringGeneratorInterface
5+
interface GeneratorInterface
66
{
77

88
/**

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,16 @@ A prefix can be obtained via a callback to a method in the entity using `prefix_
8080
return $this->category->getCode();
8181
}
8282

83-
### Custom characters
83+
### Alphabet
8484

8585
Setting `alphabet` limits the characters the generator can choose from. Defaults to alphanumeric.
8686

87-
@Vivait\StringGenerator(alphabet="abcdefghkmnpqrstuwxyz")
87+
@Vivait\StringGenerator(alphabet="abcdefghkmnpqrstuwxyz")
88+
89+
## Custom generator
90+
91+
If you want to use a different generator to create your string, create a class that implements namespace
92+
`Vivait\StringGeneratorBundle\Model\GeneratorInterface`. Add the fully qualified classname to your config.yml:
93+
94+
vivait_string_generator:
95+
generator_class: Acme\BlogBundle\Generator\UsernameGenerator

Resources/config/services.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
parameters:
22

33
services:
4+
vivait_stringgenerator.generator:
5+
class: "%vivait_string_generator.generator_class%"
46
vivait_stringgenerator.generator.listener:
5-
class: Vivait\StringGeneratorBundle\EventListener\IdGeneratorListener
6-
arguments: ["@annotation_reader"]
7+
class: Vivait\StringGeneratorBundle\EventListener\StringGeneratorListener
8+
arguments: ["@annotation_reader", "@vivait_stringgenerator.generator"]
79
tags:
8-
- { name: doctrine.event_listener, event: prePersist }
10+
- { name: doctrine.event_listener, event: prePersist }
11+

0 commit comments

Comments
 (0)