1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saxulum\BundleProvider\Provider; |
4
|
|
|
|
5
|
|
|
use Silex\Application; |
6
|
|
|
use Silex\ServiceProviderInterface; |
7
|
|
|
|
8
|
|
|
abstract class AbstractBundleProvider implements ServiceProviderInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \ReflectionClass |
12
|
|
|
*/ |
13
|
|
|
protected $reflectionClass; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param Application $app |
17
|
|
|
*/ |
18
|
|
View Code Duplication |
protected function addCommands(Application $app) |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$path = $this->getPath(); |
21
|
|
|
$app['console.command.paths'] = $app->share( |
22
|
|
|
$app->extend('console.command.paths', function ($paths) use ($path) { |
23
|
|
|
$paths[] = $path . '/Command'; |
24
|
|
|
|
25
|
|
|
return $paths; |
26
|
|
|
}) |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Application $app |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
protected function addControllers(Application $app) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$path = $this->getPath(); |
36
|
|
|
$app['route_controller_paths'] = $app->share( |
37
|
|
|
$app->extend('route_controller_paths', function ($paths) use ($path) { |
38
|
|
|
$paths[] = $path . '/Controller'; |
39
|
|
|
|
40
|
|
|
return $paths; |
41
|
|
|
}) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Application $app |
47
|
|
|
*/ |
48
|
|
|
protected function addDoctrineOrmMappings(Application $app) |
49
|
|
|
{ |
50
|
|
|
$namespace = $this->getNamespace(); |
51
|
|
|
$path = $this->getPath(); |
52
|
|
|
|
53
|
|
|
if (!isset($app['orm.ems.options'])) { |
54
|
|
|
$app['orm.ems.options'] = $app->share(function () use ($app) { |
55
|
|
|
$options = array( |
56
|
|
|
'default' => $app['orm.em.default_options'] |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
return $options; |
60
|
|
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$app['orm.ems.options'] = $app->share($app->extend('orm.ems.options', function (array $options) use ($namespace, $path) { |
64
|
|
|
$options['default']['mappings'][] = array( |
65
|
|
|
'type' => 'annotation', |
66
|
|
|
'namespace' => $namespace . '\Entity', |
67
|
|
|
'path' => $path .'/Entity', |
68
|
|
|
'use_simple_annotation_reader' => false, |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
return $options; |
72
|
|
|
})); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Application $app |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
protected function addTranslatorResources(Application $app) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$path = $this->getPath(); |
81
|
|
|
$app['translation_paths'] = $app->share( |
82
|
|
|
$app->extend('translation_paths', function ($paths) use ($path) { |
83
|
|
|
$paths[] = $path . '/Resources/translations'; |
84
|
|
|
|
85
|
|
|
return $paths; |
86
|
|
|
}) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Application $app |
92
|
|
|
* @deprecated |
93
|
|
|
*/ |
94
|
|
|
protected function addTranslatorRessources(Application $app) |
95
|
|
|
{ |
96
|
|
|
$this->addTranslatorResources($app); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Application $app |
101
|
|
|
*/ |
102
|
|
|
protected function addTwigLoaderFilesystemPath(Application $app) |
103
|
|
|
{ |
104
|
|
|
$path = $this->getPath(); |
105
|
|
|
$twigNamespace = str_replace('\\', '', $this->getNamespace()); |
106
|
|
|
|
107
|
|
|
$app['twig.loader.filesystem'] = $app->share($app->extend('twig.loader.filesystem', |
108
|
|
|
function (\Twig_Loader_Filesystem $twigLoaderFilesystem) use ($path, $twigNamespace) { |
109
|
|
|
$twigLoaderFilesystem->addPath($path. '/Resources/views', $twigNamespace); |
110
|
|
|
|
111
|
|
|
return $twigLoaderFilesystem; |
112
|
|
|
} |
113
|
|
|
)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
protected function getNamespace() |
120
|
|
|
{ |
121
|
|
|
return $this->getReflectionClass()->getNamespaceName(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
protected function getPath() |
128
|
|
|
{ |
129
|
|
|
return dirname($this->getReflectionClass()->getFileName()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return \ReflectionClass |
134
|
|
|
*/ |
135
|
|
|
protected function getReflectionClass() |
136
|
|
|
{ |
137
|
|
|
if (is_null($this->reflectionClass)) { |
138
|
|
|
$this->reflectionClass = new \ReflectionClass(get_class($this)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->reflectionClass; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.