-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainJsAction.php
79 lines (53 loc) · 1.86 KB
/
MainJsAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* MainJsAction.php
* Created by h8every1 on 24.05.2017 21:33
*/
namespace h8every1\requirejsview;
use yii\base\Action;
class MainJsAction extends Action
{
public $options = [
'modules'=>[],
];
public function run()
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
$headers = \Yii::$app->response->headers;
$headers->add('Content-Type', 'text/javascript');
return $this->controller->renderPartial('@vendor/h8every1/yii2-requirejs-view/view/main-js', [
'config' => $this->generateConfig()
]);
}
private function generateConfig()
{
$manager = $this->controller->getView()->getAssetManager();
$config = $paths = $shim = [];
foreach ($this->options['modules'] as $module) {
if ( ! class_exists($module) || ! is_a($module, assets\RequireJsAssetBundle::className())) {
continue;
}
$bundle = $manager->getBundle($module);
foreach ($bundle->js as $name => $data) {
if (is_array($data)) {
$fileName = array_shift($data);
if (count($data)) {
$shim[$name] = [];
foreach ($data as $section => $value) {
$shim[$name][$section] = $value;
}
}
} else {
$name = $fileName = $data;
}
if (substr($fileName, -3) === '.js') {
$fileName = substr($fileName, 0, -3);
}
$paths[$name] = $manager->getAssetUrl($bundle, $fileName);
}
}
$config['paths'] = $paths;
$config['shim'] = $shim;
return json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
}