1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the DoyoUserBundle project. |
5
|
|
|
* |
6
|
|
|
* (c) Anthonius Munthi <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Doyo\UserBundle\DependencyInjection; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\Config\ConfigCache; |
17
|
|
|
use Symfony\Component\Config\Definition\Processor; |
18
|
|
|
use Symfony\Component\Config\FileLocator; |
19
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
20
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
21
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
22
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
23
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
24
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
25
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
26
|
|
|
|
27
|
|
|
class DoyoUserExtension extends Extension implements PrependExtensionInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private static $doctrineDrivers = [ |
33
|
|
|
'orm' => [ |
34
|
|
|
'registry' => 'doctrine', |
35
|
|
|
'tag' => 'doctrine.event_subscriber', |
36
|
|
|
], |
37
|
|
|
'mongodb' => [ |
38
|
|
|
'registry' => 'doctrine_mongodb', |
39
|
|
|
'tag' => 'doctrine_mongodb.odm.event_subscriber', |
40
|
|
|
], |
41
|
|
|
'couchdb' => [ |
42
|
|
|
'registry' => 'doctrine_couchdb', |
43
|
|
|
'tag' => 'doctrine_couchdb.event_subscriber', |
44
|
|
|
'listener_class' => 'Doyo\UserBundle\Bridge\CouchDB\UserListener', |
45
|
|
|
], |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
public function prepend(ContainerBuilder $container) |
49
|
|
|
{ |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function load(array $configs, ContainerBuilder $container) |
53
|
|
|
{ |
54
|
1 |
|
$processor = new Processor(); |
55
|
1 |
|
$configuration = new Configuration(); |
56
|
|
|
|
57
|
1 |
|
$config = $processor->processConfiguration($configuration, $configs); |
58
|
|
|
|
59
|
1 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
60
|
|
|
|
61
|
1 |
|
$loader->load('util.xml'); |
62
|
1 |
|
$loader->load('command.xml'); |
63
|
|
|
|
64
|
1 |
|
$this->loadDbDriver($loader, $container, $config); |
65
|
|
|
|
66
|
1 |
|
$container->setParameter('doyo_user.user_class', $config['user_class']); |
67
|
1 |
|
$container->setParameter('doyo_user.model_manager_name', $config['model_manager_name']); |
68
|
1 |
|
$container->setParameter('doyo_user.api_platform', $config['api_platform']); |
69
|
1 |
|
$container->setParameter('doyo_user.backend_type_orm', true); |
70
|
|
|
|
71
|
1 |
|
$container->setAlias('doyo_user.util.email_canonicalizer', $config['service']['email_canonicalizer']); |
72
|
1 |
|
$container->setAlias('doyo_user.util.username_canonicalizer', $config['service']['username_canonicalizer']); |
73
|
1 |
|
$container->setAlias('doyo_user.util.password_updater', $config['service']['password_updater']); |
74
|
1 |
|
$container->setAlias('doyo_user.user_manager', $config['service']['user_manager']); |
75
|
|
|
|
76
|
1 |
|
if ($config['api_platform']) { |
77
|
|
|
$this->loadApiPlatform($container); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
private function loadDbDriver(XmlFileLoader $loader, ContainerBuilder $container, $config) |
82
|
|
|
{ |
83
|
1 |
|
if ('custom' !== $config['db_driver']) { |
84
|
1 |
|
if (isset(self::$doctrineDrivers[$config['db_driver']])) { |
85
|
1 |
|
$loader->load('doctrine.xml'); |
86
|
1 |
|
$container->setAlias('doyo_user.doctrine_registry', new Alias(self::$doctrineDrivers[$config['db_driver']]['registry'], false)); |
87
|
|
|
} else { |
88
|
|
|
$loader->load(sprintf('%s.xml', $config['db_driver'])); |
89
|
|
|
} |
90
|
1 |
|
$container->setParameter($this->getAlias().'.backend_type_'.$config['db_driver'], true); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
if (isset(self::$doctrineDrivers[$config['db_driver']])) { |
94
|
1 |
|
$definition = $container->getDefinition('doyo_user.object_manager'); |
95
|
1 |
|
$definition->setFactory([new Reference('doyo_user.doctrine_registry'), 'getManager']); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function loadApiPlatform($container) |
100
|
|
|
{ |
101
|
|
|
$this->generateApiResourceCache($container); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function generateApiResourceCache(ContainerBuilder $container) |
105
|
|
|
{ |
106
|
|
|
$dir = __DIR__.'/../Resources/config/api_resources'; |
107
|
|
|
if (!is_dir($dir)) { |
108
|
|
|
mkdir($dir); |
109
|
|
|
} |
110
|
|
|
$path = $dir.'/User.yaml'; |
111
|
|
|
$meta = $path.'.meta'; |
112
|
|
|
$cache = new ConfigCache($path, false); |
113
|
|
|
|
114
|
|
|
if (!$cache->isFresh() || !is_file($meta)) { |
115
|
|
|
$template = __DIR__.'/../Resources/config/template/user-resource.yaml'; |
116
|
|
|
$contents = file_get_contents($template); |
117
|
|
|
$contents = strtr($contents, [ |
118
|
|
|
'%doyo_user.user_class%' => $container->getParameter('doyo_user.user_class'), |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
//file_put_contents($dir.'/user-resource.yaml', $contents, LOCK_EX); |
122
|
|
|
$resources = [new FileResource($template)]; |
123
|
|
|
$cache->write($contents, $resources); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|