1
|
|
|
<?php /** MicroAsset */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Web; |
4
|
|
|
|
5
|
|
|
use Micro\Auth\Injector; |
6
|
|
|
use Micro\Base\Autoload; |
7
|
|
|
use Micro\Base\Exception; |
8
|
|
|
use Micro\Base\KernelInjector; |
9
|
|
|
use Micro\File\FileHelper; |
10
|
|
|
use Micro\Mvc\Views\IView; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Asset class file. |
14
|
|
|
* |
15
|
|
|
* @author Oleg Lunegov <[email protected]> |
16
|
|
|
* @link https://github.com/linpax/microphp-framework |
17
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
18
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
19
|
|
|
* @package Micro |
20
|
|
|
* @subpackage Web |
21
|
|
|
* @version 1.0 |
22
|
|
|
* @since 1.0 |
23
|
|
|
*/ |
24
|
|
|
class Asset |
25
|
|
|
{ |
26
|
|
|
/** @var string $sourcePath Full-path to source asset dir */ |
27
|
|
|
public $sourcePath; |
28
|
|
|
|
29
|
|
|
/** @var bool $isHead Is a publish into head block */ |
30
|
|
|
public $isHead = true; |
31
|
|
|
/** @var array $js JavaScript files links */ |
32
|
|
|
public $js = []; |
33
|
|
|
/** @var array $css CSS files links */ |
34
|
|
|
public $css = []; |
35
|
|
|
/** @var array $required Required assets */ |
36
|
|
|
public $required = []; |
37
|
|
|
/** @var array $excludes Excludes extensions */ |
38
|
|
|
public $excludes = []; |
39
|
|
|
|
40
|
|
|
/** @var IView $view View for install current asset */ |
41
|
|
|
protected $view; |
42
|
|
|
/** @var string $hash Unique directory to publish into assets dir */ |
43
|
|
|
protected $hash; |
44
|
|
|
/** @var string $publishPath Publish path */ |
45
|
|
|
protected $publishPath; |
46
|
|
|
/** @var array $published Published required extends */ |
47
|
|
|
private $published = []; |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor asset |
52
|
|
|
* |
53
|
|
|
* @access public |
54
|
|
|
* |
55
|
|
|
* @param IView $view |
56
|
|
|
* |
57
|
|
|
* @result void |
58
|
|
|
* @throws \Micro\Base\Exception |
59
|
|
|
*/ |
60
|
|
|
public function __construct(IView $view) |
61
|
|
|
{ |
62
|
|
|
$this->view = $view; |
63
|
|
|
|
64
|
|
|
if (!$this->sourcePath) { |
65
|
|
|
$this->sourcePath = dirname(Autoload::getClassPath(get_class($this))); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->hash = md5($this->sourcePath); |
69
|
|
|
|
70
|
|
|
$this->publishPath = '/' . (($dir = (new Injector)->param('assetsDirName')) ? $dir : 'assets') . '/' . $this->hash; |
71
|
|
|
|
72
|
|
|
$web = (new KernelInjector)->build()->getWebDir(); |
73
|
|
|
|
74
|
|
|
if (!file_exists($this->sourcePath)) { |
75
|
|
|
throw new Exception('Asset dir not exists: '.$this->sourcePath); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (!is_dir($web.$this->publishPath) && (!mkdir($web.$this->publishPath, 0777) && !is_dir($web.$this->publishPath))) { |
79
|
|
|
throw new Exception('Could not access to publish dir: '.$this->publishPath); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
FileHelper::recurseCopyIfEdited($this->sourcePath, $web.$this->publishPath, $this->excludes); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Send asset into view |
87
|
|
|
* |
88
|
|
|
* @access public |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function publish() |
92
|
|
|
{ |
93
|
|
|
foreach ($this->required AS $require) { |
94
|
|
|
if (!in_array($require, $this->published, true) && class_exists($require)) { |
95
|
|
|
$this->published[] = $require; |
96
|
|
|
|
97
|
|
|
/** @var Asset $require */ |
98
|
|
|
$require = new $require($this->view); |
99
|
|
|
$require->publish(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
View Code Duplication |
if ($this->js) { |
|
|
|
|
104
|
|
|
if (is_string($this->js)) { |
105
|
|
|
$this->js = [$this->js]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (0 !== count($this->js)) { |
109
|
|
|
/** @noinspection ForeachSourceInspection */ |
110
|
|
|
foreach ($this->js AS $script) { |
111
|
|
|
$this->view->registerScriptFile($this->publishPath.$script, $this->isHead); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
if ($this->css) { |
|
|
|
|
117
|
|
|
if (is_string($this->css)) { |
118
|
|
|
$this->css = [$this->css]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (0 !== count($this->css)) { |
122
|
|
|
/** @noinspection ForeachSourceInspection */ |
123
|
|
|
foreach ($this->css AS $style) { |
124
|
|
|
$this->view->registerCssFile($this->publishPath.$style, $this->isHead); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.