-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
544 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/vendor/ | ||
/images/ | ||
/bin/ | ||
index.php | ||
composer.lock | ||
composer.phar | ||
.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "dumkaaa/bxoptimize", | ||
"description": "", | ||
"type": "library", | ||
"license": "MIT", | ||
"minimum-stability": "dev", | ||
"require": { | ||
"php": ">=5.5.9", | ||
"symfony/console": "^3.2" | ||
}, | ||
"require-dev": { | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Dumkaaa\\BxOptimize\\": "lib/" | ||
} | ||
}, | ||
"scripts": { | ||
"bxoptimize-scripts": [ | ||
"Dumkaaa\\BxOptimize\\Install\\Installer::getBinaries" | ||
], | ||
"post-install-cmd": [ | ||
"@bxoptimize-scripts" | ||
], | ||
"post-update-cmd": [ | ||
"@bxoptimize-scripts" | ||
] | ||
}, | ||
"bin": [ | ||
"bin/bxoptimize" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
|
||
namespace Dumkaaa\BxOptimize\Finder; | ||
|
||
|
||
use Exception; | ||
|
||
class FilesFinder implements FinderInterface | ||
{ | ||
/** @var string Путь к папке, в которой будем искать файлы */ | ||
protected $path; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct($path) | ||
{ | ||
$path = trim($path, '\/'); | ||
if (empty($path) || !(is_dir($path) || is_file($path)) || !is_writable($path)) { | ||
throw new Exception('Путь не может быть пустым или нет доступа'); | ||
} | ||
$this->path = $path; | ||
|
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function findFiles($path = null) | ||
{ | ||
$path = $path ?: $this->path; | ||
$basePath = $_SERVER['DOCUMENT_ROOT']; | ||
|
||
return new \RecursiveIteratorIterator( | ||
new \RecursiveDirectoryIterator($basePath . '/' . $path) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
|
||
namespace Dumkaaa\BxOptimize\Finder; | ||
|
||
|
||
interface FinderInterface | ||
{ | ||
/** | ||
* FilesFinder constructor. | ||
* @param string $path Путь к папке, в которой будем искать файлы | ||
* @throws \Exception | ||
*/ | ||
public function __construct($path); | ||
|
||
/** | ||
* Ищет файлы в указанной папке, включая вложенные | ||
* @param string $path | ||
* @return \RecursiveIteratorIterator | ||
*/ | ||
public function findFiles($path = null); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
|
||
namespace Dumkaaa\BxOptimize\Handler; | ||
|
||
|
||
class CssHandler extends Handler | ||
{ | ||
/** {@inheritdoc} */ | ||
protected $validExt = [ | ||
'css', | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleQueue() | ||
{ | ||
foreach ($this->files as $file) { | ||
print(get_class() . " обрабатывает файл: $file\n"); | ||
} | ||
// TODO: Implement handleQueue() method. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
|
||
namespace Dumkaaa\BxOptimize\Handler; | ||
|
||
|
||
abstract class Handler implements HandlerInterface | ||
{ | ||
/** @var array Допустимые этим обработчиком разрешения файлов */ | ||
protected $validExt = []; | ||
|
||
/** @var array Очередь файлов на обработку */ | ||
protected $files = []; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function canHandleFile($file = null) | ||
{ | ||
if(!$file) { | ||
throw new \Exception("В обработчик " . get_class() . " передано неверное имя файла: $file"); | ||
} | ||
|
||
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); | ||
|
||
return in_array($ext, $this->validExt); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function queueFile($file) | ||
{ | ||
$this->files[] = $file; | ||
} | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleQueue() | ||
{ | ||
foreach ($this->files as $file) { | ||
print(get_class() . " обрабатывает файл: $file\n"); | ||
} | ||
// TODO: Implement handleQueue() method. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
|
||
namespace Dumkaaa\BxOptimize\Handler; | ||
|
||
|
||
interface HandlerInterface | ||
{ | ||
/** | ||
* Проверяет, можно ли данный обработчик обработать этот файл | ||
* @param string $file Путь к файлу | ||
* @return bool | ||
* @throws \Exception | ||
*/ | ||
public function canHandleFile($file); | ||
|
||
/** | ||
* Записывает файл в очередь на обработку | ||
* @param string $file Путь к файлу | ||
* @return void | ||
*/ | ||
function queueFile($file); | ||
|
||
/** | ||
* Обрабатывает файлы в очереди | ||
* @return mixed | ||
*/ | ||
public function handleQueue(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
|
||
namespace Dumkaaa\BxOptimize\Handler; | ||
|
||
|
||
class HandlerProcessor | ||
{ | ||
/** @var array Стандартные обработчики */ | ||
private $defaultHandlers = [ | ||
'image' => __NAMESPACE__ . '\ImageHandler', | ||
//'css' => __NAMESPACE__ . '\CssHandler', | ||
]; | ||
|
||
/** @var array Включенные обработчики */ | ||
protected $handlers = []; | ||
|
||
|
||
/** | ||
* Включаем либо обработчики из переданного набора, либо все стандартные | ||
* @param array $handlers | ||
*/ | ||
public function __construct(array $handlers) | ||
{ | ||
is_array($handlers) | ||
? $this->enableHandlers($handlers) | ||
: $this->enableHandlers($this->defaultHandlers); | ||
} | ||
|
||
/** | ||
* Включает обработчики из массива | ||
* @param array $handlers | ||
*/ | ||
public function enableHandlers(array $handlers) | ||
{ | ||
//убираем возможные дубликаты | ||
$handlers = array_unique(array_map('strtolower', $handlers)); | ||
|
||
//включаем | ||
foreach ($handlers as $handler) { | ||
$this->enableHandler($handler); | ||
} | ||
} | ||
|
||
/** | ||
* Включает обработчик из списка доступных обработчиков | ||
* @param string $key Ключ-имя обработчика | ||
* @throws \Exception | ||
*/ | ||
public function enableHandler($key) | ||
{ | ||
if(!$key) { | ||
throw new \Exception("Не передан ключ обработчика"); | ||
} else { | ||
$key = strtolower(trim($key)); | ||
} | ||
|
||
if(isset($this->defaultHandlers[$key])) { | ||
$this->handlers[$key] = new $this->defaultHandlers[$key]; | ||
} else { | ||
throw new \Exception("Обработчик $key не найден"); | ||
} | ||
} | ||
|
||
/** | ||
* Возврашает включенные обработчики | ||
* @return array | ||
*/ | ||
public function getHandlers() | ||
{ | ||
return $this->handlers; | ||
} | ||
|
||
|
||
/** | ||
* Добавляет новый обработчик и сразу включает его | ||
* @param string $key Ключ-имя обработчика | ||
* @param string $classname Класс обработчика | ||
* @param bool $replace Заменять, если уже есть такой обработчик | ||
* @return bool | ||
*/ | ||
public function addHandler($key, $classname, $replace = false) | ||
{ | ||
$return = false; | ||
|
||
if(!isset($this->handlers[$key]) || $replace) { | ||
$this->handlers[$key] = new $classname; | ||
$return = true; | ||
} | ||
|
||
return $return; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
|
||
namespace Dumkaaa\BxOptimize\Handler; | ||
|
||
|
||
class ImageHandler extends Handler | ||
{ | ||
/** {@inheritdoc} */ | ||
protected $validExt = [ | ||
'png', | ||
'jpeg', | ||
'jpg' | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleQueue() | ||
{ | ||
foreach ($this->files as $file) { | ||
print(get_class() . " обрабатывает файл: $file\n"); | ||
} | ||
// TODO: Implement handleQueue() method. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
|
||
namespace Dumkaaa\BxOptimize\Install; | ||
|
||
use Composer\Script\Event; | ||
|
||
|
||
class Installer | ||
{ | ||
protected static $tools = [ | ||
"gif" => "gifsicle", | ||
"png" => "optipng", | ||
"jpg" => "jpegtran", | ||
"pngquant" => "pngquant", | ||
//"webp" => "cwebp", | ||
]; | ||
|
||
protected static $extByPlatforms = [ | ||
//"win" => ".exe", | ||
"linux" => "-linux", | ||
//"freebsd" => "-fbsd", | ||
//"darwin" => "-mac", | ||
//"sunos" => "-sol", | ||
]; | ||
|
||
|
||
/** | ||
* Скачивает бинарники и кладёт их в папку bin/ | ||
* @param Event $event | ||
*/ | ||
public static function getBinaries(Event $event) | ||
{ | ||
$dir = self::getRootPath().'/bin'; | ||
|
||
if(!file_exists($dir)) | ||
{ | ||
mkdir($dir); | ||
} | ||
|
||
$event->getIO()->write("<info>Setup binaries to $dir :</info>"); | ||
|
||
foreach (self::$extByPlatforms as $ext) { | ||
foreach (self::$tools as $tool) { | ||
file_put_contents( | ||
$dir . '/' . $tool, | ||
fopen('https://github.com/nosilver4u/ewww-image-optimizer/raw/master/binaries/' . $tool . $ext, 'r') | ||
); | ||
$event->getIO()->write($tool . $ext); | ||
} | ||
} | ||
|
||
$event->getIO()->write('<info>Setup binaries complete.</info>'); | ||
} | ||
|
||
public static function getRootPath() | ||
{ | ||
return dirname(__DIR__); | ||
} | ||
} |
Oops, something went wrong.