| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the PHINT package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Jitendra Adhikari <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  *     <https://github.com/adhocore> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Licensed under MIT license. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Ahc\Phint\Console; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Ahc\Cli\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Ahc\Cli\Input\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Ahc\Cli\IO\Interactor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Ahc\Phint\Generator\CollisionHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Ahc\Phint\Generator\TwigGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Ahc\Phint\Util\Composer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Ahc\Phint\Util\Git; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Ahc\Phint\Util\Inflector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Ahc\Phint\Util\Path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class InitCommand extends BaseCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var string Command name */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $_name = 'init'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var string Command description */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected $_desc = 'Create and Scaffold a bare new PHP project'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Configure the command options/arguments. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     protected function onConstruct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             ->argument('<project>', 'The project name without slashes') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             ->option('-T --type', 'Project type') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             ->option('-n --name', 'Vendor full name', null, $this->_git->getConfig('user.name')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             ->option('-e --email', 'Vendor email', null, $this->_git->getConfig('user.email')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             ->option('-u --username', 'Vendor handle/username') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ->option('-N --namespace', 'Root namespace (use `/` separator)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             ->option('-P --php', 'Minimum PHP version', 'floatval') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ->option('-p --path', 'The project path (Auto resolved)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ->option('-S --sync', "Only create missing files\nUse with caution, take backup if needed", null, false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->option('-f --force', 'Run even if the project exists', null, false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ->option('-g --package', 'Packagist name (Without vendor handle)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ->option('-d --descr', 'Project description') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ->option('-w --keywords [words...]', 'Project Keywords') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ->option('-y --year', 'License Year', null, date('Y')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ->option('-b --bin [binaries...]', 'Executable binaries') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             ->option('-z --using', 'Reference package') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             ->option('-C --config', 'JSON filepath to read config from') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ->option('-G --gh-template', "Use `.github/` as template path\nBy default uses `docs/`", null, false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             ->option('-R --req [pkgs...]', 'Required packages') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             ->option('-D --dev [pkgs...]', 'Developer packages') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             ->option('-t --no-travis', 'Disable travis') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             ->option('-c --no-codecov', 'Disable codecov') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ->option('-s --no-scrutinizer', 'Disable scrutinizer') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ->option('-l --no-styleci', 'Disable StyleCI') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ->option('-L --license', 'License') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ->usage($this->writer()->colorizer()->colors('' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 . '<bold>  phint init</end> <line><project></end> ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 . '<comment>--force --descr "Awesome project" --name "YourName" --email [email protected]</end><eol/>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 . '<bold>  phint init</end> <line><project></end> ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 . '<comment>--using laravel/lumen --namespace Project/Api --type project</end><eol/>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 . '<bold>  phint init</end> <line><project></end> ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 . '<comment>--php 7.0 --config /path/to/json --dev mockery/mockery --req adhocore/cli</end><eol/>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * Execute the command action. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function execute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $io = $this->app()->io(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if ($using = $this->using) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $io->colors("Using <cyanBold>$using</end> to create project <comment>(takes some time)</end><eol/>"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $this->_composer->createProject($this->path, $this->using); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $io->comment('Generating files ...', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $this->generate($this->path, $this->values()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $io->colors('Setting up <cyanBold>git</end><eol/>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $this->_git->withWorkDir($this->path)->init()->addRemote($this->username, $this->project); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $io->colors('Setting up <cyanBold>composer</end> <comment>(takes some time)</end><eol>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if ($using) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $this->_composer->withWorkDir($this->path)->update(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $this->_composer->withWorkDir($this->path)->install(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $success = $this->_composer->successful(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $success ? $io->ok('Done', true) : $io->error('Composer setup failed', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $this->logging('end'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function interact(Interactor $io) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $project = $this->project; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         if (!\preg_match('/[a-z0-9_-]/i', $project)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             throw new InvalidArgumentException('Project argument should only contain [a-z0-9_-]'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $io->okBold('Phint Setup', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->logging('start'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $this->set('path', $path = $this->prepareProjectPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $this->loadConfig($this->config); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $this->collectMissing($io); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $this->collectPackages($io); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     protected function prepareProjectPath(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $path = $this->project; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $io   = $this->app()->io(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         if (!$this->_pathUtil->isAbsolute($path)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             $path = \getcwd() . '/' . $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         if (\is_dir($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             $this->projectExists(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             \mkdir($path, 0777, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         return $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |     protected function projectExists() | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |         if (!$this->force && !$this->sync) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |             throw new InvalidArgumentException( | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |                 \sprintf('Something with the name "%s" already exists!', \basename($path)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         if ($this->force && !$this->sync) { | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |             $io->error('You have set --force flag, existing files will be overwritten', true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     protected function loadConfig(string $path = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         if (empty($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         if (!$this->_pathUtil->isAbsolute($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             $path = \getcwd() . '/' . $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         if (!\is_file($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             $this->app()->io()->error('Invalid path specified for config', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         foreach ($this->_pathUtil->readAsJson($path) as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             $this->$key ?? $this->set($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     protected function collectMissing(Interactor $io) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         $promptConfig = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             'type' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                 'choices' => ['p' => 'project', 'l' => 'library', 'c' => 'composer-plugin'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                 'default' => 'l', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |                 'restore' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             'package' => ['default' => $this->project, 'retry' => 0], | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             'license' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |                 'choices' => ['m' => 'MIT', 'g' => 'GNULGPL', 'a' => 'Apache2', 'b' => 'BSDSimple', 'i' => 'ISC'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |                 'default' => 'm', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |             'php' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |                 'choices' => ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |                 'default' => '7.0', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |             'using'    => ['retry' => 0, 'extra' => ' (ENTER to skip)'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |             'keywords' => ['retry' => 0, 'extra' => ' (CSV, ENTER to skip)', 'default' => "php, {$this->project}"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |             'bin'      => ['retry' => 0, 'extra' => ' (CSV, ENTER to skip)'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |             // Donot promt these here! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |             'req'    => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |             'dev'    => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |             'config' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         $this->promptAll($io, $promptConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |     protected function collectPackages(Interactor $io) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |         foreach (['req' => 'Required', 'dev' => 'Developer'] as $key => $label) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |             $pkgs = $this->$key ?: $this->promptPackages($label, $io); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |             foreach ($pkgs as &$pkg) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |                 $pkg = \strpos($pkg, ':') === false ? "{$pkg}:@stable" : $pkg; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |                 $pkg = \array_combine(['name', 'version'], \explode(':', $pkg, 2)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |             $this->set($key, $pkgs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |     protected function promptPackages(string $label, Interactor $io): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         $pkgs = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |         do { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |             if (!$pkg = $io->prompt($label . ' package (ENTER to skip)', null, [$this, 'validatePackage'], 0)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |             $pkgs[] = $pkg; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |         } while (true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |         return $pkgs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |     public function validatePackage(string $pkg): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |         $pkg = \trim($pkg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |         if ($pkg && \strpos($pkg, '/') === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |             throw new InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |                 'Package name format should be vendor/package:version (version can be omitted)' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         return $pkg; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |     protected function generate(string $projectPath, array $parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |         $templatePath = __DIR__ . '/../../resources'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |         $generator    = new TwigGenerator($templatePath, $this->getCachePath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |         // Normalize license (default MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         $parameters['license']   = \strtolower($parameters['license'][0] ?? 'm'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |         $parameters['namespace'] = $this->makeNamespace($parameters['namespace']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         $parameters['keywords']  = $this->makeArray($parameters['keywords'], ['php', $this->project]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |         $parameters['bin']       = $this->makeArray($parameters['bin']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |         $generator->generate($projectPath, $parameters, new CollisionHandler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |     protected function makeNamespace(string $value): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |         $in = new Inflector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |         $project = $this->package; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |         $value   = $in->stuldyCase(\str_replace([' ', '/'], '\\', $value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |         $project = $in->stuldyCase(\str_replace([' ', '/', '\\'], '-', $project)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |         if (\stripos($value, $project) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |             $value .= '\\' . $project; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |         return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |     protected function makeArray($value, array $default = []): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |         if (empty($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |             return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |         if (\is_string($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |             $value = \array_map('trim', \explode(',', $value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |         return \array_values(\array_unique(\array_merge($default, $value))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 293 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 294 |  |  |  |