@@ -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,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){ |
|
| 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){ |
|
| 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,'/') . '.php'; |
|
| 40 | + public function render($template, $data = []) { |
|
| 41 | + $template_path = self::$templatePath.trim($template, '/').'.php'; |
|
| 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,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 | } |
@@ -11,151 +11,151 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | class Hash { |
| 14 | - use Module; |
|
| 14 | + use Module; |
|
| 15 | 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 hash($method, serialize($payload)); |
|
| 24 | - } |
|
| 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 hash($method, serialize($payload)); |
|
| 24 | + } |
|
| 25 | 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 | - } |
|
| 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 | 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 | - return hash_algos(); |
|
| 46 | - } |
|
| 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 | + return hash_algos(); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Check if an alghoritm is registered in current PHP |
|
| 50 | - * |
|
| 51 | - * @method can |
|
| 52 | - * |
|
| 53 | - * @param string $algo The hashing algorithm name |
|
| 54 | - * |
|
| 55 | - * @return bool |
|
| 56 | - */ |
|
| 57 | - public static function can($algo) { |
|
| 58 | - return in_array($algo, hash_algos()); |
|
| 59 | - } |
|
| 48 | + /** |
|
| 49 | + * Check if an alghoritm is registered in current PHP |
|
| 50 | + * |
|
| 51 | + * @method can |
|
| 52 | + * |
|
| 53 | + * @param string $algo The hashing algorithm name |
|
| 54 | + * |
|
| 55 | + * @return bool |
|
| 56 | + */ |
|
| 57 | + public static function can($algo) { |
|
| 58 | + return in_array($algo, hash_algos()); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Static magic for creating hashes with a specified algorithm. |
|
| 63 | - * |
|
| 64 | - * See [hash-algos](http://php.net/manual/it/function.hash-algos.php) for a list of algorithms |
|
| 65 | - */ |
|
| 66 | - public static function __callStatic($method, $params) { |
|
| 67 | - return self::make(current($params), $method); |
|
| 68 | - } |
|
| 61 | + /** |
|
| 62 | + * Static magic for creating hashes with a specified algorithm. |
|
| 63 | + * |
|
| 64 | + * See [hash-algos](http://php.net/manual/it/function.hash-algos.php) for a list of algorithms |
|
| 65 | + */ |
|
| 66 | + public static function __callStatic($method, $params) { |
|
| 67 | + return self::make(current($params), $method); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - public static function uuid($type = 4, $namespace = '', $name = '') { |
|
| 71 | - switch ($type) { |
|
| 72 | - case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
| 73 | - '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
| 74 | - return false; |
|
| 75 | - } |
|
| 70 | + public static function uuid($type = 4, $namespace = '', $name = '') { |
|
| 71 | + switch ($type) { |
|
| 72 | + case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
| 73 | + '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
| 78 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
| 79 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
| 80 | - } |
|
| 77 | + $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
| 78 | + $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
| 79 | + $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - $hash = md5($nstr . $name); |
|
| 83 | - return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
| 84 | - substr($hash, 0, 8), substr($hash, 8, 4), |
|
| 85 | - (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
|
| 86 | - (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
| 87 | - substr($hash, 20, 12)); |
|
| 88 | - case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
| 89 | - '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
| 90 | - return false; |
|
| 91 | - } |
|
| 82 | + $hash = md5($nstr . $name); |
|
| 83 | + return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
| 84 | + substr($hash, 0, 8), substr($hash, 8, 4), |
|
| 85 | + (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
|
| 86 | + (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
| 87 | + substr($hash, 20, 12)); |
|
| 88 | + case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
| 89 | + '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
| 90 | + return false; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
| 94 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
| 95 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
| 96 | - } |
|
| 93 | + $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
| 94 | + $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
| 95 | + $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - $hash = sha1($nstr . $name); |
|
| 99 | - return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
| 100 | - substr($hash, 0, 8), substr($hash, 8, 4), |
|
| 101 | - (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
|
| 102 | - (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
| 103 | - substr($hash, 20, 12)); |
|
| 104 | - default:case 4:return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
| 105 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
| 106 | - mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, |
|
| 107 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
| 108 | - } |
|
| 109 | - } |
|
| 98 | + $hash = sha1($nstr . $name); |
|
| 99 | + return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
| 100 | + substr($hash, 0, 8), substr($hash, 8, 4), |
|
| 101 | + (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
|
| 102 | + (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
| 103 | + substr($hash, 20, 12)); |
|
| 104 | + default:case 4:return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
| 105 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
| 106 | + mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, |
|
| 107 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - public static function murmurhash3_int($key, $seed = 0) { |
|
| 112 | - $key = (string) $key; |
|
| 113 | - $klen = strlen($key); |
|
| 114 | - $h1 = $seed; |
|
| 115 | - for ($i = 0, $bytes = $klen - ($remainder = $klen & 3); $i < $bytes;) { |
|
| 116 | - $k1 = ((ord($key[$i]) & 0xff)) |
|
| 117 | - | ((ord($key[++$i]) & 0xff) << 8) |
|
| 118 | - | ((ord($key[++$i]) & 0xff) << 16) |
|
| 119 | - | ((ord($key[++$i]) & 0xff) << 24); |
|
| 120 | - ++$i; |
|
| 121 | - $k1 = (((($k1 & 0xffff) * 0xcc9e2d51) |
|
| 122 | - + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) * 0xcc9e2d51) & 0xffff) << 16))) |
|
| 123 | - & 0xffffffff; |
|
| 124 | - $k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
|
| 125 | - $k1 = (((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 126 | - * 0x1b873593) & 0xffff) << 16))) & 0xffffffff; |
|
| 127 | - $h1 ^= $k1; |
|
| 128 | - $h1 = $h1 << 13 | ($h1 >= 0 ? $h1 >> 19 : (($h1 & 0x7fffffff) >> 19) | 0x1000); |
|
| 129 | - $h1b = (((($h1 & 0xffff) * 5) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) * 5) |
|
| 130 | - & 0xffff) << 16))) & 0xffffffff; |
|
| 131 | - $h1 = ((($h1b & 0xffff) + 0x6b64) + ((((($h1b >= 0 ? $h1b >> 16 : (($h1b & 0x7fffffff) >> 16) | 0x8000)) |
|
| 132 | - + 0xe654) & 0xffff) << 16)); |
|
| 133 | - } |
|
| 134 | - $k1 = 0; |
|
| 135 | - switch ($remainder) { |
|
| 136 | - case 3:$k1 ^= (ord($key[$i + 2]) & 0xff) << 16; |
|
| 137 | - case 2:$k1 ^= (ord($key[$i + 1]) & 0xff) << 8; |
|
| 138 | - case 1:$k1 ^= (ord($key[$i]) & 0xff); |
|
| 139 | - $k1 = ((($k1 & 0xffff) * 0xcc9e2d51) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 140 | - * 0xcc9e2d51) & 0xffff) << 16)) & 0xffffffff; |
|
| 141 | - $k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
|
| 142 | - $k1 = ((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 143 | - * 0x1b873593) & 0xffff) << 16)) & 0xffffffff; |
|
| 144 | - $h1 ^= $k1; |
|
| 145 | - } |
|
| 146 | - $h1 ^= $klen; |
|
| 147 | - $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
|
| 148 | - $h1 = ((($h1 & 0xffff) * 0x85ebca6b) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 149 | - * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff; |
|
| 150 | - $h1 ^= ($h1 >= 0 ? $h1 >> 13 : (($h1 & 0x7fffffff) >> 13) | 0x40000); |
|
| 151 | - $h1 = (((($h1 & 0xffff) * 0xc2b2ae35) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 152 | - * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff; |
|
| 153 | - $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
|
| 154 | - return $h1; |
|
| 155 | - } |
|
| 111 | + public static function murmurhash3_int($key, $seed = 0) { |
|
| 112 | + $key = (string) $key; |
|
| 113 | + $klen = strlen($key); |
|
| 114 | + $h1 = $seed; |
|
| 115 | + for ($i = 0, $bytes = $klen - ($remainder = $klen & 3); $i < $bytes;) { |
|
| 116 | + $k1 = ((ord($key[$i]) & 0xff)) |
|
| 117 | + | ((ord($key[++$i]) & 0xff) << 8) |
|
| 118 | + | ((ord($key[++$i]) & 0xff) << 16) |
|
| 119 | + | ((ord($key[++$i]) & 0xff) << 24); |
|
| 120 | + ++$i; |
|
| 121 | + $k1 = (((($k1 & 0xffff) * 0xcc9e2d51) |
|
| 122 | + + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) * 0xcc9e2d51) & 0xffff) << 16))) |
|
| 123 | + & 0xffffffff; |
|
| 124 | + $k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
|
| 125 | + $k1 = (((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 126 | + * 0x1b873593) & 0xffff) << 16))) & 0xffffffff; |
|
| 127 | + $h1 ^= $k1; |
|
| 128 | + $h1 = $h1 << 13 | ($h1 >= 0 ? $h1 >> 19 : (($h1 & 0x7fffffff) >> 19) | 0x1000); |
|
| 129 | + $h1b = (((($h1 & 0xffff) * 5) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) * 5) |
|
| 130 | + & 0xffff) << 16))) & 0xffffffff; |
|
| 131 | + $h1 = ((($h1b & 0xffff) + 0x6b64) + ((((($h1b >= 0 ? $h1b >> 16 : (($h1b & 0x7fffffff) >> 16) | 0x8000)) |
|
| 132 | + + 0xe654) & 0xffff) << 16)); |
|
| 133 | + } |
|
| 134 | + $k1 = 0; |
|
| 135 | + switch ($remainder) { |
|
| 136 | + case 3:$k1 ^= (ord($key[$i + 2]) & 0xff) << 16; |
|
| 137 | + case 2:$k1 ^= (ord($key[$i + 1]) & 0xff) << 8; |
|
| 138 | + case 1:$k1 ^= (ord($key[$i]) & 0xff); |
|
| 139 | + $k1 = ((($k1 & 0xffff) * 0xcc9e2d51) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 140 | + * 0xcc9e2d51) & 0xffff) << 16)) & 0xffffffff; |
|
| 141 | + $k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
|
| 142 | + $k1 = ((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 143 | + * 0x1b873593) & 0xffff) << 16)) & 0xffffffff; |
|
| 144 | + $h1 ^= $k1; |
|
| 145 | + } |
|
| 146 | + $h1 ^= $klen; |
|
| 147 | + $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
|
| 148 | + $h1 = ((($h1 & 0xffff) * 0x85ebca6b) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 149 | + * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff; |
|
| 150 | + $h1 ^= ($h1 >= 0 ? $h1 >> 13 : (($h1 & 0x7fffffff) >> 13) | 0x40000); |
|
| 151 | + $h1 = (((($h1 & 0xffff) * 0xc2b2ae35) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 152 | + * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff; |
|
| 153 | + $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
|
| 154 | + return $h1; |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - public static function murmurhash3($key, $seed = 0) { |
|
| 158 | - return base_convert(static::murmurhash3_int($key, $seed), 10, 32); |
|
| 159 | - } |
|
| 157 | + public static function murmurhash3($key, $seed = 0) { |
|
| 158 | + return base_convert(static::murmurhash3_int($key, $seed), 10, 32); |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | 161 | } |
@@ -69,42 +69,42 @@ discard block |
||
| 69 | 69 | |
| 70 | 70 | public static function uuid($type = 4, $namespace = '', $name = '') { |
| 71 | 71 | switch ($type) { |
| 72 | - case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
| 73 | - '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
| 74 | - return false; |
|
| 75 | - } |
|
| 72 | + case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
| 73 | + '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
| 78 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
| 79 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
| 80 | - } |
|
| 77 | + $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
| 78 | + $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
| 79 | + $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - $hash = md5($nstr . $name); |
|
| 83 | - return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
| 84 | - substr($hash, 0, 8), substr($hash, 8, 4), |
|
| 85 | - (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
|
| 86 | - (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
| 87 | - substr($hash, 20, 12)); |
|
| 88 | - case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
| 89 | - '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
| 90 | - return false; |
|
| 91 | - } |
|
| 82 | + $hash = md5($nstr . $name); |
|
| 83 | + return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
| 84 | + substr($hash, 0, 8), substr($hash, 8, 4), |
|
| 85 | + (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
|
| 86 | + (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
| 87 | + substr($hash, 20, 12)); |
|
| 88 | + case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
| 89 | + '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
| 90 | + return false; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
| 94 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
| 95 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
| 96 | - } |
|
| 93 | + $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
| 94 | + $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
| 95 | + $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - $hash = sha1($nstr . $name); |
|
| 99 | - return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
| 100 | - substr($hash, 0, 8), substr($hash, 8, 4), |
|
| 101 | - (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
|
| 102 | - (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
| 103 | - substr($hash, 20, 12)); |
|
| 104 | - default:case 4:return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
| 105 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
| 106 | - mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, |
|
| 107 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
| 98 | + $hash = sha1($nstr . $name); |
|
| 99 | + return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
| 100 | + substr($hash, 0, 8), substr($hash, 8, 4), |
|
| 101 | + (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
|
| 102 | + (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
| 103 | + substr($hash, 20, 12)); |
|
| 104 | + default:case 4:return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
| 105 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
| 106 | + mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, |
|
| 107 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
| 108 | 108 | } |
| 109 | 109 | } |
| 110 | 110 | |
@@ -133,15 +133,15 @@ discard block |
||
| 133 | 133 | } |
| 134 | 134 | $k1 = 0; |
| 135 | 135 | switch ($remainder) { |
| 136 | - case 3:$k1 ^= (ord($key[$i + 2]) & 0xff) << 16; |
|
| 137 | - case 2:$k1 ^= (ord($key[$i + 1]) & 0xff) << 8; |
|
| 138 | - case 1:$k1 ^= (ord($key[$i]) & 0xff); |
|
| 139 | - $k1 = ((($k1 & 0xffff) * 0xcc9e2d51) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 140 | - * 0xcc9e2d51) & 0xffff) << 16)) & 0xffffffff; |
|
| 141 | - $k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
|
| 142 | - $k1 = ((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 143 | - * 0x1b873593) & 0xffff) << 16)) & 0xffffffff; |
|
| 144 | - $h1 ^= $k1; |
|
| 136 | + case 3:$k1 ^= (ord($key[$i + 2]) & 0xff) << 16; |
|
| 137 | + case 2:$k1 ^= (ord($key[$i + 1]) & 0xff) << 8; |
|
| 138 | + case 1:$k1 ^= (ord($key[$i]) & 0xff); |
|
| 139 | + $k1 = ((($k1 & 0xffff) * 0xcc9e2d51) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 140 | + * 0xcc9e2d51) & 0xffff) << 16)) & 0xffffffff; |
|
| 141 | + $k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
|
| 142 | + $k1 = ((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
| 143 | + * 0x1b873593) & 0xffff) << 16)) & 0xffffffff; |
|
| 144 | + $h1 ^= $k1; |
|
| 145 | 145 | } |
| 146 | 146 | $h1 ^= $klen; |
| 147 | 147 | $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
@@ -69,33 +69,33 @@ discard block |
||
| 69 | 69 | |
| 70 | 70 | public static function uuid($type = 4, $namespace = '', $name = '') { |
| 71 | 71 | switch ($type) { |
| 72 | - case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
| 72 | + case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'. |
|
| 73 | 73 | '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
| 74 | 74 | return false; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
| 78 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
| 79 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
| 78 | + $nstr = '';for ($i = 0;$i < strlen($nhex);$i += 2) { |
|
| 79 | + $nstr .= chr(hexdec($nhex[$i].$nhex[$i + 1])); |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | - $hash = md5($nstr . $name); |
|
| 82 | + $hash = md5($nstr.$name); |
|
| 83 | 83 | return sprintf('%08s-%04s-%04x-%04x-%12s', |
| 84 | 84 | substr($hash, 0, 8), substr($hash, 8, 4), |
| 85 | 85 | (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
| 86 | 86 | (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
| 87 | 87 | substr($hash, 20, 12)); |
| 88 | - case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
| 88 | + case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'. |
|
| 89 | 89 | '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
| 90 | 90 | return false; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
| 94 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
| 95 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
| 94 | + $nstr = '';for ($i = 0;$i < strlen($nhex);$i += 2) { |
|
| 95 | + $nstr .= chr(hexdec($nhex[$i].$nhex[$i + 1])); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - $hash = sha1($nstr . $name); |
|
| 98 | + $hash = sha1($nstr.$name); |
|
| 99 | 99 | return sprintf('%08s-%04s-%04x-%04x-%12s', |
| 100 | 100 | substr($hash, 0, 8), substr($hash, 8, 4), |
| 101 | 101 | (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | $key = (string) $key; |
| 113 | 113 | $klen = strlen($key); |
| 114 | 114 | $h1 = $seed; |
| 115 | - for ($i = 0, $bytes = $klen - ($remainder = $klen & 3); $i < $bytes;) { |
|
| 115 | + for ($i = 0, $bytes = $klen - ($remainder = $klen & 3);$i < $bytes;) { |
|
| 116 | 116 | $k1 = ((ord($key[$i]) & 0xff)) |
| 117 | 117 | | ((ord($key[++$i]) & 0xff) << 8) |
| 118 | 118 | | ((ord($key[++$i]) & 0xff) << 16) |
@@ -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; |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * @param string $method The HTTP method for which the route must respond. |
| 39 | 39 | * @return Route |
| 40 | 40 | */ |
| 41 | - public function __construct($URLPattern, $callback = null, $method='get'){ |
|
| 41 | + public function __construct($URLPattern, $callback = null, $method='get') { |
|
| 42 | 42 | $this->URLPattern = rtrim(implode('',static::$prefix),'/') . '/' . trim($URLPattern,'/')?:'/'; |
| 43 | 43 | $this->URLPattern = $this->URLPattern != '/' ? rtrim($this->URLPattern,'/') : $this->URLPattern; |
| 44 | 44 | $this->dynamic = $this->isDynamic($this->URLPattern); |
@@ -56,19 +56,23 @@ discard block |
||
| 56 | 56 | * @param string $method The HTTP Method to check against. |
| 57 | 57 | * @return boolean |
| 58 | 58 | */ |
| 59 | - public function match($URL,$method='get'){ |
|
| 59 | + public function match($URL,$method='get') { |
|
| 60 | 60 | $method = strtolower($method); |
| 61 | 61 | |
| 62 | 62 | // * is an http method wildcard |
| 63 | - if (empty($this->methods[$method]) && empty($this->methods['*'])) return false; |
|
| 63 | + if (empty($this->methods[$method]) && empty($this->methods['*'])) { |
|
| 64 | + return false; |
|
| 65 | + } |
|
| 64 | 66 | $URL = rtrim($URL,'/'); |
| 65 | 67 | $args = []; |
| 66 | 68 | if ( $this->dynamic |
| 67 | 69 | ? preg_match($this->pattern,$URL,$args) |
| 68 | 70 | : $URL == rtrim($this->pattern,'/') |
| 69 | - ){ |
|
| 71 | + ) { |
|
| 70 | 72 | foreach ($args as $key => $value) { |
| 71 | - if ( false === is_string($key) ) unset($args[$key]); |
|
| 73 | + if ( false === is_string($key) ) { |
|
| 74 | + unset($args[$key]); |
|
| 75 | + } |
|
| 72 | 76 | } |
| 73 | 77 | return $args; |
| 74 | 78 | } |
@@ -81,7 +85,7 @@ discard block |
||
| 81 | 85 | * @param string $method The HTTP Method requested. |
| 82 | 86 | * @return array The callback response. |
| 83 | 87 | */ |
| 84 | - public function run(array $args,$method='get'){ |
|
| 88 | + public function run(array $args,$method='get') { |
|
| 85 | 89 | $method = strtolower($method); |
| 86 | 90 | $this->response = ''; |
| 87 | 91 | $this->response_object = null; |
@@ -150,7 +154,7 @@ discard block |
||
| 150 | 154 | |
| 151 | 155 | Event::trigger('core.route.after', $this); |
| 152 | 156 | |
| 153 | - if ( $this->response_is_object ){ |
|
| 157 | + if ( $this->response_is_object ) { |
|
| 154 | 158 | $this->response = Response::json($this->response_object); |
| 155 | 159 | } else { |
| 156 | 160 | Response::add($this->response); |
@@ -167,7 +171,7 @@ discard block |
||
| 167 | 171 | * @param string $method The HTTP Method to check against. |
| 168 | 172 | * @return array The callback response. |
| 169 | 173 | */ |
| 170 | - public function runIfMatch($URL, $method='get'){ |
|
| 174 | + public function runIfMatch($URL, $method='get') { |
|
| 171 | 175 | return ($args = $this->match($URL,$method)) ? $this->run($args,$method) : null; |
| 172 | 176 | } |
| 173 | 177 | |
@@ -177,7 +181,7 @@ discard block |
||
| 177 | 181 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 178 | 182 | * @return Route |
| 179 | 183 | */ |
| 180 | - public static function on($URLPattern, $callback = null){ |
|
| 184 | + public static function on($URLPattern, $callback = null) { |
|
| 181 | 185 | return new Route($URLPattern,$callback); |
| 182 | 186 | } |
| 183 | 187 | |
@@ -187,7 +191,7 @@ discard block |
||
| 187 | 191 | * @param $callback The callback to be invoked (with variables extracted from the route if present) when the route match the request URI. |
| 188 | 192 | * @return Route |
| 189 | 193 | */ |
| 190 | - public static function any($URLPattern, $callback = null){ |
|
| 194 | + public static function any($URLPattern, $callback = null) { |
|
| 191 | 195 | return (new Route($URLPattern,$callback))->via('*'); |
| 192 | 196 | } |
| 193 | 197 | |
@@ -233,7 +237,7 @@ discard block |
||
| 233 | 237 | */ |
| 234 | 238 | public function & via(){ |
| 235 | 239 | $this->methods = []; |
| 236 | - foreach (func_get_args() as $method){ |
|
| 240 | + foreach (func_get_args() as $method) { |
|
| 237 | 241 | $this->methods[strtolower($method)] = true; |
| 238 | 242 | } |
| 239 | 243 | return $this; |
@@ -255,7 +259,7 @@ discard block |
||
| 255 | 259 | * @return Route |
| 256 | 260 | */ |
| 257 | 261 | public function & rules(array $rules){ |
| 258 | - foreach ((array)$rules as $varname => $rule){ |
|
| 262 | + foreach ((array)$rules as $varname => $rule) { |
|
| 259 | 263 | $this->rules[$varname] = $rule; |
| 260 | 264 | } |
| 261 | 265 | $this->pattern = $this->compilePatternAsRegex($this->URLPattern,$this->rules); |
@@ -284,7 +288,9 @@ discard block |
||
| 284 | 288 | $route->callback = []; |
| 285 | 289 | foreach ($callbacks as $method => $callback) { |
| 286 | 290 | $method = strtolower($method); |
| 287 | - if (Request::method() !== $method) continue; |
|
| 291 | + if (Request::method() !== $method) { |
|
| 292 | + continue; |
|
| 293 | + } |
|
| 288 | 294 | $route->callback[$method] = $callback; |
| 289 | 295 | $route->methods[$method] = 1; |
| 290 | 296 | } |
@@ -296,8 +302,8 @@ discard block |
||
| 296 | 302 | * @param string $pattern The URL schema. |
| 297 | 303 | * @return string The compiled PREG RegEx. |
| 298 | 304 | */ |
| 299 | - protected static function compilePatternAsRegex($pattern, $rules=[]){ |
|
| 300 | - return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules){ |
|
| 305 | + protected static function compilePatternAsRegex($pattern, $rules=[]) { |
|
| 306 | + return '#^'.preg_replace_callback('#:([a-zA-Z]\w*)#S',function($g) use (&$rules) { |
|
| 301 | 307 | return '(?<' . $g[1] . '>' . (isset($rules[$g[1]])?$rules[$g[1]]:'[^/]+') .')'; |
| 302 | 308 | },str_replace(['.',')','*'],['\.',')?','.+'],$pattern)).'$#'; |
| 303 | 309 | } |
@@ -309,12 +315,16 @@ discard block |
||
| 309 | 315 | * @param boolean $cut If true don't limit the matching to the whole URL (used for group pattern extraction) |
| 310 | 316 | * @return array The extracted variables |
| 311 | 317 | */ |
| 312 | - protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false){ |
|
| 318 | + protected static function extractVariablesFromURL($pattern, $URL=null, $cut=false) { |
|
| 313 | 319 | $URL = $URL ?: Request::URI(); |
| 314 | 320 | $pattern = $cut ? str_replace('$#','',$pattern).'#' : $pattern; |
| 315 | - if ( !preg_match($pattern,$URL,$args) ) return false; |
|
| 321 | + if ( !preg_match($pattern,$URL,$args) ) { |
|
| 322 | + return false; |
|
| 323 | + } |
|
| 316 | 324 | foreach ($args as $key => $value) { |
| 317 | - if (false === is_string($key)) unset($args[$key]); |
|
| 325 | + if (false === is_string($key)) { |
|
| 326 | + unset($args[$key]); |
|
| 327 | + } |
|
| 318 | 328 | } |
| 319 | 329 | return $args; |
| 320 | 330 | } |
@@ -324,7 +334,7 @@ discard block |
||
| 324 | 334 | * @param string $pattern The URL schema. |
| 325 | 335 | * @return boolean |
| 326 | 336 | */ |
| 327 | - protected static function isDynamic($pattern){ |
|
| 337 | + protected static function isDynamic($pattern) { |
|
| 328 | 338 | return strlen($pattern) != strcspn($pattern,':(?[*+'); |
| 329 | 339 | } |
| 330 | 340 | |
@@ -333,8 +343,10 @@ discard block |
||
| 333 | 343 | * @param Route $r |
| 334 | 344 | * @return Route |
| 335 | 345 | */ |
| 336 | - public static function add($r){ |
|
| 337 | - if ( isset(static::$group[0]) ) static::$group[0]->add($r); |
|
| 346 | + public static function add($r) { |
|
| 347 | + if ( isset(static::$group[0]) ) { |
|
| 348 | + static::$group[0]->add($r); |
|
| 349 | + } |
|
| 338 | 350 | return static::$routes[implode('',static::$prefix)][] = $r; |
| 339 | 351 | } |
| 340 | 352 | |
@@ -343,41 +355,55 @@ discard block |
||
| 343 | 355 | * @param string $prefix The url prefix for the internal route definitions. |
| 344 | 356 | * @param string $callback This callback is invoked on $prefix match of the current request URI. |
| 345 | 357 | */ |
| 346 | - public static function group($prefix,$callback=null){ |
|
| 358 | + public static function group($prefix,$callback=null) { |
|
| 347 | 359 | |
| 348 | 360 | // Skip definition if current request doesn't match group. |
| 349 | 361 | |
| 350 | 362 | $prefix_complete = rtrim(implode('',static::$prefix),'/') . $prefix; |
| 351 | 363 | |
| 352 | - if ( static::isDynamic($prefix) ){ |
|
| 364 | + if ( static::isDynamic($prefix) ) { |
|
| 353 | 365 | |
| 354 | 366 | // Dynamic group, capture vars |
| 355 | 367 | $vars = static::extractVariablesFromURL(static::compilePatternAsRegex($prefix_complete),null,true); |
| 356 | 368 | |
| 357 | 369 | // Errors in compile pattern or variable extraction, aborting. |
| 358 | - if (false === $vars) return; |
|
| 370 | + if (false === $vars) { |
|
| 371 | + return; |
|
| 372 | + } |
|
| 359 | 373 | |
| 360 | 374 | static::$prefix[] = $prefix; |
| 361 | - if (empty(static::$group)) static::$group = []; |
|
| 375 | + if (empty(static::$group)) { |
|
| 376 | + static::$group = []; |
|
| 377 | + } |
|
| 362 | 378 | array_unshift(static::$group, new RouteGroup()); |
| 363 | - if ($callback) call_user_func_array($callback,$vars); |
|
| 379 | + if ($callback) { |
|
| 380 | + call_user_func_array($callback,$vars); |
|
| 381 | + } |
|
| 364 | 382 | $group = static::$group[0]; |
| 365 | 383 | array_shift(static::$group); |
| 366 | 384 | array_pop(static::$prefix); |
| 367 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
| 385 | + if (empty(static::$prefix)) { |
|
| 386 | + static::$prefix=['']; |
|
| 387 | + } |
|
| 368 | 388 | return $group; |
| 369 | 389 | |
| 370 | - } else if ( 0 === strpos(Request::URI(), $prefix_complete) ){ |
|
| 390 | + } else if ( 0 === strpos(Request::URI(), $prefix_complete) ) { |
|
| 371 | 391 | |
| 372 | 392 | // Static group |
| 373 | 393 | static::$prefix[] = $prefix; |
| 374 | - if (empty(static::$group)) static::$group = []; |
|
| 394 | + if (empty(static::$group)) { |
|
| 395 | + static::$group = []; |
|
| 396 | + } |
|
| 375 | 397 | array_unshift(static::$group, new RouteGroup()); |
| 376 | - if ($callback) call_user_func($callback); |
|
| 398 | + if ($callback) { |
|
| 399 | + call_user_func($callback); |
|
| 400 | + } |
|
| 377 | 401 | $group = static::$group[0]; |
| 378 | 402 | array_shift(static::$group); |
| 379 | 403 | array_pop(static::$prefix); |
| 380 | - if (empty(static::$prefix)) static::$prefix=['']; |
|
| 404 | + if (empty(static::$prefix)) { |
|
| 405 | + static::$prefix=['']; |
|
| 406 | + } |
|
| 381 | 407 | return $group; |
| 382 | 408 | } else { |
| 383 | 409 | |
@@ -387,7 +413,7 @@ discard block |
||
| 387 | 413 | |
| 388 | 414 | } |
| 389 | 415 | |
| 390 | - public static function exitWithError($code,$message="Application Error"){ |
|
| 416 | + public static function exitWithError($code,$message="Application Error") { |
|
| 391 | 417 | Response::error($code,$message); |
| 392 | 418 | Response::send(); |
| 393 | 419 | exit; |
@@ -398,15 +424,19 @@ discard block |
||
| 398 | 424 | * @param string $URL The URL to match onto. |
| 399 | 425 | * @return boolean true if a route callback was executed. |
| 400 | 426 | */ |
| 401 | - public static function dispatch($URL=null,$method=null){ |
|
| 402 | - if (!$URL) $URL = Request::URI(); |
|
| 403 | - if (!$method) $method = Request::method(); |
|
| 427 | + public static function dispatch($URL=null,$method=null) { |
|
| 428 | + if (!$URL) { |
|
| 429 | + $URL = Request::URI(); |
|
| 430 | + } |
|
| 431 | + if (!$method) { |
|
| 432 | + $method = Request::method(); |
|
| 433 | + } |
|
| 404 | 434 | |
| 405 | 435 | $__deferred_send = new Deferred('Response::send'); |
| 406 | 436 | |
| 407 | - foreach ((array)static::$routes as $group => $routes){ |
|
| 437 | + foreach ((array)static::$routes as $group => $routes) { |
|
| 408 | 438 | foreach ($routes as $route) { |
| 409 | - if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))){ |
|
| 439 | + if (is_a($route, 'Route') && false !== ($args = $route->match($URL,$method))) { |
|
| 410 | 440 | $route->run($args,$method); |
| 411 | 441 | return true; |
| 412 | 442 | } |
@@ -421,34 +451,36 @@ discard block |
||
| 421 | 451 | class RouteGroup { |
| 422 | 452 | protected $routes; |
| 423 | 453 | |
| 424 | - public function __construct(){ |
|
| 454 | + public function __construct() { |
|
| 425 | 455 | $this->routes = new SplObjectStorage; |
| 426 | 456 | return Route::add($this); |
| 427 | 457 | } |
| 428 | 458 | |
| 429 | - public function has($r){ |
|
| 459 | + public function has($r) { |
|
| 430 | 460 | return $this->routes->contains($r); |
| 431 | 461 | } |
| 432 | 462 | |
| 433 | - public function add($r){ |
|
| 463 | + public function add($r) { |
|
| 434 | 464 | $this->routes->attach($r); |
| 435 | 465 | return $this; |
| 436 | 466 | } |
| 437 | 467 | |
| 438 | - public function remove($r){ |
|
| 439 | - if ($this->routes->contains($r)) $this->routes->detach($r); |
|
| 468 | + public function remove($r) { |
|
| 469 | + if ($this->routes->contains($r)) { |
|
| 470 | + $this->routes->detach($r); |
|
| 471 | + } |
|
| 440 | 472 | return $this; |
| 441 | 473 | } |
| 442 | 474 | |
| 443 | - public function before($callbacks){ |
|
| 444 | - foreach ($this->routes as $route){ |
|
| 475 | + public function before($callbacks) { |
|
| 476 | + foreach ($this->routes as $route) { |
|
| 445 | 477 | $route->before($callbacks); |
| 446 | 478 | } |
| 447 | 479 | return $this; |
| 448 | 480 | } |
| 449 | 481 | |
| 450 | - public function after($callbacks){ |
|
| 451 | - foreach ($this->routes as $route){ |
|
| 482 | + public function after($callbacks) { |
|
| 483 | + foreach ($this->routes as $route) { |
|
| 452 | 484 | $route->after($callbacks); |
| 453 | 485 | } |
| 454 | 486 | return $this; |
@@ -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 | } |
@@ -32,12 +32,12 @@ discard block |
||
| 32 | 32 | $sent = false; |
| 33 | 33 | |
| 34 | 34 | |
| 35 | - public static function charset($charset){ |
|
| 35 | + public static function charset($charset) { |
|
| 36 | 36 | static::$charset = $charset; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - public static function type($mime){ |
|
| 40 | - static::header('Content-Type',$mime . (static::$charset ? '; charset='.static::$charset : '')); |
|
| 39 | + public static function type($mime) { |
|
| 40 | + static::header('Content-Type', $mime.(static::$charset ? '; charset='.static::$charset : '')); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
@@ -45,39 +45,39 @@ discard block |
||
| 45 | 45 | * @param string/bool $filename Pass a falsy value to disable download or pass a filename for exporting content |
| 46 | 46 | * @return [type] [description] |
| 47 | 47 | */ |
| 48 | - public static function download($filename){ |
|
| 48 | + public static function download($filename) { |
|
| 49 | 49 | static::$force_dl = $filename; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * Start capturing output |
| 54 | 54 | */ |
| 55 | - public static function start(){ |
|
| 55 | + public static function start() { |
|
| 56 | 56 | static::$buffer = ob_start(); |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
| 60 | 60 | * Enable CORS HTTP headers. |
| 61 | 61 | */ |
| 62 | - public static function enableCORS(){ |
|
| 62 | + public static function enableCORS() { |
|
| 63 | 63 | |
| 64 | 64 | // Allow from any origin |
| 65 | - if ($origin = filter_input(INPUT_SERVER,'HTTP_ORIGIN')) { |
|
| 65 | + if ($origin = filter_input(INPUT_SERVER, 'HTTP_ORIGIN')) { |
|
| 66 | 66 | static::header('Access-Control-Allow-Origin', $origin); |
| 67 | 67 | static::header('Access-Control-Allow-Credentials', 'true'); |
| 68 | 68 | static::header('Access-Control-Max-Age', 86400); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | // Access-Control headers are received during OPTIONS requests |
| 72 | - if (filter_input(INPUT_SERVER,'REQUEST_METHOD') == 'OPTIONS') { |
|
| 72 | + if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') == 'OPTIONS') { |
|
| 73 | 73 | static::clean(); |
| 74 | 74 | |
| 75 | - if (filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_METHOD')) { |
|
| 75 | + if (filter_input(INPUT_SERVER, 'HTTP_ACCESS_CONTROL_REQUEST_METHOD')) { |
|
| 76 | 76 | static::header('Access-Control-Allow-Methods', |
| 77 | 77 | 'GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, PATCH, TRACE'); |
| 78 | 78 | } |
| 79 | - if ($req_h = filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_HEADERS')) { |
|
| 80 | - static::header('Access-Control-Allow-Headers',$req_h); |
|
| 79 | + if ($req_h = filter_input(INPUT_SERVER, 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS')) { |
|
| 80 | + static::header('Access-Control-Allow-Headers', $req_h); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | static::send(); |
@@ -93,8 +93,8 @@ discard block |
||
| 93 | 93 | * Finish the output buffer capturing. |
| 94 | 94 | * @return string The captured buffer |
| 95 | 95 | */ |
| 96 | - public static function end(){ |
|
| 97 | - if (static::$buffer){ |
|
| 96 | + public static function end() { |
|
| 97 | + if (static::$buffer) { |
|
| 98 | 98 | static::$payload[] = ob_get_contents(); |
| 99 | 99 | ob_end_clean(); |
| 100 | 100 | static::$buffer = null; |
@@ -106,14 +106,14 @@ discard block |
||
| 106 | 106 | * Check if an response output buffering is active. |
| 107 | 107 | * @return boolean |
| 108 | 108 | */ |
| 109 | - public static function isBuffering(){ |
|
| 109 | + public static function isBuffering() { |
|
| 110 | 110 | return static::$buffer; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | /** |
| 114 | 114 | * Clear the response body |
| 115 | 115 | */ |
| 116 | - public static function clean(){ |
|
| 116 | + public static function clean() { |
|
| 117 | 117 | static::$payload = []; |
| 118 | 118 | } |
| 119 | 119 | |
@@ -121,74 +121,74 @@ discard block |
||
| 121 | 121 | * Append a JSON object to the buffer. |
| 122 | 122 | * @param mixed $payload Data to append to the response buffer |
| 123 | 123 | */ |
| 124 | - public static function json($payload){ |
|
| 124 | + public static function json($payload) { |
|
| 125 | 125 | static::type(static::TYPE_JSON); |
| 126 | - static::$payload[] = json_encode($payload, Options::get('core.response.json_flags',JSON_NUMERIC_CHECK)); |
|
| 126 | + static::$payload[] = json_encode($payload, Options::get('core.response.json_flags', JSON_NUMERIC_CHECK)); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | /** |
| 130 | 130 | * Append a text to the buffer. |
| 131 | 131 | * @param mixed $payload Text to append to the response buffer |
| 132 | 132 | */ |
| 133 | - public static function text(){ |
|
| 133 | + public static function text() { |
|
| 134 | 134 | static::type(static::TYPE_TEXT); |
| 135 | - static::$payload[] = implode('',func_get_args()); |
|
| 135 | + static::$payload[] = implode('', func_get_args()); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /** |
| 139 | 139 | * Append an XML string to the buffer. |
| 140 | 140 | * @param mixed $payload Data to append to the response buffer |
| 141 | 141 | */ |
| 142 | - public static function xml(){ |
|
| 142 | + public static function xml() { |
|
| 143 | 143 | static::type(static::TYPE_XML); |
| 144 | - static::$payload[] = implode('',func_get_args()); |
|
| 144 | + static::$payload[] = implode('', func_get_args()); |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
| 148 | 148 | * Append a SVG string to the buffer. |
| 149 | 149 | * @param mixed $payload Data to append to the response buffer |
| 150 | 150 | */ |
| 151 | - public static function svg(){ |
|
| 151 | + public static function svg() { |
|
| 152 | 152 | static::type(static::TYPE_SVG); |
| 153 | - static::$payload[] = implode('',func_get_args()); |
|
| 153 | + static::$payload[] = implode('', func_get_args()); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | /** |
| 157 | 157 | * Append an HTML string to the buffer. |
| 158 | 158 | * @param mixed $payload Data to append to the response buffer |
| 159 | 159 | */ |
| 160 | - public static function html(){ |
|
| 160 | + public static function html() { |
|
| 161 | 161 | static::type(static::TYPE_HTML); |
| 162 | - static::$payload[] = implode('',func_get_args()); |
|
| 162 | + static::$payload[] = implode('', func_get_args()); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** |
| 166 | 166 | * Append a raw string to the buffer. |
| 167 | 167 | * @param mixed $payload Data to append to the response buffer |
| 168 | 168 | */ |
| 169 | - public static function add(){ |
|
| 170 | - static::$payload[] = implode('',func_get_args()); |
|
| 169 | + public static function add() { |
|
| 170 | + static::$payload[] = implode('', func_get_args()); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - public static function status($code,$message=''){ |
|
| 174 | - static::header('Status',$message?:$code,$code); |
|
| 173 | + public static function status($code, $message = '') { |
|
| 174 | + static::header('Status', $message ?: $code, $code); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | - public static function header($name,$value,$code=null){ |
|
| 178 | - static::$headers[$name] = [$value,$code]; |
|
| 177 | + public static function header($name, $value, $code = null) { |
|
| 178 | + static::$headers[$name] = [$value, $code]; |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - public static function error($code=500,$message='Application Error'){ |
|
| 182 | - Event::trigger('core.response.error',$code,$message); |
|
| 183 | - static::status($code,$message); |
|
| 181 | + public static function error($code = 500, $message = 'Application Error') { |
|
| 182 | + Event::trigger('core.response.error', $code, $message); |
|
| 183 | + static::status($code, $message); |
|
| 184 | 184 | } |
| 185 | 185 | |
| 186 | - public static function body($setBody=null){ |
|
| 186 | + public static function body($setBody = null) { |
|
| 187 | 187 | if ($setBody) static::$payload = [$setBody]; |
| 188 | - return Filter::with('core.response.body',is_array(static::$payload)?implode('',static::$payload):static::$payload); |
|
| 188 | + return Filter::with('core.response.body', is_array(static::$payload) ? implode('', static::$payload) : static::$payload); |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | - public static function headers($setHeaders=null){ |
|
| 191 | + public static function headers($setHeaders = null) { |
|
| 192 | 192 | if ($setHeaders) static::$headers = $setHeaders; |
| 193 | 193 | return static::$headers; |
| 194 | 194 | } |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | * |
| 201 | 201 | * @return array Headers and body of the response |
| 202 | 202 | */ |
| 203 | - public static function save(){ |
|
| 203 | + public static function save() { |
|
| 204 | 204 | return [ |
| 205 | 205 | 'head' => static::$headers, |
| 206 | 206 | 'body' => static::body(), |
@@ -214,13 +214,13 @@ discard block |
||
| 214 | 214 | * |
| 215 | 215 | * @param array $data head/body saved state |
| 216 | 216 | */ |
| 217 | - public static function load($data){ |
|
| 218 | - $data = (object)$data; |
|
| 217 | + public static function load($data) { |
|
| 218 | + $data = (object) $data; |
|
| 219 | 219 | if (isset($data->head)) static::headers($data->head); |
| 220 | 220 | if (isset($data->body)) static::body($data->body); |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - public static function send($force = false){ |
|
| 223 | + public static function send($force = false) { |
|
| 224 | 224 | if (!static::$sent || $force) { |
| 225 | 225 | static::$sent = true; |
| 226 | 226 | Event::trigger('core.response.send'); |
@@ -233,8 +233,8 @@ discard block |
||
| 233 | 233 | $code = null; |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | - if ($value == 'Status'){ |
|
| 237 | - if (function_exists('http_response_code')){ |
|
| 236 | + if ($value == 'Status') { |
|
| 237 | + if (function_exists('http_response_code')) { |
|
| 238 | 238 | http_response_code($code); |
| 239 | 239 | } else { |
| 240 | 240 | header("Status: $code", true, $code); |
@@ -32,11 +32,11 @@ discard block |
||
| 32 | 32 | $sent = false; |
| 33 | 33 | |
| 34 | 34 | |
| 35 | - public static function charset($charset){ |
|
| 35 | + public static function charset($charset) { |
|
| 36 | 36 | static::$charset = $charset; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - public static function type($mime){ |
|
| 39 | + public static function type($mime) { |
|
| 40 | 40 | static::header('Content-Type',$mime . (static::$charset ? '; charset='.static::$charset : '')); |
| 41 | 41 | } |
| 42 | 42 | |
@@ -45,21 +45,21 @@ discard block |
||
| 45 | 45 | * @param string/bool $filename Pass a falsy value to disable download or pass a filename for exporting content |
| 46 | 46 | * @return [type] [description] |
| 47 | 47 | */ |
| 48 | - public static function download($filename){ |
|
| 48 | + public static function download($filename) { |
|
| 49 | 49 | static::$force_dl = $filename; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * Start capturing output |
| 54 | 54 | */ |
| 55 | - public static function start(){ |
|
| 55 | + public static function start() { |
|
| 56 | 56 | static::$buffer = ob_start(); |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
| 60 | 60 | * Enable CORS HTTP headers. |
| 61 | 61 | */ |
| 62 | - public static function enableCORS(){ |
|
| 62 | + public static function enableCORS() { |
|
| 63 | 63 | |
| 64 | 64 | // Allow from any origin |
| 65 | 65 | if ($origin = filter_input(INPUT_SERVER,'HTTP_ORIGIN')) { |
@@ -93,8 +93,8 @@ discard block |
||
| 93 | 93 | * Finish the output buffer capturing. |
| 94 | 94 | * @return string The captured buffer |
| 95 | 95 | */ |
| 96 | - public static function end(){ |
|
| 97 | - if (static::$buffer){ |
|
| 96 | + public static function end() { |
|
| 97 | + if (static::$buffer) { |
|
| 98 | 98 | static::$payload[] = ob_get_contents(); |
| 99 | 99 | ob_end_clean(); |
| 100 | 100 | static::$buffer = null; |
@@ -106,14 +106,14 @@ discard block |
||
| 106 | 106 | * Check if an response output buffering is active. |
| 107 | 107 | * @return boolean |
| 108 | 108 | */ |
| 109 | - public static function isBuffering(){ |
|
| 109 | + public static function isBuffering() { |
|
| 110 | 110 | return static::$buffer; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | /** |
| 114 | 114 | * Clear the response body |
| 115 | 115 | */ |
| 116 | - public static function clean(){ |
|
| 116 | + public static function clean() { |
|
| 117 | 117 | static::$payload = []; |
| 118 | 118 | } |
| 119 | 119 | |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | * Append a JSON object to the buffer. |
| 122 | 122 | * @param mixed $payload Data to append to the response buffer |
| 123 | 123 | */ |
| 124 | - public static function json($payload){ |
|
| 124 | + public static function json($payload) { |
|
| 125 | 125 | static::type(static::TYPE_JSON); |
| 126 | 126 | static::$payload[] = json_encode($payload, Options::get('core.response.json_flags',JSON_NUMERIC_CHECK)); |
| 127 | 127 | } |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | * Append a text to the buffer. |
| 131 | 131 | * @param mixed $payload Text to append to the response buffer |
| 132 | 132 | */ |
| 133 | - public static function text(){ |
|
| 133 | + public static function text() { |
|
| 134 | 134 | static::type(static::TYPE_TEXT); |
| 135 | 135 | static::$payload[] = implode('',func_get_args()); |
| 136 | 136 | } |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | * Append an XML string to the buffer. |
| 140 | 140 | * @param mixed $payload Data to append to the response buffer |
| 141 | 141 | */ |
| 142 | - public static function xml(){ |
|
| 142 | + public static function xml() { |
|
| 143 | 143 | static::type(static::TYPE_XML); |
| 144 | 144 | static::$payload[] = implode('',func_get_args()); |
| 145 | 145 | } |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | * Append a SVG string to the buffer. |
| 149 | 149 | * @param mixed $payload Data to append to the response buffer |
| 150 | 150 | */ |
| 151 | - public static function svg(){ |
|
| 151 | + public static function svg() { |
|
| 152 | 152 | static::type(static::TYPE_SVG); |
| 153 | 153 | static::$payload[] = implode('',func_get_args()); |
| 154 | 154 | } |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | * Append an HTML string to the buffer. |
| 158 | 158 | * @param mixed $payload Data to append to the response buffer |
| 159 | 159 | */ |
| 160 | - public static function html(){ |
|
| 160 | + public static function html() { |
|
| 161 | 161 | static::type(static::TYPE_HTML); |
| 162 | 162 | static::$payload[] = implode('',func_get_args()); |
| 163 | 163 | } |
@@ -166,30 +166,34 @@ discard block |
||
| 166 | 166 | * Append a raw string to the buffer. |
| 167 | 167 | * @param mixed $payload Data to append to the response buffer |
| 168 | 168 | */ |
| 169 | - public static function add(){ |
|
| 169 | + public static function add() { |
|
| 170 | 170 | static::$payload[] = implode('',func_get_args()); |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - public static function status($code,$message=''){ |
|
| 173 | + public static function status($code,$message='') { |
|
| 174 | 174 | static::header('Status',$message?:$code,$code); |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | - public static function header($name,$value,$code=null){ |
|
| 177 | + public static function header($name,$value,$code=null) { |
|
| 178 | 178 | static::$headers[$name] = [$value,$code]; |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - public static function error($code=500,$message='Application Error'){ |
|
| 181 | + public static function error($code=500,$message='Application Error') { |
|
| 182 | 182 | Event::trigger('core.response.error',$code,$message); |
| 183 | 183 | static::status($code,$message); |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | - public static function body($setBody=null){ |
|
| 187 | - if ($setBody) static::$payload = [$setBody]; |
|
| 186 | + public static function body($setBody=null) { |
|
| 187 | + if ($setBody) { |
|
| 188 | + static::$payload = [$setBody]; |
|
| 189 | + } |
|
| 188 | 190 | return Filter::with('core.response.body',is_array(static::$payload)?implode('',static::$payload):static::$payload); |
| 189 | 191 | } |
| 190 | 192 | |
| 191 | - public static function headers($setHeaders=null){ |
|
| 192 | - if ($setHeaders) static::$headers = $setHeaders; |
|
| 193 | + public static function headers($setHeaders=null) { |
|
| 194 | + if ($setHeaders) { |
|
| 195 | + static::$headers = $setHeaders; |
|
| 196 | + } |
|
| 193 | 197 | return static::$headers; |
| 194 | 198 | } |
| 195 | 199 | |
@@ -200,7 +204,7 @@ discard block |
||
| 200 | 204 | * |
| 201 | 205 | * @return array Headers and body of the response |
| 202 | 206 | */ |
| 203 | - public static function save(){ |
|
| 207 | + public static function save() { |
|
| 204 | 208 | return [ |
| 205 | 209 | 'head' => static::$headers, |
| 206 | 210 | 'body' => static::body(), |
@@ -214,27 +218,33 @@ discard block |
||
| 214 | 218 | * |
| 215 | 219 | * @param array $data head/body saved state |
| 216 | 220 | */ |
| 217 | - public static function load($data){ |
|
| 221 | + public static function load($data) { |
|
| 218 | 222 | $data = (object)$data; |
| 219 | - if (isset($data->head)) static::headers($data->head); |
|
| 220 | - if (isset($data->body)) static::body($data->body); |
|
| 223 | + if (isset($data->head)) { |
|
| 224 | + static::headers($data->head); |
|
| 225 | + } |
|
| 226 | + if (isset($data->body)) { |
|
| 227 | + static::body($data->body); |
|
| 228 | + } |
|
| 221 | 229 | } |
| 222 | 230 | |
| 223 | - public static function send($force = false){ |
|
| 231 | + public static function send($force = false) { |
|
| 224 | 232 | if (!static::$sent || $force) { |
| 225 | 233 | static::$sent = true; |
| 226 | 234 | Event::trigger('core.response.send'); |
| 227 | - if (false === headers_sent()) foreach (static::$headers as $name => $value_code) { |
|
| 235 | + if (false === headers_sent()) { |
|
| 236 | + foreach (static::$headers as $name => $value_code) { |
|
| 228 | 237 | |
| 229 | 238 | if (is_array($value_code)) { |
| 230 | 239 | list($value, $code) = (count($value_code) > 1) ? $value_code : [current($value_code), 200]; |
| 240 | + } |
|
| 231 | 241 | } else { |
| 232 | 242 | $value = $value_code; |
| 233 | 243 | $code = null; |
| 234 | 244 | } |
| 235 | 245 | |
| 236 | - if ($value == 'Status'){ |
|
| 237 | - if (function_exists('http_response_code')){ |
|
| 246 | + if ($value == 'Status') { |
|
| 247 | + if (function_exists('http_response_code')) { |
|
| 238 | 248 | http_response_code($code); |
| 239 | 249 | } else { |
| 240 | 250 | header("Status: $code", true, $code); |
@@ -246,7 +256,9 @@ discard block |
||
| 246 | 256 | : header("$name: $value", true); |
| 247 | 257 | } |
| 248 | 258 | } |
| 249 | - if (static::$force_dl) header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
|
| 259 | + if (static::$force_dl) { |
|
| 260 | + header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
|
| 261 | + } |
|
| 250 | 262 | echo static::body(); |
| 251 | 263 | } |
| 252 | 264 | } |