Skip to content

Commit 7116f3a

Browse files
authored
Merge pull request #7 from mikel888/master
Add automatic generation of SQL services
2 parents 07ed5b6 + de1a6f4 commit 7116f3a

5 files changed

Lines changed: 94 additions & 4 deletions

File tree

docs/reference_configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ben_gor_file:
66
file_class:
77
file:
88
class: ~ # Required
9-
persistence: doctrine_orm # Also, it can be "doctrine_odm_mongodb"
9+
persistence: doctrine_orm # Also, it can be "sql"
1010
storage: symfony # Also, it can be "gaufrette"
1111
upload_strategy: default # Also, it can be "by_hash" or "suffix_number"
1212
upload_destination: '%kernel.root_dir%/../web' # In Gaufrette storage is a configured Gaufrette filesystem

docs/reference_services.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ bengor_file.file.filesystem BenGorFile\GaufretteFilesystemBridge\In
2323
BenGorFile\SymfonyFilesystemBridge\Infrastructure\Domain\Model\SymfonyFilesystem
2424

2525
bengor_file.file.repository BenGorFile\DoctrineORMBridge\Infrastructure\Persistence\DoctrineORMFileRepository
26+
BenGorFile\File\Infrastructure\Persistence\Sql\SqlFileRepository
2627
BenGorFile\DoctrineODMMongoDBBridge\Infrastructure\Persistence\DoctrineODMMongoDBFileRepository
27-
28-
28+
29+
bengor_file.file.specification_factory BenGorFile\DoctrineORMBridge\Infrastructure\Persistence\DoctrineORMFileSpecificationFactory
30+
BenGorFile\File\Infrastructure\Persistence\Sql\SqlFileSpecificationFactory
31+
2932

3033
// Creates by Symfony DIC to the correct use of the bundle. Please, don't use them.
3134

src/BenGorFile/FileBundle/BenGorFileBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use BenGorFile\FileBundle\DependencyInjection\Compiler\ApplicationQueriesPass;
1818
use BenGorFile\FileBundle\DependencyInjection\Compiler\DomainServicesPass;
1919
use BenGorFile\FileBundle\DependencyInjection\Compiler\RoutesPass;
20+
use BenGorFile\FileBundle\DependencyInjection\Compiler\SqlServicesPass;
2021
use BenGorFile\FileBundle\DependencyInjection\Compiler\TwigPass;
2122
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
2223
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -35,6 +36,7 @@ class BenGorFileBundle extends Bundle
3536
public function build(ContainerBuilder $container)
3637
{
3738
$container->addCompilerPass(new DomainServicesPass(), PassConfig::TYPE_OPTIMIZE);
39+
$container->addCompilerPass(new SqlServicesPass(), PassConfig::TYPE_OPTIMIZE);
3840
$container->addCompilerPass(new ApplicationCommandsPass(), PassConfig::TYPE_OPTIMIZE);
3941
$container->addCompilerPass(new ApplicationDataTransformersPass(), PassConfig::TYPE_OPTIMIZE);
4042
$container->addCompilerPass(new ApplicationQueriesPass(), PassConfig::TYPE_OPTIMIZE);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the BenGorFile package.
5+
*
6+
* (c) Beñat Espiña <benatespina@gmail.com>
7+
* (c) Gorka Laucirica <gorka.lauzirika@gmail.com>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace BenGorFile\FileBundle\DependencyInjection\Compiler;
14+
15+
use BenGorFile\File\Infrastructure\Persistence\Sql\SqlFileRepository;
16+
use BenGorFile\File\Infrastructure\Persistence\Sql\SqlFileSpecificationFactory;
17+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Definition;
20+
use Symfony\Component\DependencyInjection\Reference;
21+
22+
/**
23+
* Register Sql services compiler pass.
24+
*
25+
* Service declaration via PHP allows more
26+
* flexibility with customization extend files.
27+
*
28+
* @author Mikel Etxebarria <mikeletxe4594@gmail.com>
29+
*/
30+
class SqlServicesPass implements CompilerPassInterface
31+
{
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function process(ContainerBuilder $container)
36+
{
37+
$config = $container->getParameter('bengor_file.config');
38+
foreach ($config['file_class'] as $key => $file) {
39+
if ('sql' !== $file['persistence']) {
40+
continue;
41+
}
42+
43+
if (!$container->hasDefinition('bengor.file.infrastructure.persistence.pdo')) {
44+
$container->setDefinition(
45+
'bengor.file.infrastructure.persistence.pdo',
46+
(new Definition(
47+
\PDO::class, [
48+
"mysql:host=%database_host%;dbname=%database_name%",
49+
"%database_user%",
50+
"%database_password%",
51+
[],
52+
]
53+
))->setPublic(false)
54+
);
55+
}
56+
57+
$container->setDefinition(
58+
'bengor.file.infrastructure.persistence.' . $key . '_repository',
59+
(new Definition(
60+
SqlFileRepository::class, [
61+
new Reference('bengor.file.infrastructure.persistence.pdo'),
62+
]
63+
))->setPublic(false)
64+
);
65+
66+
$container->setAlias(
67+
'bengor_file.' . $key . '.repository',
68+
'bengor.file.infrastructure.persistence.' . $key . '_repository'
69+
);
70+
71+
$container->setDefinition(
72+
'bengor.file.infrastructure.persistence.' . $key . '_specification_factory',
73+
(new Definition(
74+
SqlFileSpecificationFactory::class
75+
))->setPublic(false)
76+
);
77+
78+
$container->setAlias(
79+
'bengor_file.' . $key . '.specification_factory',
80+
'bengor.file.infrastructure.persistence.' . $key . '_specification_factory'
81+
);
82+
}
83+
}
84+
}
85+

src/BenGorFile/FileBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function getConfigTreeBuilder()
3939
->scalarNode('persistence')
4040
->defaultValue('doctrine_orm')
4141
->validate()
42-
->ifNotInArray(['doctrine_orm'])
42+
->ifNotInArray(['doctrine_orm', 'sql'])
4343
->thenInvalid('Invalid persistence layer "%s"')
4444
->end()
4545
->end()

0 commit comments

Comments
 (0)