Completed
Push — master ( 7665c8...9af7b9 )
by Robbert
61:21 queued 52:46
created

ar_template_filesystem::exists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 11
loc 11
rs 9.4285
c 2
b 0
f 2
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
			// FIXME 'netjes' een path opzoeken hier voor
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "'netjes' een path opzoeken hier voor"
Loading history...
26
			$cachepath = sha1($path . $name);
27
			$cacheroot = '/home/muller/devel/site/files/temp/';
28
			if ( 
29
				! file_exists( $cacheroot . $cachepath )  ||
30
				( filemtime($cacheroot . $cachepath ) < filemtime ( $realpath  . $name ) )
31
			) {
32
				$compiled = $this->compile($path, $name);
33
				file_put_contents($cacheroot . $cachepath, $compiled);
34
			}
35
			include (  $cacheroot . $cachepath );
36
			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...
37
		}
38
39
		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...
40
			return false;
41
		}
42
43 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...
44
			$arpath = path::collapse($path);
45
46
			if ( strpos($arpath, $this->path, 0) !== 0) {
47
				return ar('error')->raiseError('invalide path for loading template',500);
48
			}
49
50
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
51
			$realpath = realpath($realpath) .'/';
52
53
			return file_get_contents($realpath . $name);
54
		}
55
56
		public function ls($path) {
57
			$arpath = path::collapse($path);
58
59
			if ( strpos($arpath, $this->path, 0) !== 0) {
60
				return [];
61
			}
62
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
63
			$realpath = realpath($realpath) .'/';
64
			$config = json_decode(file_get_contents($realpath . 'library.json'),true);
65
			if(!isset($config['exports']) ) {
66
				$config['exports'] = [];
67
			}
68
			if(!isset($config['local']) ) {
69
				$config['local'] = [];
70
			}
71
72
			$result = [];
73
74
			$traverseDir = function ($path, $type = 'pobject', $nls = 'any') use ($arpath, &$result, $config, &$traverseDir, $realpath) {
75
				$path = path::collapse($path);
76
				$index = scandir($path, SCANDIR_SORT_NONE);
77
				if($index !== false) {
78
					list($maintype, $subtype) = explode('.', $type,2);
79
					foreach($index as $filename) {
80
						if($filename[0] === "." ) {
81
							continue;
82
						}
83
						$filepath = $path . $filename;
84
						if ( is_dir($filepath) ) {
85
							if (strlen($filename) == 2) {
86
								$traverseDir(path::collapse($filename, $path), $type, $filename);
87
							} else {
88
								$traverseDir(path::collapse($filename, $path), $filename);
89
							}
90
						} else if ( is_file($filepath) ) {
91
							$tempname = sprintf("%s.%s.%s",$type,$filename,$nls);
92
							$confname = sprintf("%s::%s",$type,$filename);
93
							$private = true;
94
							if (isset( $config['exports'] ) ) {
95
								$private = ! in_array($confname, $config['exports']);
96
							}
97
							$local = false;
98
							if (isset( $config['local'] ) ) {
99
								$local = in_array($confname, $config['local']);
100
							}
101
							$result[$filename][] = [
102
								'id'       => PHP_INT_MAX,
103
								'path'     => $arpath,
104
								'type'     => $maintype,
105
								'subtype'  => $subtype,
106
								'name'     => $tempname,
107
								'filename' => substr($filepath,strlen($realpath)),
108
								'language' => $nls,
109
								'private'  => $private,
110
								'local'    => $local,
111
							];
112
						}
113
					}
114
				}
115
116
			};
117
			$traverseDir($realpath . 'src/');
118
			return $result;
119
		}
120
121
		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...
122
		}
123
124 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...
125
			$arpath = path::collapse($path);
126
127
			if ( strpos($arpath, $this->path, 0) !== 0) {
128
				return ar('error')->raiseError('invalide path for loading template',500);
129
			}
130
131
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
132
			$realpath = realpath($realpath) .'/';
133
			return file_exists($realpath . $name);
134
		}
135
136
		public function compile($path, $name) {
137
			global $AR;
138
			$arpath = path::collapse($path);
139
140
			if ( strpos($arpath, $this->path, 0) !== 0) {
141
				return ar('error')->raiseError('invalide path for loading template',500);
142
			}
143
144
			$realpath = $this->config['path'] . substr($arpath,strlen($this->path));
145
			$realpath = realpath($realpath) .'/';
146
147
			$template = file_get_contents($realpath . $name);
148
149
			require_once(AriadneBasePath."/modules/mod_pinp.phtml");
150
151
			$pinp = new pinp($AR->PINP_Functions, "local->", "\$AR_this->_");
152
153
			// FIXME error checking
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "error checking"
Loading history...
154
			$compiled = $pinp->compile(strtr($template,"\r",""));
155
			$compiled = sprintf($AR->PINPtemplate, $compiled);
156
			return $compiled;
157
158
		}
159
	}
160