These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Oro\Bundle\RequireJSBundle\Provider; |
||
4 | |||
5 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
6 | use Symfony\Component\Yaml\Yaml; |
||
7 | use Symfony\Component\Templating\TemplateReferenceInterface; |
||
8 | use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
||
9 | |||
10 | use Doctrine\Common\Cache\CacheProvider; |
||
11 | |||
12 | use Oro\Component\PhpUtils\ArrayUtil; |
||
13 | |||
14 | class Config |
||
15 | { |
||
16 | const REQUIREJS_CONFIG_CACHE_KEY = 'requirejs_config'; |
||
17 | |||
18 | /** |
||
19 | * @var \Doctrine\Common\Cache\CacheProvider |
||
20 | */ |
||
21 | protected $cache; |
||
22 | |||
23 | /** |
||
24 | * @var ContainerInterface |
||
25 | */ |
||
26 | protected $container; |
||
27 | |||
28 | /** |
||
29 | * @var EngineInterface |
||
30 | */ |
||
31 | protected $templating; |
||
32 | |||
33 | /** |
||
34 | * @var string|TemplateReferenceInterface |
||
35 | */ |
||
36 | protected $template; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $collectedConfig; |
||
42 | |||
43 | /** |
||
44 | * @param ContainerInterface $container |
||
45 | * @param EngineInterface $templating |
||
46 | * @param $template |
||
47 | */ |
||
48 | public function __construct(ContainerInterface $container, EngineInterface $templating, $template) |
||
49 | { |
||
50 | $this->container = $container; |
||
51 | $this->templating = $templating; |
||
52 | $this->template = $template; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Set cache instance |
||
57 | * |
||
58 | * @param \Doctrine\Common\Cache\CacheProvider $cache |
||
59 | */ |
||
60 | public function setCache(CacheProvider $cache) |
||
61 | { |
||
62 | $this->cache = $cache; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Fetches piece of JS-code with require.js main config from cache |
||
67 | * or if it was not there - generates and put into a cache |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | public function getMainConfig() |
||
72 | { |
||
73 | $config = null; |
||
74 | if ($this->cache) { |
||
75 | $config = $this->cache->fetch(self::REQUIREJS_CONFIG_CACHE_KEY); |
||
76 | } |
||
77 | if (empty($config)) { |
||
78 | $config = $this->generateMainConfig(); |
||
79 | if ($this->cache) { |
||
80 | $this->cache->save(self::REQUIREJS_CONFIG_CACHE_KEY, $config); |
||
81 | } |
||
82 | } |
||
83 | return $config; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Generates main config for require.js |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | public function generateMainConfig() |
||
92 | { |
||
93 | $requirejs = $this->collectConfigs(); |
||
94 | $config = $requirejs['config']; |
||
95 | if (!empty($config['paths']) && is_array($config['paths'])) { |
||
96 | foreach ($config['paths'] as &$path) { |
||
97 | if (substr($path, 0, 8) === 'bundles/') { |
||
98 | $path = substr($path, 8); |
||
99 | } |
||
100 | if (substr($path, -3) === '.js') { |
||
101 | $path = substr($path, 0, -3); |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | return $this->templating->render($this->template, array('config' => $config)); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Generates build config for require.js |
||
110 | * |
||
111 | * @param string $configPath path to require.js main config |
||
112 | * @return array |
||
113 | */ |
||
114 | public function generateBuildConfig($configPath) |
||
115 | { |
||
116 | $config = $this->collectConfigs(); |
||
117 | |||
118 | $config['build']['baseUrl'] = './bundles'; |
||
119 | $config['build']['out'] = './' . $config['build_path']; |
||
120 | $config['build']['mainConfigFile'] = './' . $configPath; |
||
121 | |||
122 | $paths = array( |
||
123 | // build-in configuration |
||
124 | 'require-config' => '../' . substr($configPath, 0, -3), |
||
125 | // build-in require.js lib |
||
126 | 'require-lib' => 'ororequirejs/lib/require', |
||
127 | ); |
||
128 | |||
129 | $config['build']['paths'] = array_merge($config['build']['paths'], $paths); |
||
130 | $config['build']['include'] = array_merge( |
||
131 | array_keys($paths), |
||
132 | array_keys($config['config']['paths']) |
||
133 | ); |
||
134 | |||
135 | return $config['build']; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Goes across bundles and collects configurations |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | public function collectConfigs() |
||
144 | { |
||
145 | if (!$this->collectedConfig) { |
||
146 | $config = $this->container->getParameter('oro_require_js'); |
||
147 | $bundles = $this->container->getParameter('kernel.bundles'); |
||
148 | foreach ($bundles as $bundle) { |
||
149 | $reflection = new \ReflectionClass($bundle); |
||
150 | if (is_file($file = dirname($reflection->getFilename()) . '/Resources/config/requirejs.yml')) { |
||
151 | $requirejs = Yaml::parse(file_get_contents(realpath($file))); |
||
152 | $config = ArrayUtil::arrayMergeRecursiveDistinct($config, $requirejs); |
||
0 ignored issues
–
show
|
|||
153 | } |
||
154 | } |
||
155 | |||
156 | $this->collectedConfig = $config; |
||
157 | } |
||
158 | |||
159 | return $this->collectedConfig; |
||
160 | } |
||
161 | } |
||
162 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.