00f100/phpuphar
Advanced tools
| bin/* | ||
| vendor/* | ||
| composer.lock |
| { | ||
| "name": "00f100/phpuphar", | ||
| "type": "library", | ||
| "version": "0.7.0", | ||
| "description": "Library to self update your phar file", | ||
| "keywords": ["self", "update", "phar", "php"], | ||
| "homepage": "https://github.com/00f100/phpuphar", | ||
| "authors": [ | ||
| { | ||
| "name": "João Moraes", | ||
| "email": "joaomoraesbr@gmail.com", | ||
| "homepage": "https://github.com/00f100" | ||
| } | ||
| ], | ||
| "require": { | ||
| "php": ">=5.5", | ||
| "guzzlehttp/guzzle": "6.*" | ||
| }, | ||
| "require-dev": { | ||
| "00f100/phpdbug": "*" | ||
| }, | ||
| "autoload": { | ||
| "psr-4": { | ||
| "PHPUPhar\\": "src/" | ||
| } | ||
| } | ||
| } |
| MIT License | ||
| Copyright (c) 2016 João Moraes | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
| # PHPUPhar | ||
| Library to self update your Phar package | ||
| ## Installation | ||
| ``` | ||
| $ composer require 00f100/phpuphar | ||
| ``` | ||
| or add in your composer.json | ||
| ``` | ||
| "require": { | ||
| "00f100/phpuphar": "*" | ||
| }, | ||
| ``` | ||
| ## Usage | ||
| ```php | ||
| # Url to view current VERSION in GIT | ||
| $urlUpdate = array( | ||
| 'base' => 'https://example.com', | ||
| 'path' => '/path/to/version', | ||
| ); | ||
| # Version current | ||
| $version = '1.0.0'; | ||
| # Url to download Phar file | ||
| $urlDownloadPhar = 'https://github.com/00F100/phpatr/raw/master/dist/phpatr.phar'; | ||
| # Name of phar file | ||
| $namePhar = 'example.phar'; | ||
| # New instance of PHPUPhar | ||
| use PHPUPhar\PHPUPhar; | ||
| $selfUpdate = new PHPUPhar($urlUpdate, false, $version, $urlDownloadPhar, $namePhar); | ||
| # Test number of version | ||
| if ($version != $selfUpdate->getVersion() && $selfUpdate->update()) { | ||
| # Your version has updated ... | ||
| } | ||
| ``` |
| <?php | ||
| namespace PHPUPhar { | ||
| use Phar; | ||
| use GuzzleHttp\Client; | ||
| use Exception; | ||
| class PHPUPhar | ||
| { | ||
| private $urlVersion; | ||
| private $onlyCheck; | ||
| private $versionNow; | ||
| private $urlDownload; | ||
| private $version = false; | ||
| public function __construct(array $urlVersion, $onlyCheck, $versionNow = null, $urlDownload = null, $pharFileName = null) | ||
| { | ||
| $this->urlVersion = $urlVersion; | ||
| $this->onlyCheck = $onlyCheck; | ||
| $this->versionNow = trim(str_replace('.', '', $versionNow)); | ||
| $this->urlDownload = $urlDownload; | ||
| $this->pharFileName = $pharFileName; | ||
| } | ||
| public function getVersion() | ||
| { | ||
| if (!$this->version) { | ||
| $this->version = $this->getLastVersion(); | ||
| } | ||
| return $this->version; | ||
| } | ||
| public function update() | ||
| { | ||
| $lastVersion = str_replace('.', '', $this->getVersion()); | ||
| if ($lastVersion != str_replace('.', '', $this->versionNow)) { | ||
| return $this->updatePhar(); | ||
| } | ||
| return false; | ||
| } | ||
| private function updatePhar() | ||
| { | ||
| $pharFile = str_replace($_SERVER['argv'][0], '', Phar::running(false)) . '/' . $this->pharFileName; | ||
| try { | ||
| $client = new Client(); | ||
| $response = $client->request('GET', $this->urlDownload); | ||
| $body = $response->getBody(); | ||
| $phar = array(); | ||
| while (!$body->eof()) { | ||
| $phar[] = $body->read(10240); | ||
| } | ||
| if(is_file($pharFile)){ | ||
| unlink($pharFile); | ||
| } | ||
| if(is_file($pharFile . '.gz')){ | ||
| unlink($pharFile . '.gz'); | ||
| } | ||
| $phar = implode($phar); | ||
| $fopen = fopen($pharFile, 'w'); | ||
| fwrite($fopen, $phar); | ||
| fclose($fopen); | ||
| copy($pharFile, $this->pharFileName); | ||
| return true; | ||
| } catch (Exception $e) { | ||
| } | ||
| return false; | ||
| } | ||
| private function getLastVersion() | ||
| { | ||
| $client = new Client([ | ||
| 'base_uri' => $this->urlVersion['base'], | ||
| 'timeout' => 10, | ||
| 'allow_redirects' => false, | ||
| ]); | ||
| try { | ||
| $response = $client->request('GET', $this->urlVersion['path']); | ||
| } catch(Exception $e){ | ||
| return false; | ||
| } | ||
| $body = $response->getBody(); | ||
| $version = array(); | ||
| while (!$body->eof()) { | ||
| $version[] = $body->read(1024); | ||
| } | ||
| return trim(implode($version)); | ||
| } | ||
| } | ||
| } |
| bin/* | ||
| vendor/* | ||
| composer.lock |
| { | ||
| "name": "00f100/phpuphar", | ||
| "type": "library", | ||
| "version": "0.7.0", | ||
| "description": "Library to self update your phar file", | ||
| "keywords": ["self", "update", "phar", "php"], | ||
| "homepage": "https://github.com/00f100/phpuphar", | ||
| "authors": [ | ||
| { | ||
| "name": "João Moraes", | ||
| "email": "joaomoraesbr@gmail.com", | ||
| "homepage": "https://github.com/00f100" | ||
| } | ||
| ], | ||
| "require": { | ||
| "php": ">=5.5", | ||
| "guzzlehttp/guzzle": "6.*" | ||
| }, | ||
| "require-dev": { | ||
| "00f100/phpdbug": "*" | ||
| }, | ||
| "autoload": { | ||
| "psr-4": { | ||
| "PHPUPhar\\": "src/" | ||
| } | ||
| } | ||
| } |
| MIT License | ||
| Copyright (c) 2016 João Moraes | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
| # PHPUPhar | ||
| ## Instalation | ||
| ``` | ||
| $ composer require 00f100/phpuphar | ||
| ``` | ||
| or add in your composer.json | ||
| ``` | ||
| "require": { | ||
| "00f100/phpuphar": "*" | ||
| }, | ||
| ``` | ||
| ## Usage | ||
| ```php | ||
| # Url to view current VERSION in GIT | ||
| $urlUpdate = array( | ||
| 'base' => 'https://example.com', | ||
| 'path' => '/path/to/version', | ||
| ); | ||
| # Version current | ||
| $version = '1.0.0'; | ||
| # Url to download Phar file | ||
| $urlDownloadPhar = 'https://github.com/00F100/phpatr/raw/master/dist/phpatr.phar'; | ||
| # Name of phar file | ||
| $namePhar = 'example.phar'; | ||
| # New instance of PHPUPhar | ||
| use PHPUPhar\PHPUPhar; | ||
| $selfUpdate = new PHPUPhar($urlUpdate, false, $version, $urlDownloadPhar, $namePhar); | ||
| # Test number of version | ||
| if ($version != $selfUpdate->getVersion() && $selfUpdate->update()) { | ||
| # Your version has updated ... | ||
| } | ||
| ``` |
| <?php | ||
| namespace PHPUPhar { | ||
| use Phar; | ||
| use GuzzleHttp\Client; | ||
| use Exception; | ||
| class PHPUPhar | ||
| { | ||
| private $urlVersion; | ||
| private $onlyCheck; | ||
| private $versionNow; | ||
| private $urlDownload; | ||
| private $version = false; | ||
| public function __construct(array $urlVersion, $onlyCheck, $versionNow = null, $urlDownload = null, $pharFileName = null) | ||
| { | ||
| $this->urlVersion = $urlVersion; | ||
| $this->onlyCheck = $onlyCheck; | ||
| $this->versionNow = trim(str_replace('.', '', $versionNow)); | ||
| $this->urlDownload = $urlDownload; | ||
| $this->pharFileName = $pharFileName; | ||
| } | ||
| public function getVersion() | ||
| { | ||
| if (!$this->version) { | ||
| $this->version = $this->getLastVersion(); | ||
| } | ||
| return $this->version; | ||
| } | ||
| public function update() | ||
| { | ||
| $lastVersion = str_replace('.', '', $this->getVersion()); | ||
| if ($lastVersion != str_replace('.', '', $this->versionNow)) { | ||
| return $this->updatePhar(); | ||
| } | ||
| return false; | ||
| } | ||
| private function updatePhar() | ||
| { | ||
| $pharFile = str_replace($_SERVER['argv'][0], '', Phar::running(false)) . '/' . $this->pharFileName; | ||
| try { | ||
| $client = new Client(); | ||
| $response = $client->request('GET', $this->urlDownload); | ||
| $body = $response->getBody(); | ||
| $phar = array(); | ||
| while (!$body->eof()) { | ||
| $phar[] = $body->read(10240); | ||
| } | ||
| if(is_file($pharFile)){ | ||
| unlink($pharFile); | ||
| } | ||
| if(is_file($pharFile . '.gz')){ | ||
| unlink($pharFile . '.gz'); | ||
| } | ||
| $phar = implode($phar); | ||
| $fopen = fopen($pharFile, 'w'); | ||
| fwrite($fopen, $phar); | ||
| fclose($fopen); | ||
| copy($pharFile, $this->pharFileName); | ||
| return true; | ||
| } catch (Exception $e) { | ||
| } | ||
| return false; | ||
| } | ||
| private function getLastVersion() | ||
| { | ||
| $client = new Client([ | ||
| 'base_uri' => $this->urlVersion['base'], | ||
| 'timeout' => 10, | ||
| 'allow_redirects' => false, | ||
| ]); | ||
| try { | ||
| $response = $client->request('GET', $this->urlVersion['path']); | ||
| } catch(Exception $e){ | ||
| return false; | ||
| } | ||
| $body = $response->getBody(); | ||
| $version = array(); | ||
| while (!$body->eof()) { | ||
| $version[] = $body->read(1024); | ||
| } | ||
| return trim(implode($version)); | ||
| } | ||
| } | ||
| } |