|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Arrilot\BitrixBlade; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
|
6
|
|
|
use Illuminate\Contracts\View\Factory; |
|
7
|
|
|
|
|
8
|
|
|
class BladeProvider |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Path to a folder view common view can be stored. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected static $baseViewPath; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Local path to blade cache storage. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected static $cachePath; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* View factory. |
|
26
|
|
|
* |
|
27
|
|
|
* @var Factory |
|
28
|
|
|
*/ |
|
29
|
|
|
protected static $viewFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Service container factory. |
|
33
|
|
|
* |
|
34
|
|
|
* @var Container |
|
35
|
|
|
*/ |
|
36
|
|
|
protected static $container; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Register blade engine in Bitrix. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $baseViewPath |
|
42
|
|
|
* @param string $cachePath |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function register($baseViewPath = 'local/views', $cachePath = 'bitrix/cache/blade') |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
static::$baseViewPath = $_SERVER['DOCUMENT_ROOT'].'/'.$baseViewPath; |
|
47
|
|
|
static::$cachePath = $_SERVER['DOCUMENT_ROOT'].'/'.$cachePath; |
|
48
|
|
|
|
|
49
|
|
|
static::instantiateServiceContainer(); |
|
50
|
|
|
static::instantiateViewFactory(); |
|
51
|
|
|
static::registerBitrixDirectives(); |
|
52
|
|
|
|
|
53
|
|
|
global $arCustomTemplateEngines; |
|
|
|
|
|
|
54
|
|
|
$arCustomTemplateEngines['blade'] = [ |
|
55
|
|
|
'templateExt' => ['blade'], |
|
56
|
|
|
'function' => 'renderBladeTemplate', |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get view factory. |
|
62
|
|
|
* |
|
63
|
|
|
* @return Factory |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function getViewFactory() |
|
66
|
|
|
{ |
|
67
|
|
|
return static::$viewFactory; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return BladeCompiler |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getCompiler() |
|
74
|
|
|
{ |
|
75
|
|
|
return static::$container['blade.compiler']; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Update paths where blade tries to find additional views. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $templateDir |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function updateViewPaths($templateDir) |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
$newPaths = [ |
|
86
|
|
|
$_SERVER['DOCUMENT_ROOT'].$templateDir, |
|
87
|
|
|
static::$baseViewPath, |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
|
|
$finder = Container::getInstance()->make('view.finder'); |
|
91
|
|
|
$finder->setPaths($newPaths); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Instantiate service container if it's not instantiated yet. |
|
96
|
|
|
*/ |
|
97
|
|
|
protected static function instantiateServiceContainer() |
|
98
|
|
|
{ |
|
99
|
|
|
$container = Container::getInstance(); |
|
100
|
|
|
|
|
101
|
|
|
if (!$container) { |
|
102
|
|
|
$container = new Container(); |
|
103
|
|
|
Container::setInstance($container); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
static::$container = $container; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Instantiate view factory. |
|
111
|
|
|
*/ |
|
112
|
|
|
protected static function instantiateViewFactory() |
|
113
|
|
|
{ |
|
114
|
|
|
static::createDirIfNotExist(static::$baseViewPath); |
|
115
|
|
|
static::createDirIfNotExist(static::$cachePath); |
|
116
|
|
|
|
|
117
|
|
|
$viewPaths = [ |
|
118
|
|
|
static::$baseViewPath, |
|
119
|
|
|
]; |
|
120
|
|
|
$cache = static::$cachePath; |
|
121
|
|
|
|
|
122
|
|
|
$blade = new Blade($viewPaths, $cache, static::$container); |
|
123
|
|
|
|
|
124
|
|
|
static::$viewFactory = $blade->view(); |
|
125
|
|
|
static::$viewFactory->addExtension('blade', 'blade'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Create dir if it does not exist. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $path |
|
132
|
|
|
*/ |
|
133
|
|
|
protected static function createDirIfNotExist($path) |
|
134
|
|
|
{ |
|
135
|
|
|
if (!file_exists($path)) { |
|
136
|
|
|
$mask = umask(0); |
|
137
|
|
|
mkdir($path, 0777, true); |
|
138
|
|
|
umask($mask); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Register bitrix directives. |
|
144
|
|
|
*/ |
|
145
|
|
|
protected static function registerBitrixDirectives() |
|
146
|
|
|
{ |
|
147
|
|
|
$compiler = static::getCompiler(); |
|
148
|
|
|
$compiler->directive('bxComponent', function ($expression) { |
|
149
|
|
|
$expression = rtrim($expression, ')'); |
|
150
|
|
|
$expression = ltrim($expression, '('); |
|
151
|
|
|
|
|
152
|
|
|
return '<?php $APPLICATION->IncludeComponent('.$expression.'); ?>'; |
|
153
|
|
|
}); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: