ar_template_filesystem::exists()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
ccs 0
cts 9
cp 0
crap 6
1
<?php
2
3
	use arc\path as path;
4
5
	class ar_template_filesystem extends arBase {
6
		private $path;
7
		private $config;
8
9
		public function __construct($path, $config ) {
10
			$this->path   = path::collapse($path);
11
			$this->config = $config;
12
			$this->config['path'] = path::collapse($config['path']);
13
		}
14
15
		public function get($path, $name){
16
			$arpath = path::collapse($path);
17
18
			if ( strpos($arpath, $this->path, 0) !== 0) {
19
				return ar('error')->raiseError('invalide path for loading template',500);
20
			}
21
22
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
23
			$realpath = realpath($realpath) .'/';
24
25
			$cachepath = sha1($path . $name);
26
			$tempOb    = ar::context()->getObject();
27
			$cacheroot = $tempOb->store->get_config('files').'temp/';
28
29
			if ( 
30
				! file_exists( $cacheroot . $cachepath )  ||
31
				( filemtime($cacheroot . $cachepath ) < filemtime ( $realpath  . $name ) )
32
			) {
33
				$compiled = $this->compile($path, $name);
34
				file_put_contents($cacheroot . $cachepath, $compiled);
35
			}
36
			include (  $cacheroot . $cachepath );
37
			return $arTemplateFunction;
0 ignored issues
show
Bug introduced by
The variable $arTemplateFunction does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
38
		}
39
40
		public function save($path, $name, $template, $local=null, $private=null) {
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $template is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $local is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $private is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
			return false;
42
		}
43
44 View Code Duplication
		public function load($path, $name) {
45
			$arpath = path::collapse($path);
46
47
			if ( strpos($arpath, $this->path, 0) !== 0) {
48
				return ar('error')->raiseError('invalide path for loading template',500);
49
			}
50
51
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
52
			$realpath = realpath($realpath) .'/';
53
54
			return file_get_contents($realpath . $name);
55
		}
56
57
		public function ls($path) {
58
			$arpath = path::collapse($path);
59
60
			if ( strpos($arpath, $this->path, 0) !== 0) {
61
				return [];
62
			}
63
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
64
			$realpath = realpath($realpath) .'/';
65
			$traverse = 'src/';
66
			if (!file_exists($realpath . 'library.json')) {
67
				$realparent = path::parent($realpath);
68
				$node = basename($realpath);
69
				if ($node === 'tests' && file_exists($realparent . 'library.json') ) {
70
					// special case
71
					// system/lib/libname/tests/ is a sibling of src instead of a child library
72
					$traverse = 'tests/';
73
					$arpath   = path::parent($path);
74
					$realpath = $realparent;
75
				} else {
76
					return [];
77
				}
78
			}
79
80
			$config = json_decode(file_get_contents($realpath . 'library.json'),true);
81
82
			if(!isset($config['exports']) ) {
83
				$config['exports'] = [];
84
			}
85
			if(!isset($config['local']) ) {
86
				$config['local'] = [];
87
			}
88
89
			$result = [];
90
91
			$traverseDir = function ($path, $type = 'pobject', $nls = 'any') use ($arpath, &$result, $config, &$traverseDir, $realpath) {
92
				$path = path::collapse($path);
93
				if (is_dir($path) ) {
94
					$index = scandir($path, SCANDIR_SORT_NONE);
95
					if($index !== false) {
96
						list($maintype, $subtype) = explode('.', $type,2);
97
						foreach($index as $filename) {
98
							if($filename[0] === "." ) {
99
								continue;
100
							}
101
							$filepath = $path . $filename;
102
							if ( is_dir($filepath) ) {
103
								if (strlen($filename) == 2) {
104
									$traverseDir(path::collapse($filename, $path), $type, $filename);
105
								} else {
106
									$traverseDir(path::collapse($filename, $path), $filename);
107
								}
108
							} else if ( is_file($filepath) ) {
109
								$tempname = sprintf("%s.%s.%s",$type,$filename,$nls);
110
								$confname = sprintf("%s::%s",$type,$filename);
111
								$private = true;
112
								if (isset( $config['exports'] ) ) {
113
									if ($config['exports'][0] === '*') {
114
										$private = false;
115
									} else {
116
										$private = ! (
117
											in_array($filename, $config['exports']) ||
118
											in_array($confname, $config['exports'])
119
										);
120
									}
121
								}
122
								$local = false;
123
								if (isset( $config['local'] ) ) {
124
									$local = in_array($confname, $config['local']);
125
								}
126
								$result[$filename][] = [
127
									'id'       => PHP_INT_MAX,
128
									'path'     => $arpath,
129
									'type'     => $maintype,
130
									'subtype'  => $subtype,
131
									'name'     => $tempname,
132
									'filename' => substr($filepath,strlen($realpath)),
133
									'language' => $nls,
134
									'private'  => $private,
135
									'local'    => $local,
136
								];
137
							}
138
						}
139
					}
140
				}
141
142
			};
143
			$traverseDir($realpath . $traverse );
144
			return $result;
145
		}
146
147
		public function rm($path, $name){
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
		}
149
150 View Code Duplication
		public function exists($path, $name) {
151
			$arpath = path::collapse($path);
152
153
			if ( strpos($arpath, $this->path, 0) !== 0) {
154
				return ar('error')->raiseError('invalide path for loading template',500);
155
			}
156
157
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
158
			$realpath = realpath($realpath) .'/';
159
			return file_exists($realpath . $name);
160
		}
161
162
		public function compile($path, $name) {
163
			global $AR;
164
			$arpath = path::collapse($path);
165
166
			if ( strpos($arpath, $this->path, 0) !== 0) {
167
				return ar('error')->raiseError('invalide path for loading template',500);
168
			}
169
170
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
171
			$realpath = realpath($realpath) .'/';
172
173
			$template = file_get_contents($realpath . $name);
174
175
			require_once(AriadneBasePath."/modules/mod_pinp.phtml");
176
177
			$pinp = new pinp($AR->PINP_Functions, "local->", "\$AR_this->_");
178
179
			// FIXME error checking
180
			$compiled = $pinp->compile(strtr($template,"\r",""));
181
			$compiled = sprintf($AR->PINPtemplate, $compiled);
182
			return $compiled;
183
184
		}
185
	}
186