|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Knp\RadBundle\AppBundle; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
6
|
|
|
use Symfony\Component\Config\FileLocator; |
|
7
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
9
|
|
|
use Symfony\Component\Config\Resource\DirectoryResource; |
|
10
|
|
|
use Symfony\Component\Config\Definition\Processor; |
|
11
|
|
|
|
|
12
|
|
|
class ContainerExtension extends Extension |
|
13
|
|
|
{ |
|
14
|
|
|
private $bundle; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(ConfigurableBundleInterface $bundle, ConfigurationFactory $configFactory = null, Processor $configProcessor = null) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->bundle = $bundle; |
|
19
|
|
|
$this->configFactory = $configFactory ?: new ConfigurationFactory; |
|
|
|
|
|
|
20
|
|
|
$this->configProcessor = $configProcessor ?: new Processor; |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function getAlias() |
|
24
|
|
|
{ |
|
25
|
|
|
return strtolower(str_replace('Bundle', '', $this->bundle->getName())); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
32
|
|
|
{ |
|
33
|
|
|
$path = $this->bundle->getPath(); |
|
34
|
|
|
|
|
35
|
|
|
$environment = $container->getParameter('kernel.environment'); |
|
36
|
|
|
$loader = new Loader\YamlFileLoader( |
|
37
|
|
|
$container, new FileLocator($path.'/Resources/config') |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
if (file_exists($path.'/Resources/config/services.yml')) { |
|
41
|
|
|
$loader->load('services.yml'); |
|
42
|
|
|
} |
|
43
|
|
|
if (file_exists($path.'/Resources/config/services_'.$environment.'.yml')) { |
|
44
|
|
|
$loader->load('services_'.$environment.'.yml'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$paths = array( |
|
48
|
|
|
'Entity', |
|
49
|
|
|
'Form', |
|
50
|
|
|
'Security', |
|
51
|
|
|
'Twig', |
|
52
|
|
|
'Validator'.DIRECTORY_SEPARATOR.'Constraints', |
|
53
|
|
|
'Resources'.DIRECTORY_SEPARATOR.'config', |
|
54
|
|
|
'Resources'.DIRECTORY_SEPARATOR.'translations', |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($paths as $dir) { |
|
58
|
|
|
if (is_dir($dirPath = $path.DIRECTORY_SEPARATOR.$dir)) { |
|
59
|
|
|
$container->addResource(new DirectoryResource($dirPath)); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$configuration = $this->getConfiguration($configs, $container); |
|
64
|
|
|
$config = $this->configProcessor->processConfiguration($configuration, $configs); |
|
65
|
|
|
|
|
66
|
|
|
$this->bundle->buildContainer($config, $container); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getConfiguration(array $configs, ContainerBuilder $container) |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->configFactory->createConfiguration( |
|
72
|
|
|
$this->bundle, |
|
73
|
|
|
$configs, |
|
74
|
|
|
$container |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: