From 21640f3fc63e93e2e3918d82d6b585920f0865fb Mon Sep 17 00:00:00 2001 From: AnrDaemon Date: Tue, 10 Oct 2017 14:54:26 +0300 Subject: [PATCH] Fix using $this outside object context. Fix setters chaining. Change base classes' resolution. Use ArrayIterator to store entries. --- CHANGES | 19 ++++++++++++---- Gallery.php | 65 ++++++++++++++++++++++------------------------------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/CHANGES b/CHANGES index 20ef410..476b197 100644 --- a/CHANGES +++ b/CHANGES @@ -1,11 +1,22 @@ ------------------------------------------------------------------------ -r671 | anrdaemon | 2017-06-23 22:49:17 +0300 (Пт, 23 июн 2017) | 2 lines +r684 | anrdaemon | 2017-10-10 07:41:44 +0300 (Вт, 10 окт 2017) | 3 lines -* Tweak thumbnail image dimensions calculation. +- Fix using $this outside object context. +* Change base classes' resolution. ------------------------------------------------------------------------ -r670 | anrdaemon | 2017-06-23 22:38:51 +0300 (Пт, 23 июн 2017) | 2 lines +r674 | anrdaemon | 2017-06-25 02:01:04 +0300 (Вс, 25 июн 2017) | 2 lines -+ GD2 support for thumbnail images. +* ArrayIterator to store entries. + +------------------------------------------------------------------------ +r673 | anrdaemon | 2017-06-25 01:57:12 +0300 (Вс, 25 июн 2017) | 2 lines + +* Reformat initial assignment. + +------------------------------------------------------------------------ +r672 | anrdaemon | 2017-06-24 00:56:23 +0300 (Сб, 24 июн 2017) | 2 lines + +* Fix setters chaining. ------------------------------------------------------------------------ diff --git a/Gallery.php b/Gallery.php index 1d12891..bff74eb 100644 --- a/Gallery.php +++ b/Gallery.php @@ -3,31 +3,20 @@ * * A simple drop-in file-based HTML gallery. * -* $Id: Gallery.php 675 2017-06-24 23:18:10Z anrdaemon $ +* $Id: Gallery.php 684 2017-10-10 04:41:44Z anrdaemon $ */ namespace AnrDaemon\MyLittleGallery; -use - ArrayAccess, - ArrayIterator, - Countable, - Exception, - ErrorException, - Imagick, - Iterator, - NumberFormatter, - SplFileInfo; - class Gallery -implements ArrayAccess, Countable, Iterator +implements \ArrayAccess, \Countable, \Iterator { const previewTemplate = '
%3$s

%3$s

'; const defaultTypes = 'gif|jpeg|jpg|png|wbmp|webp'; - // All paths are UTF-8! (Except those from SplFileInfo) + // All paths are UTF-8! (Except those from \SplFileInfo) protected $path; // Gallery base path protected $prefix = array(); // Various prefixes for correct links construction protected $params = array(); // Files list @@ -63,7 +52,7 @@ protected function fromFileList(array $list) if($meta === false || $meta[0] === 0 || $meta[1] === 0) continue; - $this->params[$name] = new ArrayIterator([ + $this->params[$name] = new \ArrayIterator([ 'desc' => $name, 'path' => "{$this->path}/{$name}", 'width' => $meta[0], @@ -81,15 +70,15 @@ protected function fromFileList(array $list) return $this; } - public static function fromListfile(SplFileInfo $target, $charset = 'CP866', $fsEncoding = null) + public static function fromListfile(\SplFileInfo $target, $charset = 'CP866', $fsEncoding = null) { $path = $target->getRealPath(); if(empty($path)) - throw new Exception('Can\'t use empty path.', 500); + throw new \Exception('Can\'t use empty path.', 500); if(is_dir($path)) - throw new Exception('Target is a directory', 500); + throw new \Exception('Target is a directory', 500); $self = new static($target->getPathInfo(), null, $fsEncoding); @@ -107,7 +96,7 @@ public static function fromListfile(SplFileInfo $target, $charset = 'CP866', $fs if($meta === false || $meta[0] === 0 || $meta[1] === 0) continue; - $this->params[$name] = new ArrayIterator([ + $self->params[$name] = new \ArrayIterator([ 'desc' => $a['desc'], 'path' => "{$self->path}/{$name}", 'width' => $meta[0], @@ -126,7 +115,7 @@ public static function fromListfile(SplFileInfo $target, $charset = 'CP866', $fs return $self; } - public static function fromDirectory(SplFileInfo $target, array $extensions = null, $fsEncoding = null) + public static function fromDirectory(\SplFileInfo $target, array $extensions = null, $fsEncoding = null) { if(empty($extensions)) { @@ -138,15 +127,15 @@ public static function fromDirectory(SplFileInfo $target, array $extensions = nu return static::fromCustomMask($target, $mask, $fsEncoding); } - public static function fromCustomMask(SplFileInfo $target, $mask, $fsEncoding = null) + public static function fromCustomMask(\SplFileInfo $target, $mask, $fsEncoding = null) { $path = $target->getRealPath(); if(empty($path)) - throw new Exception('Can\'t use empty path.', 500); + throw new \Exception('Can\'t use empty path.', 500); if(!is_dir($path)) - throw new Exception('Target is not a directory', 500); + throw new \Exception('Target is not a directory', 500); $self = new static($target, null, $fsEncoding); @@ -173,9 +162,9 @@ public function showIndex($template = null) return $gp; } - public function setNumberFormatter($locale = 'en_US.UTF-8', $style = NumberFormatter::DECIMAL, $pattern = '') + public function setNumberFormatter($locale = 'en_US.UTF-8', $style = \NumberFormatter::DECIMAL, $pattern = '') { - $this->nf = new NumberFormatter($locale, $style, $pattern); + $this->nf = new \NumberFormatter($locale, $style, $pattern); return $this; } @@ -228,7 +217,7 @@ public function imagePreviewExists($name) public function setPreviewSize($width = null, $height = null) { if((int)$width < 0 || (int)$height < 0) - throw new Exception('Thumbnail dimensions can\'t be negative.', 500); + throw new \Exception('Thumbnail dimensions can\'t be negative.', 500); $this->pWidth = (int)$width ?: 160; $this->pHeight = (int)$height ?: 120; @@ -239,7 +228,7 @@ public function setPreviewSize($width = null, $height = null) public function setPrefix($name, $prefix) { if(!isset($this->prefix[$name])) - throw new Exception("Unknown prefix '$name'.", 500); + throw new \Exception("Unknown prefix '$name'.", 500); $this->prefix[$name] = $prefix; @@ -292,7 +281,7 @@ public function thumbnailImage($name) ); if(!isset($this->params[$name])) - throw new Exception("Image '$name' is not registered in the gallery.", 404); + throw new \Exception("Image '$name' is not registered in the gallery.", 404); $path = $this->getPath("/.preview/$name", true); if(is_file($path)) @@ -300,11 +289,11 @@ public function thumbnailImage($name) try { - set_error_handler(function($s, $m, $f, $l, $c = null) { throw new ErrorException($m, 0, $s, $f, $l); }); + set_error_handler(function($s, $m, $f, $l, $c = null) { throw new \ErrorException($m, 0, $s, $f, $l); }); if(class_exists('Imagick')) { - $img = new Imagick("{$this->path}/$name"); + $img = new \Imagick("{$this->path}/$name"); $img->thumbnailImage($this->pWidth, $this->pHeight, true); $img->writeImage("{$this->path}/.preview/$name"); } @@ -312,7 +301,7 @@ public function thumbnailImage($name) { $src = imagecreatefromstring(file_get_contents($this->getPath("/$name", true))); if($src === false) - throw new Exception("The file '$name' can't be interpreted as image.", 500); + throw new \Exception("The file '$name' can't be interpreted as image.", 500); $oFactor = $this->params[$name]['width'] / $this->params[$name]['height']; $tFactor = $this->pWidth / $this->pHeight; @@ -329,16 +318,16 @@ public function thumbnailImage($name) $img = imagecreatetruecolor($w, $h); if(!imagecopyresampled($img, $src, 0, 0, 0, 0, $w, $h, $this->params[$name]['width'], $this->params[$name]['height'])) - throw new Exception("Unable to create thumbnail for '$name'.", 500); + throw new \Exception("Unable to create thumbnail for '$name'.", 500); $gdSave[$this->params[$name]['mime']]($img, $path); } else - throw new ErrorException('Imagick or gd2 extension is required to create thumbnails at runtime.', 501); + throw new \ErrorException('Imagick or gd2 extension is required to create thumbnails at runtime.', 501); restore_error_handler(); } - catch(Exception $e) + catch(\Exception $e) { restore_error_handler(); if(!is_dir(dirname($path))) @@ -358,17 +347,17 @@ public function isSaneName($fname) { $name = basename($fname); if(preg_match('/[^!#$%&\'()+,\-.;=@\[\]^_`{}~\p{L}\d\s]/uiS', $name)) - throw new Exception("Invalid character in name '$name'.", 400); + throw new \Exception("Invalid character in name '$name'.", 400); if(!preg_match('{.+\.(' . implode('|', array_map('preg_quote', $this->extensions)) . ')$}ui', $name)) - throw new Exception('Invalid filename extension.', 400); + throw new \Exception('Invalid filename extension.', 400); return true; } // Magic! - protected function __construct(SplFileInfo $path, array $extensions = null, $fsEncoding = null) + protected function __construct(\SplFileInfo $path, array $extensions = null, $fsEncoding = null) { if(version_compare(PHP_VERSION, '7.1', '<')) { @@ -409,7 +398,7 @@ protected function __construct(SplFileInfo $path, array $extensions = null, $fsE } if(empty($masks)) - throw new Exception('File extensions can\'t be empty strings.', 500); + throw new \Exception('File extensions can\'t be empty strings.', 500); $this->extensions = $masks; }