@@ -32,7 +32,7 @@ |
||
32 | 32 | $w[] = '--'.$value; |
33 | 33 | } else { |
34 | 34 | if(is_bool($value)){ |
35 | - if($value) $w[] = '--'.$key; |
|
35 | + if($value) $w[] = '--'.$key; |
|
36 | 36 | } else { |
37 | 37 | $w[] = '--'.$key.'='.escapeshellarg($value); |
38 | 38 | } |
@@ -22,17 +22,17 @@ discard block |
||
22 | 22 | * @param array $params |
23 | 23 | * @return string |
24 | 24 | */ |
25 | - protected static function _compileCommand($command,array $params){ |
|
25 | + protected static function _compileCommand($command, array $params) { |
|
26 | 26 | $s = $w = []; |
27 | 27 | foreach ($params as $p) { |
28 | 28 | if ($p instanceof static) { |
29 | 29 | $s[] = '$('.$p->getShellCommand().')'; |
30 | 30 | } else if (is_array($p)) foreach ($p as $key => $value) { |
31 | - if(is_numeric($key)){ |
|
31 | + if (is_numeric($key)) { |
|
32 | 32 | $w[] = '--'.$value; |
33 | 33 | } else { |
34 | - if(is_bool($value)){ |
|
35 | - if($value) $w[] = '--'.$key; |
|
34 | + if (is_bool($value)) { |
|
35 | + if ($value) $w[] = '--'.$key; |
|
36 | 36 | } else { |
37 | 37 | $w[] = '--'.$key.'='.escapeshellarg($value); |
38 | 38 | } |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | } |
43 | 43 | } |
44 | 44 | return trim( |
45 | - '/usr/bin/env '.$command.' '.implode(' ',array_merge($w,$s)) |
|
45 | + '/usr/bin/env '.$command.' '.implode(' ', array_merge($w, $s)) |
|
46 | 46 | ); |
47 | 47 | } |
48 | 48 | |
@@ -50,68 +50,68 @@ discard block |
||
50 | 50 | * Returns the compiled shell command |
51 | 51 | * @return string |
52 | 52 | */ |
53 | - public function getShellCommand(){ |
|
53 | + public function getShellCommand() { |
|
54 | 54 | return $this->command; |
55 | 55 | } |
56 | 56 | |
57 | - public static function __callStatic($command,$params){ |
|
57 | + public static function __callStatic($command, $params) { |
|
58 | 58 | // Check if is an alias |
59 | - if (isset(static::$aliases[$command])){ |
|
60 | - if(!$results = call_user_func_array(static::$aliases[$command],$params)) |
|
59 | + if (isset(static::$aliases[$command])) { |
|
60 | + if (!$results = call_user_func_array(static::$aliases[$command], $params)) |
|
61 | 61 | throw new Exception('Shell aliases must return a Shell class or a command string.'); |
62 | - return $results instanceof static? $results : new static($results); |
|
62 | + return $results instanceof static ? $results : new static($results); |
|
63 | 63 | } else { |
64 | - return new static($command,$params); |
|
64 | + return new static($command, $params); |
|
65 | 65 | } |
66 | 66 | } |
67 | 67 | |
68 | - public function __construct($command,$params=null){ |
|
69 | - $this->command = $params?static::_compileCommand($command,$params):$command; |
|
68 | + public function __construct($command, $params = null) { |
|
69 | + $this->command = $params ? static::_compileCommand($command, $params) : $command; |
|
70 | 70 | } |
71 | 71 | |
72 | - public function __toString(){ |
|
72 | + public function __toString() { |
|
73 | 73 | $output = []; |
74 | - exec($this->command,$output,$error_code); |
|
75 | - return empty($output)?'':implode(PHP_EOL,$output); |
|
74 | + exec($this->command, $output, $error_code); |
|
75 | + return empty($output) ? '' : implode(PHP_EOL, $output); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | /** |
79 | 79 | * Concatenate multiple shell commands via piping |
80 | 80 | * @return Shell The piped shell command |
81 | 81 | */ |
82 | - public static function pipe(/* ... */){ |
|
82 | + public static function pipe(/* ... */) { |
|
83 | 83 | $cmd = []; |
84 | 84 | foreach (func_get_args() as $item) { |
85 | - $cmd[] = ($item instanceof static)?$item->getShellCommand():$item; |
|
85 | + $cmd[] = ($item instanceof static) ? $item->getShellCommand() : $item; |
|
86 | 86 | } |
87 | - return new static(implode(' | ',$cmd)); |
|
87 | + return new static(implode(' | ', $cmd)); |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | /** |
91 | 91 | * Concatenate multiple shell commands via logical implication ( && ) |
92 | 92 | * @return Shell The concatenated shell command |
93 | 93 | */ |
94 | - public static function sequence(/* ... */){ |
|
94 | + public static function sequence(/* ... */) { |
|
95 | 95 | $cmd = []; |
96 | 96 | foreach (func_get_args() as $item) { |
97 | - $cmd[] = ($item instanceof static)?$item->getShellCommand():$item; |
|
97 | + $cmd[] = ($item instanceof static) ? $item->getShellCommand() : $item; |
|
98 | 98 | } |
99 | - return new static(implode(' && ',$cmd)); |
|
99 | + return new static(implode(' && ', $cmd)); |
|
100 | 100 | } |
101 | 101 | |
102 | - public static function execCommand($command,$params){ |
|
103 | - return new static($command,$params); |
|
102 | + public static function execCommand($command, $params) { |
|
103 | + return new static($command, $params); |
|
104 | 104 | } |
105 | 105 | |
106 | - public static function alias($command,callable $callback){ |
|
106 | + public static function alias($command, callable $callback) { |
|
107 | 107 | static::$aliases[$command] = $callback; |
108 | 108 | } |
109 | 109 | |
110 | - public static function escape($arg){ |
|
110 | + public static function escape($arg) { |
|
111 | 111 | return escapeshellarg($arg); |
112 | 112 | } |
113 | 113 | |
114 | - public function run(){ |
|
114 | + public function run() { |
|
115 | 115 | return $this->__toString(); |
116 | 116 | } |
117 | 117 |
@@ -22,18 +22,22 @@ discard block |
||
22 | 22 | * @param array $params |
23 | 23 | * @return string |
24 | 24 | */ |
25 | - protected static function _compileCommand($command,array $params){ |
|
25 | + protected static function _compileCommand($command,array $params) { |
|
26 | 26 | $s = $w = []; |
27 | 27 | foreach ($params as $p) { |
28 | 28 | if ($p instanceof static) { |
29 | 29 | $s[] = '$('.$p->getShellCommand().')'; |
30 | - } else if (is_array($p)) foreach ($p as $key => $value) { |
|
30 | + } else if (is_array($p)) { |
|
31 | + foreach ($p as $key => $value) { |
|
31 | 32 | if(is_numeric($key)){ |
32 | - $w[] = '--'.$value; |
|
33 | + $w[] = '--'.$value; |
|
34 | + } |
|
33 | 35 | } else { |
34 | - if(is_bool($value)){ |
|
35 | - if($value) $w[] = '--'.$key; |
|
36 | - } else { |
|
36 | + if(is_bool($value)) { |
|
37 | + if($value) { |
|
38 | + $w[] = '--'.$key; |
|
39 | + } |
|
40 | + } else { |
|
37 | 41 | $w[] = '--'.$key.'='.escapeshellarg($value); |
38 | 42 | } |
39 | 43 | } |
@@ -50,26 +54,27 @@ discard block |
||
50 | 54 | * Returns the compiled shell command |
51 | 55 | * @return string |
52 | 56 | */ |
53 | - public function getShellCommand(){ |
|
57 | + public function getShellCommand() { |
|
54 | 58 | return $this->command; |
55 | 59 | } |
56 | 60 | |
57 | - public static function __callStatic($command,$params){ |
|
61 | + public static function __callStatic($command,$params) { |
|
58 | 62 | // Check if is an alias |
59 | - if (isset(static::$aliases[$command])){ |
|
60 | - if(!$results = call_user_func_array(static::$aliases[$command],$params)) |
|
61 | - throw new Exception('Shell aliases must return a Shell class or a command string.'); |
|
63 | + if (isset(static::$aliases[$command])) { |
|
64 | + if(!$results = call_user_func_array(static::$aliases[$command],$params)) { |
|
65 | + throw new Exception('Shell aliases must return a Shell class or a command string.'); |
|
66 | + } |
|
62 | 67 | return $results instanceof static? $results : new static($results); |
63 | 68 | } else { |
64 | 69 | return new static($command,$params); |
65 | 70 | } |
66 | 71 | } |
67 | 72 | |
68 | - public function __construct($command,$params=null){ |
|
73 | + public function __construct($command,$params=null) { |
|
69 | 74 | $this->command = $params?static::_compileCommand($command,$params):$command; |
70 | 75 | } |
71 | 76 | |
72 | - public function __toString(){ |
|
77 | + public function __toString() { |
|
73 | 78 | $output = []; |
74 | 79 | exec($this->command,$output,$error_code); |
75 | 80 | return empty($output)?'':implode(PHP_EOL,$output); |
@@ -79,7 +84,7 @@ discard block |
||
79 | 84 | * Concatenate multiple shell commands via piping |
80 | 85 | * @return Shell The piped shell command |
81 | 86 | */ |
82 | - public static function pipe(/* ... */){ |
|
87 | + public static function pipe(/* ... */) { |
|
83 | 88 | $cmd = []; |
84 | 89 | foreach (func_get_args() as $item) { |
85 | 90 | $cmd[] = ($item instanceof static)?$item->getShellCommand():$item; |
@@ -91,7 +96,7 @@ discard block |
||
91 | 96 | * Concatenate multiple shell commands via logical implication ( && ) |
92 | 97 | * @return Shell The concatenated shell command |
93 | 98 | */ |
94 | - public static function sequence(/* ... */){ |
|
99 | + public static function sequence(/* ... */) { |
|
95 | 100 | $cmd = []; |
96 | 101 | foreach (func_get_args() as $item) { |
97 | 102 | $cmd[] = ($item instanceof static)?$item->getShellCommand():$item; |
@@ -99,19 +104,19 @@ discard block |
||
99 | 104 | return new static(implode(' && ',$cmd)); |
100 | 105 | } |
101 | 106 | |
102 | - public static function execCommand($command,$params){ |
|
107 | + public static function execCommand($command,$params) { |
|
103 | 108 | return new static($command,$params); |
104 | 109 | } |
105 | 110 | |
106 | - public static function alias($command,callable $callback){ |
|
111 | + public static function alias($command,callable $callback) { |
|
107 | 112 | static::$aliases[$command] = $callback; |
108 | 113 | } |
109 | 114 | |
110 | - public static function escape($arg){ |
|
115 | + public static function escape($arg) { |
|
111 | 116 | return escapeshellarg($arg); |
112 | 117 | } |
113 | 118 | |
114 | - public function run(){ |
|
119 | + public function run() { |
|
115 | 120 | return $this->__toString(); |
116 | 121 | } |
117 | 122 |
@@ -28,6 +28,9 @@ |
||
28 | 28 | return $this->exists($path) ? file_get_contents($this->realPath($path)) : false; |
29 | 29 | } |
30 | 30 | |
31 | + /** |
|
32 | + * @param string|false $data |
|
33 | + */ |
|
31 | 34 | public function write($path, $data){ |
32 | 35 | $r_path = $this->realPath($path); |
33 | 36 | if ( ! is_dir($r_dir = dirname($r_path)) ) @mkdir($r_dir,0775,true); |
@@ -17,42 +17,42 @@ discard block |
||
17 | 17 | protected $root; |
18 | 18 | |
19 | 19 | public function __construct(array $options = []) { |
20 | - $this->root = empty($options['root'])?'/':(rtrim($options['root'],'/').'/'); |
|
20 | + $this->root = empty($options['root']) ? '/' : (rtrim($options['root'], '/').'/'); |
|
21 | 21 | } |
22 | 22 | |
23 | - public function exists($path){ |
|
23 | + public function exists($path) { |
|
24 | 24 | return file_exists($this->realPath($path)); |
25 | 25 | } |
26 | 26 | |
27 | - public function read($path){ |
|
27 | + public function read($path) { |
|
28 | 28 | return $this->exists($path) ? file_get_contents($this->realPath($path)) : false; |
29 | 29 | } |
30 | 30 | |
31 | - public function write($path, $data){ |
|
31 | + public function write($path, $data) { |
|
32 | 32 | $r_path = $this->realPath($path); |
33 | - if ( ! is_dir($r_dir = dirname($r_path)) ) @mkdir($r_dir,0775,true); |
|
33 | + if (!is_dir($r_dir = dirname($r_path))) @mkdir($r_dir, 0775, true); |
|
34 | 34 | return file_put_contents($r_path, $data); |
35 | 35 | } |
36 | 36 | |
37 | - public function append($path, $data){ |
|
37 | + public function append($path, $data) { |
|
38 | 38 | return file_put_contents($this->realPath($path), $data, FILE_APPEND); |
39 | 39 | } |
40 | 40 | |
41 | - public function move($old, $new){ |
|
41 | + public function move($old, $new) { |
|
42 | 42 | // Atomic |
43 | - if($this->exists($old)){ |
|
44 | - return $this->write($new,$this->read($old)) && $this->delete($old); |
|
43 | + if ($this->exists($old)) { |
|
44 | + return $this->write($new, $this->read($old)) && $this->delete($old); |
|
45 | 45 | } else return false; |
46 | 46 | } |
47 | 47 | |
48 | - public function delete($path){ |
|
48 | + public function delete($path) { |
|
49 | 49 | return $this->exists($path) ? unlink($this->realPath($path)) : false; |
50 | 50 | } |
51 | 51 | |
52 | - public function search($pattern, $recursive=true){ |
|
52 | + public function search($pattern, $recursive = true) { |
|
53 | 53 | $results = []; |
54 | 54 | $root_len = strlen($this->root); |
55 | - $rx_pattern = '('.strtr($pattern,['.'=>'\.','*'=>'.*','?'=>'.']).')Ai'; |
|
55 | + $rx_pattern = '('.strtr($pattern, ['.'=>'\.', '*'=>'.*', '?'=>'.']).')Ai'; |
|
56 | 56 | |
57 | 57 | /* |
58 | 58 | $files = new \RegexIterator(new \RecursiveDirectoryIterator($this->root, |
@@ -68,12 +68,12 @@ discard block |
||
68 | 68 | new \RecursiveDirectoryIterator( |
69 | 69 | $this->root, |
70 | 70 | \RecursiveDirectoryIterator::SKIP_DOTS)), |
71 | - $rx_pattern,\RegexIterator::GET_MATCH); |
|
71 | + $rx_pattern, \RegexIterator::GET_MATCH); |
|
72 | 72 | |
73 | 73 | $fileList = []; |
74 | - foreach($tree as $group) { |
|
75 | - foreach($group as $path) { |
|
76 | - $results[] = trim(substr($path, $root_len),'/'); |
|
74 | + foreach ($tree as $group) { |
|
75 | + foreach ($group as $path) { |
|
76 | + $results[] = trim(substr($path, $root_len), '/'); |
|
77 | 77 | } |
78 | 78 | } |
79 | 79 | |
@@ -81,8 +81,8 @@ discard block |
||
81 | 81 | |
82 | 82 | } |
83 | 83 | |
84 | - protected function realPath($path){ |
|
85 | - return $this->root . $path; |
|
84 | + protected function realPath($path) { |
|
85 | + return $this->root.$path; |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | } |
@@ -20,36 +20,40 @@ discard block |
||
20 | 20 | $this->root = empty($options['root'])?'/':(rtrim($options['root'],'/').'/'); |
21 | 21 | } |
22 | 22 | |
23 | - public function exists($path){ |
|
23 | + public function exists($path) { |
|
24 | 24 | return file_exists($this->realPath($path)); |
25 | 25 | } |
26 | 26 | |
27 | - public function read($path){ |
|
27 | + public function read($path) { |
|
28 | 28 | return $this->exists($path) ? file_get_contents($this->realPath($path)) : false; |
29 | 29 | } |
30 | 30 | |
31 | - public function write($path, $data){ |
|
31 | + public function write($path, $data) { |
|
32 | 32 | $r_path = $this->realPath($path); |
33 | - if ( ! is_dir($r_dir = dirname($r_path)) ) @mkdir($r_dir,0775,true); |
|
33 | + if ( ! is_dir($r_dir = dirname($r_path)) ) { |
|
34 | + @mkdir($r_dir,0775,true); |
|
35 | + } |
|
34 | 36 | return file_put_contents($r_path, $data); |
35 | 37 | } |
36 | 38 | |
37 | - public function append($path, $data){ |
|
39 | + public function append($path, $data) { |
|
38 | 40 | return file_put_contents($this->realPath($path), $data, FILE_APPEND); |
39 | 41 | } |
40 | 42 | |
41 | - public function move($old, $new){ |
|
43 | + public function move($old, $new) { |
|
42 | 44 | // Atomic |
43 | - if($this->exists($old)){ |
|
45 | + if($this->exists($old)) { |
|
44 | 46 | return $this->write($new,$this->read($old)) && $this->delete($old); |
45 | - } else return false; |
|
47 | + } else { |
|
48 | + return false; |
|
49 | + } |
|
46 | 50 | } |
47 | 51 | |
48 | - public function delete($path){ |
|
52 | + public function delete($path) { |
|
49 | 53 | return $this->exists($path) ? unlink($this->realPath($path)) : false; |
50 | 54 | } |
51 | 55 | |
52 | - public function search($pattern, $recursive=true){ |
|
56 | + public function search($pattern, $recursive=true) { |
|
53 | 57 | $results = []; |
54 | 58 | $root_len = strlen($this->root); |
55 | 59 | $rx_pattern = '('.strtr($pattern,['.'=>'\.','*'=>'.*','?'=>'.']).')Ai'; |
@@ -81,7 +85,7 @@ discard block |
||
81 | 85 | |
82 | 86 | } |
83 | 87 | |
84 | - protected function realPath($path){ |
|
88 | + protected function realPath($path) { |
|
85 | 89 | return $this->root . $path; |
86 | 90 | } |
87 | 91 |
@@ -56,6 +56,9 @@ |
||
56 | 56 | class PHPContext { |
57 | 57 | protected $data = []; |
58 | 58 | |
59 | + /** |
|
60 | + * @param string $path |
|
61 | + */ |
|
59 | 62 | public function __construct($data=[], $path=null){ |
60 | 63 | $this->data = $data; |
61 | 64 | } |
@@ -17,7 +17,7 @@ |
||
17 | 17 | const EXTENSION = 'php'; |
18 | 18 | |
19 | 19 | protected static $templatePath, |
20 | - $globals = []; |
|
20 | + $globals = []; |
|
21 | 21 | |
22 | 22 | public function __construct($path=null,$options=[]){ |
23 | 23 | self::$templatePath = ($path ? rtrim($path,'/') : __DIR__) . '/'; |
@@ -19,27 +19,27 @@ discard block |
||
19 | 19 | protected static $templatePath, |
20 | 20 | $globals = []; |
21 | 21 | |
22 | - public function __construct($path=null,$options=[]){ |
|
22 | + public function __construct($path=null,$options=[]) { |
|
23 | 23 | self::$templatePath = ($path ? rtrim($path,'/') : __DIR__) . '/'; |
24 | 24 | } |
25 | 25 | |
26 | - public static function exists($path){ |
|
26 | + public static function exists($path) { |
|
27 | 27 | return is_file(self::$templatePath.$path.'.php'); |
28 | 28 | } |
29 | 29 | |
30 | - public static function addGlobal($key,$val){ |
|
30 | + public static function addGlobal($key,$val) { |
|
31 | 31 | self::$globals[$key] = $val; |
32 | 32 | } |
33 | 33 | |
34 | - public static function addGlobals(array $defs){ |
|
34 | + public static function addGlobals(array $defs) { |
|
35 | 35 | foreach ((array)$defs as $key=>$val) { |
36 | 36 | self::$globals[$key] = $val; |
37 | 37 | } |
38 | 38 | } |
39 | 39 | |
40 | - public function render($template, $data=[]){ |
|
40 | + public function render($template, $data=[]) { |
|
41 | 41 | $template_path = self::$templatePath . trim($template,'/') . '.php'; |
42 | - $sandbox = function() use ($template_path){ |
|
42 | + $sandbox = function() use ($template_path) { |
|
43 | 43 | ob_start(); |
44 | 44 | include($template_path); |
45 | 45 | $__buffer__ = ob_get_contents(); |
@@ -56,11 +56,11 @@ discard block |
||
56 | 56 | class PHPContext { |
57 | 57 | protected $data = []; |
58 | 58 | |
59 | - public function __construct($data=[], $path=null){ |
|
59 | + public function __construct($data=[], $path=null) { |
|
60 | 60 | $this->data = $data; |
61 | 61 | } |
62 | 62 | |
63 | - public function partial($template, $vars=[]){ |
|
63 | + public function partial($template, $vars=[]) { |
|
64 | 64 | return \View::from($template,array_merge($this->data,$vars)); |
65 | 65 | } |
66 | 66 | |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | |
69 | 69 | public function __unset($n){} |
70 | 70 | |
71 | - public function __get($n){ |
|
71 | + public function __get($n) { |
|
72 | 72 | return empty($this->data[$n]) ? '' : $this->data[$n]; |
73 | 73 | } |
74 | 74 | } |
@@ -19,26 +19,26 @@ discard block |
||
19 | 19 | protected static $templatePath, |
20 | 20 | $globals = []; |
21 | 21 | |
22 | - public function __construct($path=null,$options=[]){ |
|
23 | - self::$templatePath = ($path ? rtrim($path,'/') : __DIR__) . '/'; |
|
22 | + public function __construct($path = null, $options = []) { |
|
23 | + self::$templatePath = ($path ? rtrim($path, '/') : __DIR__).'/'; |
|
24 | 24 | } |
25 | 25 | |
26 | - public static function exists($path){ |
|
27 | - return is_file(self::$templatePath . $path . static::EXTENSION); |
|
26 | + public static function exists($path) { |
|
27 | + return is_file(self::$templatePath.$path.static::EXTENSION); |
|
28 | 28 | } |
29 | 29 | |
30 | - public static function addGlobal($key,$val){ |
|
30 | + public static function addGlobal($key, $val) { |
|
31 | 31 | self::$globals[$key] = $val; |
32 | 32 | } |
33 | 33 | |
34 | - public static function addGlobals(array $defs){ |
|
35 | - foreach ((array)$defs as $key=>$val) { |
|
34 | + public static function addGlobals(array $defs) { |
|
35 | + foreach ((array) $defs as $key=>$val) { |
|
36 | 36 | self::$globals[$key] = $val; |
37 | 37 | } |
38 | 38 | } |
39 | 39 | |
40 | - public function render($template, $data=[]){ |
|
41 | - $template_path = self::$templatePath . trim($template,'/') . static::EXTENSION; |
|
40 | + public function render($template, $data = []) { |
|
41 | + $template_path = self::$templatePath.trim($template, '/').static::EXTENSION; |
|
42 | 42 | $sandbox = function() use ($template_path){ |
43 | 43 | ob_start(); |
44 | 44 | include($template_path); |
@@ -56,19 +56,19 @@ discard block |
||
56 | 56 | class PHPContext { |
57 | 57 | protected $data = []; |
58 | 58 | |
59 | - public function __construct($data=[], $path=null){ |
|
59 | + public function __construct($data = [], $path = null) { |
|
60 | 60 | $this->data = $data; |
61 | 61 | } |
62 | 62 | |
63 | - public function partial($template, $vars=[]){ |
|
64 | - return \View::from($template,array_merge($this->data,$vars)); |
|
63 | + public function partial($template, $vars = []) { |
|
64 | + return \View::from($template, array_merge($this->data, $vars)); |
|
65 | 65 | } |
66 | 66 | |
67 | - public function __isset($n){ return true; } |
|
67 | + public function __isset($n) { return true;} |
|
68 | 68 | |
69 | - public function __unset($n){} |
|
69 | + public function __unset($n) {} |
|
70 | 70 | |
71 | - public function __get($n){ |
|
71 | + public function __get($n) { |
|
72 | 72 | return empty($this->data[$n]) ? '' : $this->data[$n]; |
73 | 73 | } |
74 | 74 | } |
@@ -19,50 +19,50 @@ |
||
19 | 19 | $fileCache; |
20 | 20 | |
21 | 21 | public function __construct(array $options = []) { |
22 | - $this->path = empty($options['root'])?(tempnam(sys_get_temp_dir(), 'CFZ_').'.zip'):rtrim($options['root']); |
|
22 | + $this->path = empty($options['root']) ? (tempnam(sys_get_temp_dir(), 'CFZ_').'.zip') : rtrim($options['root']); |
|
23 | 23 | $this->zipfile = new \ZipArchive(); |
24 | - if ( !$this->zipfile->open($this->path, \ZipArchive::CREATE) ){ |
|
24 | + if (!$this->zipfile->open($this->path, \ZipArchive::CREATE)) { |
|
25 | 25 | throw new \Exception("File::ZIP Cannot open or create ".$this->path); |
26 | 26 | } |
27 | 27 | } |
28 | 28 | |
29 | - public function exists($path){ |
|
29 | + public function exists($path) { |
|
30 | 30 | return false !== $this->zipfile->locateName($path); |
31 | 31 | } |
32 | 32 | |
33 | - public function read($path){ |
|
33 | + public function read($path) { |
|
34 | 34 | if (isset($this->fileCache[$path])) return $this->fileCache[$path]; |
35 | 35 | return $this->exists($path) ? $this->zipfile->getFromName($path) : false; |
36 | 36 | } |
37 | 37 | |
38 | - public function write($path, $data){ |
|
38 | + public function write($path, $data) { |
|
39 | 39 | // This is needed because we cant write and read from the same archive. |
40 | 40 | $this->fileCache[$path] = $data; |
41 | 41 | return $this->zipfile->addFromString($path, $data); |
42 | 42 | } |
43 | 43 | |
44 | - public function append($path, $data){ |
|
45 | - return $this->write($path, ($this->read($path) ?: '') . $data); |
|
44 | + public function append($path, $data) { |
|
45 | + return $this->write($path, ($this->read($path) ?: '').$data); |
|
46 | 46 | } |
47 | 47 | |
48 | - public function delete($path){ |
|
48 | + public function delete($path) { |
|
49 | 49 | return $this->exists($path) ? $this->zipfile->deleteName($path) : false; |
50 | 50 | } |
51 | 51 | |
52 | - public function move($old, $new){ |
|
52 | + public function move($old, $new) { |
|
53 | 53 | // Atomic rename |
54 | 54 | // This is needed because we cant write and read from the same archive. |
55 | - return $this->write($new,$this->read($old)) && $this->delete($old); |
|
55 | + return $this->write($new, $this->read($old)) && $this->delete($old); |
|
56 | 56 | // return $this->zipfile->renameName($old, $new); |
57 | 57 | } |
58 | 58 | |
59 | - public function search($pattern, $recursive=true){ |
|
59 | + public function search($pattern, $recursive = true) { |
|
60 | 60 | $results = []; |
61 | - $rx_pattern = '('.strtr($pattern,['.'=>'\.','*'=>'.*','?'=>'.']).')Ai'; |
|
61 | + $rx_pattern = '('.strtr($pattern, ['.'=>'\.', '*'=>'.*', '?'=>'.']).')Ai'; |
|
62 | 62 | |
63 | - for( $i = 0, $c = $this->zipfile->numFiles; $i < $c; $i++ ){ |
|
64 | - $stat = $this->zipfile->statIndex( $i ); |
|
65 | - if (preg_match($rx_pattern,$stat['name'])) $results[] = $stat['name']; |
|
63 | + for ($i = 0, $c = $this->zipfile->numFiles;$i < $c;$i++) { |
|
64 | + $stat = $this->zipfile->statIndex($i); |
|
65 | + if (preg_match($rx_pattern, $stat['name'])) $results[] = $stat['name']; |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | return $results; |
@@ -21,48 +21,52 @@ |
||
21 | 21 | public function __construct(array $options = []) { |
22 | 22 | $this->path = empty($options['root'])?(tempnam(sys_get_temp_dir(), 'CFZ_').'.zip'):rtrim($options['root']); |
23 | 23 | $this->zipfile = new \ZipArchive(); |
24 | - if ( !$this->zipfile->open($this->path, \ZipArchive::CREATE) ){ |
|
24 | + if ( !$this->zipfile->open($this->path, \ZipArchive::CREATE) ) { |
|
25 | 25 | throw new \Exception("File::ZIP Cannot open or create ".$this->path); |
26 | 26 | } |
27 | 27 | } |
28 | 28 | |
29 | - public function exists($path){ |
|
29 | + public function exists($path) { |
|
30 | 30 | return false !== $this->zipfile->locateName($path); |
31 | 31 | } |
32 | 32 | |
33 | - public function read($path){ |
|
34 | - if (isset($this->fileCache[$path])) return $this->fileCache[$path]; |
|
33 | + public function read($path) { |
|
34 | + if (isset($this->fileCache[$path])) { |
|
35 | + return $this->fileCache[$path]; |
|
36 | + } |
|
35 | 37 | return $this->exists($path) ? $this->zipfile->getFromName($path) : false; |
36 | 38 | } |
37 | 39 | |
38 | - public function write($path, $data){ |
|
40 | + public function write($path, $data) { |
|
39 | 41 | // This is needed because we cant write and read from the same archive. |
40 | 42 | $this->fileCache[$path] = $data; |
41 | 43 | return $this->zipfile->addFromString($path, $data); |
42 | 44 | } |
43 | 45 | |
44 | - public function append($path, $data){ |
|
46 | + public function append($path, $data) { |
|
45 | 47 | return $this->write($path, ($this->read($path) ?: '') . $data); |
46 | 48 | } |
47 | 49 | |
48 | - public function delete($path){ |
|
50 | + public function delete($path) { |
|
49 | 51 | return $this->exists($path) ? $this->zipfile->deleteName($path) : false; |
50 | 52 | } |
51 | 53 | |
52 | - public function move($old, $new){ |
|
54 | + public function move($old, $new) { |
|
53 | 55 | // Atomic rename |
54 | 56 | // This is needed because we cant write and read from the same archive. |
55 | 57 | return $this->write($new,$this->read($old)) && $this->delete($old); |
56 | 58 | // return $this->zipfile->renameName($old, $new); |
57 | 59 | } |
58 | 60 | |
59 | - public function search($pattern, $recursive=true){ |
|
61 | + public function search($pattern, $recursive=true) { |
|
60 | 62 | $results = []; |
61 | 63 | $rx_pattern = '('.strtr($pattern,['.'=>'\.','*'=>'.*','?'=>'.']).')Ai'; |
62 | 64 | |
63 | - for( $i = 0, $c = $this->zipfile->numFiles; $i < $c; $i++ ){ |
|
65 | + for( $i = 0, $c = $this->zipfile->numFiles; $i < $c; $i++ ) { |
|
64 | 66 | $stat = $this->zipfile->statIndex( $i ); |
65 | - if (preg_match($rx_pattern,$stat['name'])) $results[] = $stat['name']; |
|
67 | + if (preg_match($rx_pattern,$stat['name'])) { |
|
68 | + $results[] = $stat['name']; |
|
69 | + } |
|
66 | 70 | } |
67 | 71 | |
68 | 72 | return $results; |
@@ -13,9 +13,9 @@ |
||
13 | 13 | namespace View; |
14 | 14 | |
15 | 15 | interface Adapter { |
16 | - public function __construct($path=null, $options=[]); |
|
17 | - public function render($template,$data=[]); |
|
16 | + public function __construct($path = null, $options = []); |
|
17 | + public function render($template, $data = []); |
|
18 | 18 | public static function exists($path); |
19 | - public static function addGlobal($key,$val); |
|
19 | + public static function addGlobal($key, $val); |
|
20 | 20 | public static function addGlobals(array $defs); |
21 | 21 | } |
@@ -12,12 +12,12 @@ discard block |
||
12 | 12 | |
13 | 13 | class Deferred { |
14 | 14 | |
15 | - protected $callback, |
|
15 | + protected $callback, |
|
16 | 16 | $enabled = true; |
17 | 17 | |
18 | - public function __construct( callable $callback ) { |
|
19 | - $this->callback = $callback; |
|
20 | - } |
|
18 | + public function __construct( callable $callback ) { |
|
19 | + $this->callback = $callback; |
|
20 | + } |
|
21 | 21 | |
22 | 22 | public function disarm() { |
23 | 23 | $this->enabled = false; |
@@ -27,8 +27,8 @@ discard block |
||
27 | 27 | $this->enabled = true; |
28 | 28 | } |
29 | 29 | |
30 | - public function __destruct() { |
|
31 | - if ( $this->enabled ) call_user_func( $this->callback ); |
|
32 | - } |
|
30 | + public function __destruct() { |
|
31 | + if ( $this->enabled ) call_user_func( $this->callback ); |
|
32 | + } |
|
33 | 33 | |
34 | 34 | } |
35 | 35 | \ No newline at end of file |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | protected $callback, |
16 | 16 | $enabled = true; |
17 | 17 | |
18 | - public function __construct( callable $callback ) { |
|
18 | + public function __construct(callable $callback) { |
|
19 | 19 | $this->callback = $callback; |
20 | 20 | } |
21 | 21 | |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | } |
29 | 29 | |
30 | 30 | public function __destruct() { |
31 | - if ( $this->enabled ) call_user_func( $this->callback ); |
|
31 | + if ($this->enabled) call_user_func($this->callback); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | } |
35 | 35 | \ No newline at end of file |
@@ -28,7 +28,9 @@ |
||
28 | 28 | } |
29 | 29 | |
30 | 30 | public function __destruct() { |
31 | - if ( $this->enabled ) call_user_func( $this->callback ); |
|
31 | + if ( $this->enabled ) { |
|
32 | + call_user_func( $this->callback ); |
|
33 | + } |
|
32 | 34 | } |
33 | 35 | |
34 | 36 | } |
35 | 37 | \ No newline at end of file |
@@ -26,7 +26,7 @@ |
||
26 | 26 | * @param mixed $t The text template |
27 | 27 | * @param mixed $v (default: null) The array of values exposed in template. |
28 | 28 | * @return string |
29 | - */ |
|
29 | + */ |
|
30 | 30 | public static function render($t,$v=null){ |
31 | 31 | if (empty($v)) return preg_replace('/{{[^}]+}}/','',$t); |
32 | 32 | for( // Init |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | * @param mixed $v (default: null) The array of values exposed in template. |
28 | 28 | * @return string |
29 | 29 | */ |
30 | - public static function render($t,$v=null){ |
|
30 | + public static function render($t, $v = null) { |
|
31 | 31 | if (Options::get('core.text.replace_empties', true)) { |
32 | 32 | $replacer = function($c) use ($v){ |
33 | 33 | return Object::fetch(trim($c[1]), $v); |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | }; |
39 | 39 | } |
40 | 40 | |
41 | - return preg_replace_callback("(\{\{([^}]+)\}\})S",$replacer,$t); |
|
41 | + return preg_replace_callback("(\{\{([^}]+)\}\})S", $replacer, $t); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | /** |
@@ -53,9 +53,9 @@ discard block |
||
53 | 53 | * @param string $text The text to slugify |
54 | 54 | * @return string The slug. |
55 | 55 | */ |
56 | - public static function slugify($text){ |
|
56 | + public static function slugify($text) { |
|
57 | 57 | return preg_replace( |
58 | - ['(\s+)','([^a-z0-9-])i','(-+)'],['-','','-'], |
|
58 | + ['(\s+)', '([^a-z0-9-])i', '(-+)'], ['-', '', '-'], |
|
59 | 59 | strtolower(self::removeAccents($text))); |
60 | 60 | } |
61 | 61 | |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | * @param string $text The text to translit |
72 | 72 | * @return string The translited text |
73 | 73 | */ |
74 | - public static function removeAccents($text){ |
|
74 | + public static function removeAccents($text) { |
|
75 | 75 | static $diac; |
76 | 76 | return strtr( |
77 | 77 | utf8_decode($text), |
@@ -27,13 +27,13 @@ discard block |
||
27 | 27 | * @param mixed $v (default: null) The array of values exposed in template. |
28 | 28 | * @return string |
29 | 29 | */ |
30 | - public static function render($t,$v=null){ |
|
30 | + public static function render($t,$v=null) { |
|
31 | 31 | if (Options::get('core.text.replace_empties', true)) { |
32 | - $replacer = function($c) use ($v){ |
|
32 | + $replacer = function($c) use ($v) { |
|
33 | 33 | return Object::fetch(trim($c[1]), $v); |
34 | 34 | }; |
35 | 35 | } else { |
36 | - $replacer = function($c) use ($v){ |
|
36 | + $replacer = function($c) use ($v) { |
|
37 | 37 | return Object::fetch(trim($c[1]), $v) ?: $c[0]; |
38 | 38 | }; |
39 | 39 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | * @param string $text The text to slugify |
54 | 54 | * @return string The slug. |
55 | 55 | */ |
56 | - public static function slugify($text){ |
|
56 | + public static function slugify($text) { |
|
57 | 57 | return preg_replace( |
58 | 58 | ['(\s+)','([^a-z0-9-])i','(-+)'],['-','','-'], |
59 | 59 | strtolower(self::removeAccents($text))); |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | * @param string $text The text to translit |
72 | 72 | * @return string The translited text |
73 | 73 | */ |
74 | - public static function removeAccents($text){ |
|
74 | + public static function removeAccents($text) { |
|
75 | 75 | static $diac; |
76 | 76 | return strtr( |
77 | 77 | utf8_decode($text), |
@@ -27,9 +27,9 @@ discard block |
||
27 | 27 | return static::$fields->all(); |
28 | 28 | } |
29 | 29 | |
30 | - public static function get($key, $default=null){ |
|
30 | + public static function get($key, $default = null) { |
|
31 | 31 | if (!static::$fields) static::$fields = new Map(); |
32 | - if (is_array($key)){ |
|
32 | + if (is_array($key)) { |
|
33 | 33 | $results = []; |
34 | 34 | foreach ($key as $_src_key => $_dst_key) |
35 | 35 | $results[$_dst_key] = static::$fields->get($_src_key); |
@@ -39,46 +39,46 @@ discard block |
||
39 | 39 | } |
40 | 40 | } |
41 | 41 | |
42 | - public static function set($key, $value=null){ |
|
42 | + public static function set($key, $value = null) { |
|
43 | 43 | if (!static::$fields) static::$fields = new Map(); |
44 | 44 | return static::$fields->set($key, $value); |
45 | 45 | } |
46 | 46 | |
47 | - public static function delete($key, $compact=true){ |
|
47 | + public static function delete($key, $compact = true) { |
|
48 | 48 | if (!static::$fields) static::$fields = new Map(); |
49 | 49 | static::$fields->delete($key, $compact); |
50 | 50 | } |
51 | 51 | |
52 | - public static function exists($key){ |
|
52 | + public static function exists($key) { |
|
53 | 53 | if (!static::$fields) static::$fields = new Map(); |
54 | 54 | return static::$fields->exists($key); |
55 | 55 | } |
56 | 56 | |
57 | - public static function clear(){ |
|
57 | + public static function clear() { |
|
58 | 58 | if (!static::$fields) static::$fields = new Map(); |
59 | 59 | static::$fields->clear(); |
60 | 60 | } |
61 | 61 | |
62 | - public static function load($fields){ |
|
62 | + public static function load($fields) { |
|
63 | 63 | if (!static::$fields) static::$fields = new Map(); |
64 | 64 | static::$fields->load($fields); |
65 | 65 | } |
66 | 66 | |
67 | - public static function merge(array $array, $merge_back=false){ |
|
67 | + public static function merge(array $array, $merge_back = false) { |
|
68 | 68 | if (!static::$fields) static::$fields = new Map(); |
69 | 69 | static::$fields->merge($array, $merge_back); |
70 | 70 | } |
71 | 71 | |
72 | - protected static function compact(){ |
|
72 | + protected static function compact() { |
|
73 | 73 | if (!static::$fields) static::$fields = new Map(); |
74 | 74 | static::$fields->compact(); |
75 | 75 | } |
76 | 76 | |
77 | - protected static function & find($path, $create=false, callable $operation=null) { |
|
77 | + protected static function & find($path, $create = false, callable $operation = null) { |
|
78 | 78 | return static::$fields->find($path, $create, $operation); |
79 | 79 | } |
80 | 80 | |
81 | - public function jsonSerialize(){ |
|
81 | + public function jsonSerialize() { |
|
82 | 82 | if (!static::$fields) static::$fields = new Map(); |
83 | 83 | return static::$fields->jsonSerialize(); |
84 | 84 | } |
@@ -23,13 +23,17 @@ discard block |
||
23 | 23 | protected static $fields = null; |
24 | 24 | |
25 | 25 | public static function & all(){ |
26 | - if (!static::$fields) static::$fields = new Map(); |
|
26 | + if (!static::$fields) { |
|
27 | + static::$fields = new Map(); |
|
28 | + } |
|
27 | 29 | return static::$fields->all(); |
28 | 30 | } |
29 | 31 | |
30 | - public static function get($key, $default=null){ |
|
31 | - if (!static::$fields) static::$fields = new Map(); |
|
32 | - if (is_array($key)){ |
|
32 | + public static function get($key, $default=null) { |
|
33 | + if (!static::$fields) { |
|
34 | + static::$fields = new Map(); |
|
35 | + } |
|
36 | + if (is_array($key)) { |
|
33 | 37 | $results = []; |
34 | 38 | foreach ($key as $_src_key => $_dst_key) |
35 | 39 | $results[$_dst_key] = static::$fields->get($_src_key); |
@@ -39,38 +43,52 @@ discard block |
||
39 | 43 | } |
40 | 44 | } |
41 | 45 | |
42 | - public static function set($key, $value=null){ |
|
43 | - if (!static::$fields) static::$fields = new Map(); |
|
46 | + public static function set($key, $value=null) { |
|
47 | + if (!static::$fields) { |
|
48 | + static::$fields = new Map(); |
|
49 | + } |
|
44 | 50 | return static::$fields->set($key, $value); |
45 | 51 | } |
46 | 52 | |
47 | - public static function delete($key, $compact=true){ |
|
48 | - if (!static::$fields) static::$fields = new Map(); |
|
53 | + public static function delete($key, $compact=true) { |
|
54 | + if (!static::$fields) { |
|
55 | + static::$fields = new Map(); |
|
56 | + } |
|
49 | 57 | static::$fields->delete($key, $compact); |
50 | 58 | } |
51 | 59 | |
52 | - public static function exists($key){ |
|
53 | - if (!static::$fields) static::$fields = new Map(); |
|
60 | + public static function exists($key) { |
|
61 | + if (!static::$fields) { |
|
62 | + static::$fields = new Map(); |
|
63 | + } |
|
54 | 64 | return static::$fields->exists($key); |
55 | 65 | } |
56 | 66 | |
57 | - public static function clear(){ |
|
58 | - if (!static::$fields) static::$fields = new Map(); |
|
67 | + public static function clear() { |
|
68 | + if (!static::$fields) { |
|
69 | + static::$fields = new Map(); |
|
70 | + } |
|
59 | 71 | static::$fields->clear(); |
60 | 72 | } |
61 | 73 | |
62 | - public static function load($fields){ |
|
63 | - if (!static::$fields) static::$fields = new Map(); |
|
74 | + public static function load($fields) { |
|
75 | + if (!static::$fields) { |
|
76 | + static::$fields = new Map(); |
|
77 | + } |
|
64 | 78 | static::$fields->load($fields); |
65 | 79 | } |
66 | 80 | |
67 | - public static function merge(array $array, $merge_back=false){ |
|
68 | - if (!static::$fields) static::$fields = new Map(); |
|
81 | + public static function merge(array $array, $merge_back=false) { |
|
82 | + if (!static::$fields) { |
|
83 | + static::$fields = new Map(); |
|
84 | + } |
|
69 | 85 | static::$fields->merge($array, $merge_back); |
70 | 86 | } |
71 | 87 | |
72 | - protected static function compact(){ |
|
73 | - if (!static::$fields) static::$fields = new Map(); |
|
88 | + protected static function compact() { |
|
89 | + if (!static::$fields) { |
|
90 | + static::$fields = new Map(); |
|
91 | + } |
|
74 | 92 | static::$fields->compact(); |
75 | 93 | } |
76 | 94 | |
@@ -78,8 +96,10 @@ discard block |
||
78 | 96 | return static::$fields->find($path, $create, $operation); |
79 | 97 | } |
80 | 98 | |
81 | - public function jsonSerialize(){ |
|
82 | - if (!static::$fields) static::$fields = new Map(); |
|
99 | + public function jsonSerialize() { |
|
100 | + if (!static::$fields) { |
|
101 | + static::$fields = new Map(); |
|
102 | + } |
|
83 | 103 | return static::$fields->jsonSerialize(); |
84 | 104 | } |
85 | 105 |
@@ -110,6 +110,9 @@ discard block |
||
110 | 110 | } |
111 | 111 | } |
112 | 112 | |
113 | + /** |
|
114 | + * @param string $key |
|
115 | + */ |
|
113 | 116 | public static function murmur($key, $seed = 0, $as_integer=false) { |
114 | 117 | $key = (string) $key; |
115 | 118 | $klen = strlen($key); |
@@ -161,6 +164,11 @@ discard block |
||
161 | 164 | return strtr(base64_encode(static::random_bytes($bytes)),'+/=','-_'); |
162 | 165 | } |
163 | 166 | |
167 | + /** |
|
168 | + * @param integer $bytes |
|
169 | + * |
|
170 | + * @return string |
|
171 | + */ |
|
164 | 172 | public static function random_bytes($bytes){ |
165 | 173 | static $randf = null; |
166 | 174 | if (function_exists('random_bytes')) { |
@@ -157,11 +157,11 @@ |
||
157 | 157 | return $as_integer ? $h1 : base_convert($h1 ,10, 32); |
158 | 158 | } |
159 | 159 | |
160 | - public static function random($bytes=9){ |
|
160 | + public static function random($bytes=9) { |
|
161 | 161 | return strtr(base64_encode(static::random_bytes($bytes)),'+/=','-_'); |
162 | 162 | } |
163 | 163 | |
164 | - public static function random_bytes($bytes){ |
|
164 | + public static function random_bytes($bytes) { |
|
165 | 165 | static $randf = null; |
166 | 166 | if (function_exists('random_bytes')) { |
167 | 167 | return \random_bytes($bytes); |
@@ -11,104 +11,104 @@ discard block |
||
11 | 11 | */ |
12 | 12 | |
13 | 13 | class Hash { |
14 | - use Module; |
|
15 | - |
|
16 | - /** |
|
17 | - * Create ah hash for payload |
|
18 | - * @param mixed $payload The payload string/object/array |
|
19 | - * @param integer $method The hashing method, default is "md5" |
|
20 | - * @return string The hash string |
|
21 | - */ |
|
22 | - public static function make($payload, $method = 'md5') { |
|
23 | - return $method == 'murmur' ? static::murmur(serialize($payload)) : hash($method, serialize($payload)); |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * Verify if given payload matches hash |
|
28 | - * @param mixed $payload The payload string/object/array |
|
29 | - * @param string $hash The hash string |
|
30 | - * @param integer $method The hashing method |
|
31 | - * @return bool Returns `true` if payload matches hash |
|
32 | - */ |
|
33 | - public static function verify($payload, $hash, $method = 'md5') { |
|
34 | - return static::make($payload, $method) == $hash; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * List registered hashing algorithms |
|
39 | - * |
|
40 | - * @method methods |
|
41 | - * |
|
42 | - * @return array Array containing the list of supported hashing algorithms. |
|
43 | - */ |
|
44 | - public static function methods() { |
|
14 | + use Module; |
|
15 | + |
|
16 | + /** |
|
17 | + * Create ah hash for payload |
|
18 | + * @param mixed $payload The payload string/object/array |
|
19 | + * @param integer $method The hashing method, default is "md5" |
|
20 | + * @return string The hash string |
|
21 | + */ |
|
22 | + public static function make($payload, $method = 'md5') { |
|
23 | + return $method == 'murmur' ? static::murmur(serialize($payload)) : hash($method, serialize($payload)); |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * Verify if given payload matches hash |
|
28 | + * @param mixed $payload The payload string/object/array |
|
29 | + * @param string $hash The hash string |
|
30 | + * @param integer $method The hashing method |
|
31 | + * @return bool Returns `true` if payload matches hash |
|
32 | + */ |
|
33 | + public static function verify($payload, $hash, $method = 'md5') { |
|
34 | + return static::make($payload, $method) == $hash; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * List registered hashing algorithms |
|
39 | + * |
|
40 | + * @method methods |
|
41 | + * |
|
42 | + * @return array Array containing the list of supported hashing algorithms. |
|
43 | + */ |
|
44 | + public static function methods() { |
|
45 | 45 | // Merge PHP provided algos with ours (murmur) |
46 | - return array_merge(hash_algos(), ['murmur','murmurhash3']); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Check if an alghoritm is registered in current PHP |
|
51 | - * |
|
52 | - * @method can |
|
53 | - * |
|
54 | - * @param string $algo The hashing algorithm name |
|
55 | - * |
|
56 | - * @return bool |
|
57 | - */ |
|
58 | - public static function can($algo) { |
|
46 | + return array_merge(hash_algos(), ['murmur','murmurhash3']); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Check if an alghoritm is registered in current PHP |
|
51 | + * |
|
52 | + * @method can |
|
53 | + * |
|
54 | + * @param string $algo The hashing algorithm name |
|
55 | + * |
|
56 | + * @return bool |
|
57 | + */ |
|
58 | + public static function can($algo) { |
|
59 | 59 | // Faster than : in_array(explode(',',implode(',',static::methods()))) |
60 | - return strpos(implode(',',static::methods()).',', "$algo,") !== false; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Static magic for creating hashes with a specified algorithm. |
|
65 | - * |
|
66 | - * See [hash-algos](http://php.net/manual/it/function.hash-algos.php) for a list of algorithms |
|
67 | - */ |
|
68 | - public static function __callStatic($method, $params) { |
|
69 | - return self::make(current($params), $method); |
|
70 | - } |
|
71 | - |
|
72 | - public static function uuid($type = 4, $namespace = '', $name = '') { |
|
73 | - switch ($type) { |
|
74 | - case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
75 | - '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
76 | - return false; |
|
77 | - } |
|
78 | - |
|
79 | - $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
80 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
81 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
82 | - } |
|
83 | - |
|
84 | - $hash = md5($nstr . $name); |
|
85 | - return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
86 | - substr($hash, 0, 8), substr($hash, 8, 4), |
|
87 | - (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
|
88 | - (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
89 | - substr($hash, 20, 12)); |
|
90 | - case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
91 | - '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
92 | - return false; |
|
93 | - } |
|
94 | - |
|
95 | - $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
96 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
97 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
98 | - } |
|
99 | - |
|
100 | - $hash = sha1($nstr . $name); |
|
101 | - return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
102 | - substr($hash, 0, 8), substr($hash, 8, 4), |
|
103 | - (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
|
104 | - (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
105 | - substr($hash, 20, 12)); |
|
106 | - default:case 4:return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
107 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
108 | - mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, |
|
109 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
110 | - } |
|
111 | - } |
|
60 | + return strpos(implode(',',static::methods()).',', "$algo,") !== false; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Static magic for creating hashes with a specified algorithm. |
|
65 | + * |
|
66 | + * See [hash-algos](http://php.net/manual/it/function.hash-algos.php) for a list of algorithms |
|
67 | + */ |
|
68 | + public static function __callStatic($method, $params) { |
|
69 | + return self::make(current($params), $method); |
|
70 | + } |
|
71 | + |
|
72 | + public static function uuid($type = 4, $namespace = '', $name = '') { |
|
73 | + switch ($type) { |
|
74 | + case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
75 | + '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
76 | + return false; |
|
77 | + } |
|
78 | + |
|
79 | + $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
80 | + $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
81 | + $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
82 | + } |
|
83 | + |
|
84 | + $hash = md5($nstr . $name); |
|
85 | + return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
86 | + substr($hash, 0, 8), substr($hash, 8, 4), |
|
87 | + (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
|
88 | + (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
89 | + substr($hash, 20, 12)); |
|
90 | + case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
91 | + '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
92 | + return false; |
|
93 | + } |
|
94 | + |
|
95 | + $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
96 | + $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
97 | + $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
98 | + } |
|
99 | + |
|
100 | + $hash = sha1($nstr . $name); |
|
101 | + return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
102 | + substr($hash, 0, 8), substr($hash, 8, 4), |
|
103 | + (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
|
104 | + (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
105 | + substr($hash, 20, 12)); |
|
106 | + default:case 4:return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
107 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
108 | + mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, |
|
109 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
110 | + } |
|
111 | + } |
|
112 | 112 | |
113 | 113 | public static function murmur($key, $seed = 0, $as_integer=false) { |
114 | 114 | $key = array_values(unpack('C*',(string) $key)); |
@@ -145,8 +145,8 @@ discard block |
||
145 | 145 | $h1 = (((($h1 & 0xffff) * 0xc2b2ae35) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff; |
146 | 146 | $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
147 | 147 | |
148 | - return $as_integer ? $h1 : base_convert($h1 ,10, 32); |
|
149 | - } |
|
148 | + return $as_integer ? $h1 : base_convert($h1 ,10, 32); |
|
149 | + } |
|
150 | 150 | |
151 | 151 | public static function random($bytes=9){ |
152 | 152 | return strtr(base64_encode(static::random_bytes($bytes)),'+/=','-_'); |
@@ -71,42 +71,42 @@ |
||
71 | 71 | |
72 | 72 | public static function uuid($type = 4, $namespace = '', $name = '') { |
73 | 73 | switch ($type) { |
74 | - case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
75 | - '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
76 | - return false; |
|
77 | - } |
|
78 | - |
|
79 | - $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
80 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
81 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
82 | - } |
|
83 | - |
|
84 | - $hash = md5($nstr . $name); |
|
85 | - return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
86 | - substr($hash, 0, 8), substr($hash, 8, 4), |
|
87 | - (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
|
88 | - (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
89 | - substr($hash, 20, 12)); |
|
90 | - case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
91 | - '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
92 | - return false; |
|
93 | - } |
|
94 | - |
|
95 | - $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
96 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
97 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
98 | - } |
|
99 | - |
|
100 | - $hash = sha1($nstr . $name); |
|
101 | - return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
102 | - substr($hash, 0, 8), substr($hash, 8, 4), |
|
103 | - (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
|
104 | - (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
105 | - substr($hash, 20, 12)); |
|
106 | - default:case 4:return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
107 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
108 | - mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, |
|
109 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
74 | + case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
75 | + '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
76 | + return false; |
|
77 | + } |
|
78 | + |
|
79 | + $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
80 | + $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
81 | + $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
82 | + } |
|
83 | + |
|
84 | + $hash = md5($nstr . $name); |
|
85 | + return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
86 | + substr($hash, 0, 8), substr($hash, 8, 4), |
|
87 | + (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
|
88 | + (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
89 | + substr($hash, 20, 12)); |
|
90 | + case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
91 | + '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
92 | + return false; |
|
93 | + } |
|
94 | + |
|
95 | + $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
96 | + $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
97 | + $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
98 | + } |
|
99 | + |
|
100 | + $hash = sha1($nstr . $name); |
|
101 | + return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
102 | + substr($hash, 0, 8), substr($hash, 8, 4), |
|
103 | + (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
|
104 | + (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
105 | + substr($hash, 20, 12)); |
|
106 | + default:case 4:return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
107 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
108 | + mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, |
|
109 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
110 | 110 | } |
111 | 111 | } |
112 | 112 |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | */ |
44 | 44 | public static function methods() { |
45 | 45 | // Merge PHP provided algos with ours (murmur) |
46 | - return array_merge(hash_algos(), ['murmur','murmurhash3']); |
|
46 | + return array_merge(hash_algos(), ['murmur', 'murmurhash3']); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | */ |
58 | 58 | public static function can($algo) { |
59 | 59 | // Faster than : in_array(explode(',',implode(',',static::methods()))) |
60 | - return strpos(implode(',',static::methods()).',', "$algo,") !== false; |
|
60 | + return strpos(implode(',', static::methods()).',', "$algo,") !== false; |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | /** |
@@ -71,33 +71,33 @@ discard block |
||
71 | 71 | |
72 | 72 | public static function uuid($type = 4, $namespace = '', $name = '') { |
73 | 73 | switch ($type) { |
74 | - case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
74 | + case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'. |
|
75 | 75 | '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
76 | 76 | return false; |
77 | 77 | } |
78 | 78 | |
79 | 79 | $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
80 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
81 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
80 | + $nstr = '';for ($i = 0;$i < strlen($nhex);$i += 2) { |
|
81 | + $nstr .= chr(hexdec($nhex[$i].$nhex[$i + 1])); |
|
82 | 82 | } |
83 | 83 | |
84 | - $hash = md5($nstr . $name); |
|
84 | + $hash = md5($nstr.$name); |
|
85 | 85 | return sprintf('%08s-%04s-%04x-%04x-%12s', |
86 | 86 | substr($hash, 0, 8), substr($hash, 8, 4), |
87 | 87 | (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
88 | 88 | (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
89 | 89 | substr($hash, 20, 12)); |
90 | - case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
90 | + case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'. |
|
91 | 91 | '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
92 | 92 | return false; |
93 | 93 | } |
94 | 94 | |
95 | 95 | $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
96 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
97 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
96 | + $nstr = '';for ($i = 0;$i < strlen($nhex);$i += 2) { |
|
97 | + $nstr .= chr(hexdec($nhex[$i].$nhex[$i + 1])); |
|
98 | 98 | } |
99 | 99 | |
100 | - $hash = sha1($nstr . $name); |
|
100 | + $hash = sha1($nstr.$name); |
|
101 | 101 | return sprintf('%08s-%04s-%04x-%04x-%12s', |
102 | 102 | substr($hash, 0, 8), substr($hash, 8, 4), |
103 | 103 | (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
@@ -110,11 +110,11 @@ discard block |
||
110 | 110 | } |
111 | 111 | } |
112 | 112 | |
113 | - public static function murmur($key, $seed = 0, $as_integer=false) { |
|
114 | - $key = array_values(unpack('C*',(string) $key)); |
|
113 | + public static function murmur($key, $seed = 0, $as_integer = false) { |
|
114 | + $key = array_values(unpack('C*', (string) $key)); |
|
115 | 115 | $klen = count($key); |
116 | - $h1 = (int)$seed; |
|
117 | - for ($i=0,$bytes=$klen-($remainder=$klen&3) ; $i<$bytes ; ) { |
|
116 | + $h1 = (int) $seed; |
|
117 | + for ($i = 0, $bytes = $klen - ($remainder = $klen & 3);$i < $bytes;) { |
|
118 | 118 | $k1 = $key[$i] |
119 | 119 | | ($key[++$i] << 8) |
120 | 120 | | ($key[++$i] << 16) |
@@ -145,14 +145,14 @@ discard block |
||
145 | 145 | $h1 = (((($h1 & 0xffff) * 0xc2b2ae35) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff; |
146 | 146 | $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
147 | 147 | |
148 | - return $as_integer ? $h1 : base_convert($h1 ,10, 32); |
|
148 | + return $as_integer ? $h1 : base_convert($h1, 10, 32); |
|
149 | 149 | } |
150 | 150 | |
151 | - public static function random($bytes=9){ |
|
152 | - return strtr(base64_encode(static::random_bytes($bytes)),'+/=','-_'); |
|
151 | + public static function random($bytes = 9) { |
|
152 | + return strtr(base64_encode(static::random_bytes($bytes)), '+/=', '-_'); |
|
153 | 153 | } |
154 | 154 | |
155 | - public static function random_bytes($bytes){ |
|
155 | + public static function random_bytes($bytes) { |
|
156 | 156 | static $randf = null; |
157 | 157 | if (function_exists('random_bytes')) { |
158 | 158 | return \random_bytes($bytes); |