1
|
|
|
<?php /** MicroAsset */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Web; |
4
|
|
|
|
5
|
|
|
use Micro\Base\Autoload; |
6
|
|
|
use Micro\Base\Exception; |
7
|
|
|
use Micro\File\FileHelper; |
8
|
|
|
use Micro\Mvc\Views\IView; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Asset class file. |
12
|
|
|
* |
13
|
|
|
* @author Oleg Lunegov <[email protected]> |
14
|
|
|
* @link https://github.com/lugnsk/micro |
15
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
16
|
|
|
* @license /LICENSE |
17
|
|
|
* @package Micro |
18
|
|
|
* @subpackage Web |
19
|
|
|
* @version 1.0 |
20
|
|
|
* @since 1.0 |
21
|
|
|
*/ |
22
|
|
|
class Asset |
23
|
|
|
{ |
24
|
|
|
/** @var string $sourcePath Full-path to source asset dir */ |
25
|
|
|
public $sourcePath; |
26
|
|
|
|
27
|
|
|
/** @var bool $isHead Is a publish into head block */ |
28
|
|
|
public $isHead = true; |
29
|
|
|
/** @var array $js JavaScript files links */ |
30
|
|
|
public $js = []; |
31
|
|
|
/** @var array $css CSS files links */ |
32
|
|
|
public $css = []; |
33
|
|
|
/** @var array $required Required assets */ |
34
|
|
|
public $required = []; |
35
|
|
|
/** @var array $excludes Excludes extensions */ |
36
|
|
|
public $excludes = []; |
37
|
|
|
|
38
|
|
|
/** @var IView $view View for install current asset */ |
39
|
|
|
protected $view; |
40
|
|
|
/** @var string $hash Unique directory to publish into assets dir */ |
41
|
|
|
protected $hash; |
42
|
|
|
/** @var string $publishPath Publish path */ |
43
|
|
|
protected $publishPath; |
44
|
|
|
/** @var array $published Published required extends */ |
45
|
|
|
private $published = []; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor asset |
50
|
|
|
* |
51
|
|
|
* @access public |
52
|
|
|
* |
53
|
|
|
* @param IView $view |
54
|
|
|
* |
55
|
|
|
* @result void |
56
|
|
|
* @throws \Micro\Base\Exception |
57
|
|
|
*/ |
58
|
|
|
public function __construct(IView $view) |
59
|
|
|
{ |
60
|
|
|
$this->view = $view; |
61
|
|
|
|
62
|
|
|
if (!$this->sourcePath) { |
63
|
|
|
$this->sourcePath = dirname(Autoload::getClassPath(get_class($this))); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->hash = md5($this->sourcePath); |
67
|
|
|
|
68
|
|
|
$this->publishPath = '/' . (($dir = $view->container->assetsDirName) ? $dir : 'assets') . '/' . $this->hash; |
|
|
|
|
69
|
|
|
|
70
|
|
|
$web = $this->view->container->kernel->getWebDir(); |
|
|
|
|
71
|
|
|
|
72
|
|
|
if (!file_exists($this->sourcePath)) { |
73
|
|
|
throw new Exception('Asset dir not exists: ' . $this->sourcePath); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (!@mkdir($web . $this->publishPath, 0777) && !is_dir($web . $this->publishPath)) { |
77
|
|
|
throw new Exception('Could not access to publish dir: ' . $this->publishPath); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
FileHelper::recurseCopyIfEdited($this->sourcePath, $web . $this->publishPath, $this->excludes); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Send asset into view |
85
|
|
|
* |
86
|
|
|
* @access public |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
public function publish() |
90
|
|
|
{ |
91
|
|
|
foreach ($this->required AS $require) { |
92
|
|
|
if (!in_array($require, $this->published, true) && class_exists($require)) { |
93
|
|
|
$this->published[] = $require; |
94
|
|
|
/** @var Asset $require */ |
95
|
|
|
$require = new $require($this->view); |
96
|
|
|
$require->publish(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
if ($this->js) { |
|
|
|
|
101
|
|
|
if (is_string($this->js)) { |
102
|
|
|
$this->js = [$this->js]; |
103
|
|
|
} |
104
|
|
|
foreach ($this->js AS $script) { |
105
|
|
|
$this->view->registerScriptFile($this->publishPath . $script, $this->isHead); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
View Code Duplication |
if ($this->css) { |
|
|
|
|
109
|
|
|
if (is_string($this->css)) { |
110
|
|
|
$this->css = [$this->css]; |
111
|
|
|
} |
112
|
|
|
foreach ($this->css AS $style) { |
113
|
|
|
$this->view->registerCssFile($this->publishPath . $style, $this->isHead); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: