Completed
Pull Request — master (#119)
by Robbert
08:44
created

ar_template_filesystem::compile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 23
rs 9.0856
c 1
b 0
f 1
ccs 0
cts 15
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
			$config = [];
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
			$traverse = 'src/';
67
			if (!file_exists($realpath . 'library.json')) {
68
				$realparent = path::parent($realpath);
69
				$node = basename($realpath);
70
				if ($node === 'tests' && file_exists($realparent . 'library.json') ) {
71
					// special case
72
					// system/lib/libname/tests/ is a sibling of src instead of a child library
73
					$traverse = 'tests/';
74
					$arpath   = path::parent($path);
75
					$realpath = $realparent;
76
				} else {
77
					return [];
78
				}
79
			}
80
81
			$config = json_decode(file_get_contents($realpath . 'library.json'),true);
82
83
			if(!isset($config['exports']) ) {
84
				$config['exports'] = [];
85
			}
86
			if(!isset($config['local']) ) {
87
				$config['local'] = [];
88
			}
89
90
			$result = [];
91
92
			$traverseDir = function ($path, $type = 'pobject', $nls = 'any') use ($arpath, &$result, $config, &$traverseDir, $realpath) {
93
				$path = path::collapse($path);
94
				if (is_dir($path) ) {
95
					$index = scandir($path, SCANDIR_SORT_NONE);
96
					if($index !== false) {
97
						list($maintype, $subtype) = explode('.', $type,2);
98
						foreach($index as $filename) {
99
							if($filename[0] === "." ) {
100
								continue;
101
							}
102
							$filepath = $path . $filename;
103
							if ( is_dir($filepath) ) {
104
								if (strlen($filename) == 2) {
105
									$traverseDir(path::collapse($filename, $path), $type, $filename);
106
								} else {
107
									$traverseDir(path::collapse($filename, $path), $filename);
108
								}
109
							} else if ( is_file($filepath) ) {
110
								$tempname = sprintf("%s.%s.%s",$type,$filename,$nls);
111
								$confname = sprintf("%s::%s",$type,$filename);
112
								$private = true;
113
								if (isset( $config['exports'] ) ) {
114
									$private = ! in_array($confname, $config['exports']);
115
								}
116
								$local = false;
117
								if (isset( $config['local'] ) ) {
118
									$local = in_array($confname, $config['local']);
119
								}
120
								$result[$filename][] = [
121
									'id'       => PHP_INT_MAX,
122
									'path'     => $arpath,
123
									'type'     => $maintype,
124
									'subtype'  => $subtype,
125
									'name'     => $tempname,
126
									'filename' => substr($filepath,strlen($realpath)),
127
									'language' => $nls,
128
									'private'  => $private,
129
									'local'    => $local,
130
								];
131
							}
132
						}
133
					}
134
				}
135
136
			};
137
			$traverseDir($realpath . $traverse );
138
			return $result;
139
		}
140
141
		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...
142
		}
143
144 View Code Duplication
		public function exists($path, $name) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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