1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use FOS\UserBundle\Form\Type\ResettingFormType; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\Config\FileLocator; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
11
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
12
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
13
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
14
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This is the class that loads and manages your bundle configuration |
19
|
|
|
* |
20
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
21
|
|
|
*/ |
22
|
|
|
class KunstmaanAdminExtension extends Extension implements PrependExtensionInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Loads a specific configuration. |
26
|
|
|
* |
27
|
|
|
* @param array $configs An array of configuration values |
28
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance |
29
|
|
|
* |
30
|
|
|
* @throws InvalidArgumentException When provided tag is not defined in this extension |
31
|
|
|
*/ |
32
|
|
|
public function load(array $configs, ContainerBuilder $container) |
33
|
|
|
{ |
34
|
|
|
$container->setParameter('version_checker.url', 'https://bundles.kunstmaan.be/version-check'); |
35
|
|
|
$container->setParameter('version_checker.timeframe', 60*60*24); |
36
|
|
|
$container->setParameter('version_checker.enabled', true); |
37
|
|
|
|
38
|
|
|
$configuration = new Configuration(); |
39
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
40
|
|
|
|
41
|
|
|
if (array_key_exists('dashboard_route', $config)) { |
42
|
|
|
$container->setParameter('kunstmaan_admin.dashboard_route', $config['dashboard_route']); |
43
|
|
|
} |
44
|
|
|
if (array_key_exists('admin_password', $config)) { |
45
|
|
|
$container->setParameter('kunstmaan_admin.admin_password', $config['admin_password']); |
46
|
|
|
} |
47
|
|
|
$container->setParameter('kunstmaan_admin.admin_locales', $config['admin_locales']); |
48
|
|
|
$container->setParameter('kunstmaan_admin.default_admin_locale', $config['default_admin_locale']); |
49
|
|
|
|
50
|
|
|
$container->setParameter('kunstmaan_admin.session_security.ip_check', $config['session_security']['ip_check']); |
51
|
|
|
$container->setParameter('kunstmaan_admin.session_security.user_agent_check', $config['session_security']['user_agent_check']); |
52
|
|
|
|
53
|
|
|
$container->setParameter('kunstmaan_admin.admin_prefix', $this->normalizeUrlSlice($config['admin_prefix'])); |
54
|
|
|
|
55
|
|
|
$container->setParameter('kunstmaan_admin.admin_exception_excludes', $config['admin_exception_excludes']); |
56
|
|
|
|
57
|
|
|
$container->setParameter('kunstmaan_admin.google_signin.enabled', $config['google_signin']['enabled']); |
58
|
|
|
$container->setParameter('kunstmaan_admin.google_signin.client_id', $config['google_signin']['client_id']); |
59
|
|
|
$container->setParameter('kunstmaan_admin.google_signin.client_secret', $config['google_signin']['client_secret']); |
60
|
|
|
$container->setParameter('kunstmaan_admin.google_signin.hosted_domains', $config['google_signin']['hosted_domains']); |
61
|
|
|
|
62
|
|
|
$container->setParameter('kunstmaan_admin.password_restrictions.min_digits' , $config['password_restrictions']['min_digits']); |
63
|
|
|
$container->setParameter('kunstmaan_admin.password_restrictions.min_uppercase' , $config['password_restrictions']['min_uppercase']); |
64
|
|
|
$container->setParameter('kunstmaan_admin.password_restrictions.min_special_characters' , $config['password_restrictions']['min_special_characters']); |
65
|
|
|
$container->setParameter('kunstmaan_admin.password_restrictions.min_length' , $config['password_restrictions']['min_length']); |
66
|
|
|
$container->setParameter('kunstmaan_admin.password_restrictions.max_length' , $config['password_restrictions']['max_length']); |
67
|
|
|
$container->setParameter('kunstmaan_admin.enable_toolbar_helper', $config['enable_toolbar_helper']); |
68
|
|
|
$container->setParameter('kunstmaan_admin.toolbar_firewall_names', !empty($config['provider_keys']) ? $config['provider_keys'] : $config['toolbar_firewall_names']); |
69
|
|
|
$container->setParameter('kunstmaan_admin.admin_firewall_name', $config['admin_firewall_name']); |
70
|
|
|
|
71
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
72
|
|
|
$loader->load('services.yml'); |
73
|
|
|
|
74
|
|
|
if (!empty($config['enable_console_exception_listener']) && $config['enable_console_exception_listener']) { |
75
|
|
|
$loader->load('console_listener.yml'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (0 !== count($config['menu_items'])) { |
79
|
|
|
$this->addSimpleMenuAdaptor($container, $config['menu_items']); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function prepend(ContainerBuilder $container) |
84
|
|
|
{ |
85
|
|
|
$knpMenuConfig['twig'] = true; // set to false to disable the Twig extension and the TwigRenderer |
|
|
|
|
86
|
|
|
$knpMenuConfig['templating'] = false; // if true, enables the helper for PHP templates |
87
|
|
|
$knpMenuConfig['default_renderer'] = 'twig'; // The renderer to use, list is also available by default |
88
|
|
|
$container->prependExtensionConfig('knp_menu', $knpMenuConfig); |
89
|
|
|
|
90
|
|
|
$fosUserConfig['db_driver'] = 'orm'; // other valid values are 'mongodb', 'couchdb' |
|
|
|
|
91
|
|
|
$fosUserConfig['firewall_name'] = 'main'; |
92
|
|
|
$fosUserConfig['user_class'] = 'Kunstmaan\AdminBundle\Entity\User'; |
93
|
|
|
$fosUserConfig['group']['group_class'] = 'Kunstmaan\AdminBundle\Entity\Group'; |
94
|
|
|
$fosUserConfig['resetting']['token_ttl'] = 86400; |
95
|
|
|
// Use this node only if you don't want the global email address for the resetting email |
96
|
|
|
$fosUserConfig['resetting']['email']['from_email']['address'] = '[email protected]'; |
97
|
|
|
$fosUserConfig['resetting']['email']['from_email']['sender_name'] = 'admin'; |
98
|
|
|
$fosUserConfig['resetting']['email']['template'] = 'FOSUserBundle:Resetting:email.txt.twig'; |
99
|
|
|
$fosUserConfig['resetting']['form']['type'] = ResettingFormType::class; |
100
|
|
|
$fosUserConfig['resetting']['form']['name'] = 'fos_user_resetting_form'; |
101
|
|
|
$fosUserConfig['resetting']['form']['validation_groups'] = ['ResetPassword']; |
102
|
|
|
$container->prependExtensionConfig('fos_user', $fosUserConfig); |
103
|
|
|
|
104
|
|
|
$monologConfig['handlers']['main']['type'] = 'rotating_file'; |
|
|
|
|
105
|
|
|
$monologConfig['handlers']['main']['path'] = sprintf('%s/%s', $container->getParameter('kernel.logs_dir'), $container->getParameter('kernel.environment')); |
106
|
|
|
$monologConfig['handlers']['main']['level'] = 'debug'; |
107
|
|
|
$container->prependExtensionConfig('monolog', $monologConfig); |
108
|
|
|
|
109
|
|
|
$configs = $container->getExtensionConfig($this->getAlias()); |
110
|
|
|
$this->processConfiguration(new Configuration(), $configs); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritDoc} |
115
|
|
|
*/ |
116
|
|
|
public function getNamespace() |
117
|
|
|
{ |
118
|
|
|
return 'http://bundles.kunstmaan.be/schema/dic/admin'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritDoc} |
123
|
|
|
*/ |
124
|
|
|
public function getXsdValidationBasePath() |
125
|
|
|
{ |
126
|
|
|
return __DIR__.'/../Resources/config/schema'; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function addSimpleMenuAdaptor(ContainerBuilder $container, array $menuItems) |
130
|
|
|
{ |
131
|
|
|
$definition = new Definition('Kunstmaan\AdminBundle\Helper\Menu\SimpleMenuAdaptor', [ |
132
|
|
|
new Reference('security.authorization_checker'), |
133
|
|
|
$menuItems |
134
|
|
|
]); |
135
|
|
|
$definition->addTag('kunstmaan_admin.menu.adaptor'); |
136
|
|
|
|
137
|
|
|
$container->setDefinition('kunstmaan_admin.menu.adaptor.simple', $definition); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $urlSlice |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
protected function normalizeUrlSlice($urlSlice) |
146
|
|
|
{ |
147
|
|
|
/* Get rid of exotic characters that would break the url */ |
148
|
|
|
$urlSlice = filter_var($urlSlice, FILTER_SANITIZE_URL); |
149
|
|
|
|
150
|
|
|
/* Remove leading and trailing slashes */ |
151
|
|
|
$urlSlice = trim($urlSlice, '/'); |
152
|
|
|
|
153
|
|
|
/* Make sure our $urlSlice is literally used in our regex */ |
154
|
|
|
$urlSlice = preg_quote($urlSlice); |
155
|
|
|
|
156
|
|
|
return $urlSlice; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.