-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathVoltProvider.php
More file actions
139 lines (116 loc) · 4.47 KB
/
Copy pathVoltProvider.php
File metadata and controls
139 lines (116 loc) · 4.47 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of the Phalcon Developer Tools.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Phalcon\DevTools\Providers;
use Phalcon\Config\Config;
use Phalcon\DevTools\Mvc\View\Engine\Volt\Extension\Php as PhpExt;
use Phalcon\Di\DiInterface;
use Phalcon\Di\ServiceProviderInterface;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
class VoltProvider implements ServiceProviderInterface
{
/**
* @var DiInterface
*/
protected $di;
/**
* @var string
*/
protected $providerName = 'volt';
/**
* Registers a service provider.
*
* @param DiInterface $di
*/
public function register(DiInterface $di): void
{
$this->di = $di;
$basePath = $di->getShared('application')->getBasePath();
$ptoolsPath = $di->getShared('application')->getPtoolsPath();
$that = $this;
$di->setShared($this->providerName, function ($view, $di) use ($basePath, $ptoolsPath, $that) {
/** @var DiInterface $this */
$volt = new VoltEngine($view, $di);
$config = $this->getShared('config');
$appCacheDir = $config->get('application', new Config)->get('cacheDir');
$defaultCacheDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phalcon' . DIRECTORY_SEPARATOR . 'volt';
/** @var Config $voltConfig */
$voltConfig = null;
if ($config->offsetExists('volt')) {
$voltConfig = $config->get('volt');
} elseif ($config->offsetExists('view')) {
$voltConfig = $config->get('view');
}
if (!$voltConfig instanceof Config) {
$voltConfig = new Config([
'compiledExt' => '.php',
'separator' => '_',
'cacheDir' => $appCacheDir ?: $defaultCacheDir,
'forceCompile' => ENV_DEVELOPMENT === APPLICATION_ENV,
]);
}
$compiledPath = function ($templatePath) use (
$voltConfig,
$basePath,
$ptoolsPath,
$that
) {
/**
* @var DiInterface $this
* @var Config $voltConfig
*/
if (0 === strpos($templatePath, $basePath)) {
$templatePath = substr($templatePath, strlen($basePath));
} elseif (0 === strpos($templatePath, $ptoolsPath . DIRECTORY_SEPARATOR . 'src')) {
$templatePath = substr($templatePath, strlen($ptoolsPath . DIRECTORY_SEPARATOR . 'src'));
}
$templatePath = trim($templatePath, '\\/');
$filename = str_replace(['\\', '/'], $voltConfig->get('separator', '_'), $templatePath);
$filename = basename($filename, '.volt') . $voltConfig->get('compiledExt', '.php');
$cacheDir = $that->getCacheDir($voltConfig);
return rtrim($cacheDir, '\\/') . DIRECTORY_SEPARATOR . $filename;
};
$options = [
'path' => $voltConfig->get('compiledPath', $compiledPath),
'always' => ENV_DEVELOPMENT === APPLICATION_ENV || boolval($voltConfig->get('forceCompile')),
];
$volt->setOptions($options);
$volt->getCompiler()->addExtension(new PhpExt);
return $volt;
});
}
/**
* get volt cache directory
*
* @param Config $voltConfig
*
* @return string
*/
protected function getCacheDir(Config $voltConfig): string
{
$appCacheDir = $this->di->getShared('config')->path('application.cacheDir');
$cacheDir = $voltConfig->get('cacheDir', $appCacheDir);
$defaultCacheDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phalcon' . DIRECTORY_SEPARATOR . 'volt';
if ($cacheDir && is_dir($cacheDir) && is_writable($cacheDir)) {
return $cacheDir;
}
$this->di->getShared('logger')->warning(
'Unable to initialize Volt cache dir: {cache}. Used temp path: {default}',
[
'cache' => $cacheDir,
'default' => $defaultCacheDir
]
);
if (!is_dir($defaultCacheDir)) {
mkdir($defaultCacheDir, 0777, true);
}
return $defaultCacheDir;
}
}