| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the puli/cli package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Bernhard Schussek <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Puli\Cli\Updater; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Humbug\SelfUpdate\Exception\HttpRequestException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Humbug\SelfUpdate\Strategy\StrategyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Humbug\SelfUpdate\Updater; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Humbug\SelfUpdate\VersionParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Puli\Cli\PuliApplicationConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class PuliStrategy implements StrategyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     const ANY = 'any'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     const STABLE = 'stable'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     const UNSTABLE = 'unstable'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     const MANIFEST = 'https://puli.io/download/versions.json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     const REMOTE_PHAR = 'https://puli.io/download/%s/puli.phar'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private static $stabilities = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         self::STABLE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         self::UNSTABLE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         self::ANY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private $stability = self::ANY; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * Download the remote Phar file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param Updater $updater | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |     public function download(Updater $updater) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         /* Switch remote request errors to HttpRequestExceptions */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |         set_error_handler(array($updater, 'throwHttpRequestException')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |         $remoteUrl = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |             self::REMOTE_PHAR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |             $this->getCurrentRemoteVersion($updater) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |         $result = humbug_get_contents($remoteUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         restore_error_handler(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         if (false === $result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             throw new HttpRequestException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 'Request to URL failed: %s', $remoteUrl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |         file_put_contents($updater->getTempPharFile(), $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * Retrieve the current version available remotely. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param Updater $updater | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 2 |  |     public function getCurrentRemoteVersion(Updater $updater) | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         /* Switch remote request errors to HttpRequestExceptions */ | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 2 |  |         set_error_handler(array($updater, 'throwHttpRequestException')); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 2 |  |         $versions = json_decode(humbug_get_contents(self::MANIFEST), true); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 2 |  |         restore_error_handler(); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 2 |  |         if (false === $versions) { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             throw new HttpRequestException(sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |                 'Request to URL failed: %s', self::MANIFEST | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             )); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 2 |  |         $versionParser = new VersionParser($versions); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 2 |  |         if ($this->getStability() === self::STABLE) { | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |             return $versionParser->getMostRecentStable(); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 2 |  |         if ($this->getStability() === self::UNSTABLE) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             return $versionParser->getMostRecentUnstable(); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 2 |  |         return $versionParser->getMostRecentAll(); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * Retrieve the current version of the local phar file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param Updater $updater | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |     public function getCurrentLocalVersion(Updater $updater) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |         return PuliApplicationConfig::VERSION; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * Set target stability. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @param string $stability | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 6 |  |     public function setStability($stability) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 6 |  |         if (!in_array($stability, self::$stabilities, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |             throw new InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 'Invalid stability value. Must be one of "stable", "unstable" or "any".' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 5 |  |         $this->stability = $stability; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * Get target stability. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 5 |  |     public function getStability() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 5 |  |         return $this->stability; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |  |