-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factory.php
201 lines (181 loc) · 6.1 KB
/
Factory.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
namespace Mods\Theme;
use Closure;
use Countable;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\View\Factory as FactoryContract;
class Factory
{
/**
* The view finder implementation.
*
* @var \Mods\Theme\ThemeResolver
*/
protected $themeResolver;
/**
* The view finder implementation.
*
* @var \Illuminate\Contracts\View\Factory
*/
protected $view;
/**
* The event dispatcher instance.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $events;
/**
* The IoC container instance.
*
* @var \Illuminate\Contracts\Container\Container
*/
protected $container;
protected static $layoutXmlLocation = [];
protected static $themeTemplatePath = [];
/**
* Create a new view factory instance.
*
* @param \Mods\Theme\ThemeResolver $themeResolver
* @param \Illuminate\Contracts\View\Factory $view
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function __construct(
ThemeResolver $themeResolver,
FactoryContract $view,
Dispatcher $events
) {
$this->themeResolver = $themeResolver;
$this->view = $view;
$this->events = $events;
}
/**
* Get the IoC container instance.
*
* @return \Illuminate\Contracts\Container\Container
*/
public function getContainer()
{
return $this->container;
}
/**
* Set the IoC container instance.
*
* @param \Illuminate\Contracts\Container\Container $container
* @return void
*/
public function setContainer(Container $container)
{
$this->container = $container;
}
/**
* Add a new asset namespace to the loader.
*
* @param string $namespace
* @param string|array $hints
* @return void
*/
public function addAssetNamespace($namespace, $hints, $area)
{
if ($this->container['config']->get('deploying', false)) {
$this->container['theme.asset.resolver']->addNamespace($namespace, $hints, $area);
}
}
/**
* Add a new namespace to the loader.
*
* @param string $namespace
* @param string|array $hints
* @return void
*/
public function addViewNamespace($namespace, $hints)
{
$areas = array_merge(['frontend'], array_values($this->container['config']->get('app.areas', [])));
$xmlLocations = [];
foreach ($areas as $area) {
$areaNamespace = $namespace.'_'.$area;
if (is_dir($appPath = $this->container->resourcePath().'/views/processed/'.$area.'/'.$namespace)) {
$this->view->addNamespace($areaNamespace, $appPath);
}
$paths = $this->themeResolver->getPaths($area)->toArray();
foreach ($paths as $path) {
if (is_dir($appPath = $path.'/templates/') && !isset(static::$themeTemplatePath[md5($appPath)])) {
$this->view->addNamespace($area, $appPath);
static::$themeTemplatePath[md5($appPath)] = $appPath;
}
if (is_dir($appPath = $path.'/templates/'.$namespace)) {
$this->view->addNamespace($areaNamespace, $appPath);
}
}
foreach ((array) $hints as $path) {
if (is_dir($appPath = $path.'/'.$area.'/templates/')) {
$this->view->addNamespace($areaNamespace, $appPath);
}
if (is_dir($appPath = $path.'/'.$area.'/layouts/')) {
static::$layoutXmlLocation[$area][md5($appPath)] = $appPath;
}
}
}
}
public function booted($app)
{
$this->registerLayoutXml($app);
$this->registerLang($app);
if ($app['config']->get('deploying', false)) {
$this->registerAsset($app);
}
static::$layoutXmlLocation = static::$themeTemplatePath = [];
}
protected function registerLayoutXml($app)
{
$areas = array_merge(['frontend'], array_values($app['config']->get('app.areas', [])));
foreach ($areas as $area) {
$paths = $this->themeResolver->getPaths($area)->toArray();
foreach ($paths as $path) {
if (is_dir($appPath = $path.'/layouts/')) {
static::$layoutXmlLocation[$area][md5($appPath)] = $appPath;
}
}
}
$oldXmlLocations = $app['config']->get('layout.xml_location', []);
$xmlLocations = array_merge_recursive($oldXmlLocations, static::$layoutXmlLocation);
$app['config']->set('layout.xml_location', $xmlLocations);
}
protected function registerLang($app)
{
$areas = array_merge(['frontend'], array_values($app['config']->get('app.areas', [])));
foreach ($areas as $area) {
$paths = $this->themeResolver->getPaths($area)->toArray();
foreach ($paths as $path) {
if (is_dir($appPath = $path.'/lang/')) {
$app['translation.loader']->addLoaderPaths([$appPath]);
}
}
}
}
protected function registerAsset($app)
{
$areas = array_merge(['frontend'], array_values($app['config']->get('app.areas', [])));
$assetPaths = [];
foreach ($areas as $area) {
$themes = $this->themeResolver->themeCollection($area);
foreach ($themes as $key => $theme) {
$paths = $this->themeResolver->getPaths($area, $key)->toArray();
foreach ($paths as $path) {
if (is_dir($appPath = $path.'/assets/')) {
$assetPaths[$area][$key][] = $appPath;
}
}
}
}
$app['theme.asset.resolver']->addLocation($assetPaths);
}
public function getActiveTheme($area)
{
return $this->themeResolver->getActive($area);
}
}