1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 21.02.16 at 14:14 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonphp\view; |
7
|
|
|
|
8
|
|
|
use samsonframework\core\ResourcesInterface; |
9
|
|
|
use samsonframework\core\SystemInterface; |
10
|
|
|
use samsonframework\view\Generator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* SamsonPHP view module |
14
|
|
|
* @package samsonphp\view |
15
|
|
|
*/ |
16
|
|
|
class Module extends \samson\core\ExternalModule implements \samsonframework\core\CompressInterface |
17
|
|
|
{ |
18
|
|
|
/** @var Generator */ |
19
|
|
|
protected $generator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This method should be used to override generic compression logic. |
23
|
|
|
* |
24
|
|
|
* @param mixed $obj Pointer to compressor instance |
25
|
|
|
* @param array|null $code Collection of already compressed code |
26
|
|
|
* @return bool False if generic compression needs to be avoided |
27
|
|
|
*/ |
28
|
|
|
public function beforeCompress(&$obj = null, array &$code = null) |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* This method is called after generic compression logic has finished. |
35
|
|
|
* |
36
|
|
|
* @param mixed $obj Pointer to compressor instance |
37
|
|
|
* @param array|null $code Collection of already compressed code |
38
|
|
|
* @return bool False if generic compression needs to be avoided |
39
|
|
|
*/ |
40
|
|
|
public function afterCompress(&$obj = null, array &$code = null) |
41
|
|
|
{ |
42
|
|
|
// Iterate through generated php code |
43
|
|
|
foreach ($this->generator->metadata as $metadata) { |
|
|
|
|
44
|
|
|
// Compress generated php code |
45
|
|
|
$obj->compress_php($metadata->path, $this, $code, $metadata->namespace); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Module constructor. |
51
|
|
|
* |
52
|
|
|
* @param string $path |
53
|
|
|
* @param ResourcesInterface $resources |
54
|
|
|
* @param SystemInterface $system |
55
|
|
|
* @param Generator $generator |
56
|
|
|
*/ |
57
|
|
|
public function __construct($path, ResourcesInterface $resources, SystemInterface $system, Generator $generator = null) |
58
|
|
|
{ |
59
|
|
|
parent::__construct($path, $resources, $system); |
60
|
|
|
|
61
|
|
|
$this->generator = isset($generator) |
62
|
|
|
? $generator |
63
|
|
|
: new Generator( |
64
|
|
|
new \samsonphp\generator\Generator(), |
65
|
|
|
'view', |
66
|
|
|
array('\www', '\view'), |
67
|
|
|
View::class |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
// Register View class file autoloader |
71
|
|
|
spl_autoload_register(array($this, 'autoload')); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Help autoloading view classes as we know where we store them. |
76
|
|
|
* |
77
|
|
|
* @param string $class View class name for searching |
78
|
|
|
*/ |
79
|
|
|
public function autoload($class) |
80
|
|
|
{ |
81
|
|
|
$classPath = $this->cache_path.str_replace('\\', '/', $class).'.php'; |
82
|
|
|
if (file_exists($classPath)) { |
83
|
|
|
require_once($classPath); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Module preparation stage. |
89
|
|
|
* This function called after module instance creation but before |
90
|
|
|
* initialization stage. |
91
|
|
|
* |
92
|
|
|
* @param array $params Preparation stage parameters |
93
|
|
|
* |
94
|
|
|
* @return bool|void Preparation stage result |
95
|
|
|
*/ |
96
|
|
|
public function prepare(array $params = array()) |
97
|
|
|
{ |
98
|
|
|
$this->generator->scan(__SAMSON_CWD__.'/src'); |
99
|
|
|
//$this->generator->scan(__SAMSON_CWD__.'/app'); |
100
|
|
|
$signature = $this->generator->hash(); |
101
|
|
|
if ($this->cache_refresh($signature)) { |
102
|
|
|
$this->generator->generate($this->cache_path); |
103
|
|
|
// Store cache file |
104
|
|
|
file_put_contents($signature, ''); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Add system static variable to all classes |
108
|
|
|
require_once 'View.php'; |
109
|
|
|
View::$system = &$this->system; |
110
|
|
|
|
111
|
|
|
// Continue parent logic |
112
|
|
|
return parent::prepare($params); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.