1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Go! AOP framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2015, Lisachenko Alexander <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Go\Symfony\GoAopBundle\DependencyInjection; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use Go\Aop\Aspect; |
15
|
|
|
use Symfony\Component\Config\FileLocator; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
18
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
19
|
|
|
|
20
|
|
|
class GoAopExtension extends Extension |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function getNamespace() |
26
|
|
|
{ |
27
|
|
|
return 'http://go.aopphp.com/xsd-schema/go-aop-bundle'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function getXsdValidationBasePath() |
34
|
|
|
{ |
35
|
|
|
return __DIR__ . '/../Resources/config/schema'; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Loads a specific configuration. |
40
|
|
|
* |
41
|
|
|
* @param array $config An array of configuration values |
42
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance |
43
|
|
|
* |
44
|
|
|
* @throws \InvalidArgumentException When provided tag is not defined in this extension |
45
|
|
|
* |
46
|
|
|
* @api |
47
|
|
|
*/ |
48
|
|
|
public function load(array $config, ContainerBuilder $container) |
49
|
|
|
{ |
50
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
51
|
|
|
$loader->load('services.xml'); |
52
|
|
|
$loader->load('commands.xml'); |
53
|
|
|
|
54
|
|
|
$configurator = new Configuration(); |
55
|
|
|
$config = $this->processConfiguration($configurator, $config); |
56
|
|
|
|
57
|
|
|
$normalizedOptions = array(); |
58
|
|
|
foreach ($config['options'] as $optionKey => $value) { |
59
|
|
|
// this will convert 'under_scores' into 'underScores' |
60
|
|
|
$optionKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $optionKey)))); |
61
|
|
|
$normalizedOptions[$optionKey] = $value; |
62
|
|
|
} |
63
|
|
|
$container->setParameter('goaop.options', $normalizedOptions); |
64
|
|
|
|
65
|
|
|
if ($config['cache_warmer']) { |
66
|
|
|
$definition = $container->getDefinition('goaop.cache.warmer'); |
67
|
|
|
$definition->addTag('kernel.cache_warmer'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($config['doctrine_support']) { |
71
|
|
|
$container |
72
|
|
|
->getDefinition('goaop.bridge.doctrine.metadata_load_interceptor') |
73
|
|
|
->addTag('doctrine.event_subscriber'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Service autoconfiguration is available in Symfony 3.3+ |
77
|
|
|
if (method_exists($container, 'registerForAutoconfiguration')) { |
78
|
|
|
$container |
79
|
|
|
->registerForAutoconfiguration(Aspect::class) |
80
|
|
|
->addTag('goaop.aspect'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.