|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the lucid-console project. |
| 5 | + * |
| 6 | + |
| 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 Lucid\Console\Commands; |
| 13 | + |
| 14 | +use Lucid\Console\Finder; |
| 15 | +use Lucid\Console\Command; |
| 16 | +use Illuminate\Support\Composer; |
| 17 | +use Illuminate\Filesystem\Filesystem; |
| 18 | +use Symfony\Component\Console\Input\InputArgument; |
| 19 | +use Symfony\Component\Finder\Finder as SymfonyFinder; |
| 20 | +use Symfony\Component\Console\Command\Command as SymfonyCommand; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Charalampos Raftopoulos <[email protected]> |
| 24 | + */ |
| 25 | +class ChangeSourceNamespaceCommand extends SymfonyCommand |
| 26 | +{ |
| 27 | + use Finder; |
| 28 | + use Command; |
| 29 | + |
| 30 | + /** |
| 31 | + * The console command name. |
| 32 | + * |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + protected $name = 'src:name'; |
| 36 | + |
| 37 | + /** |
| 38 | + * The console command description. |
| 39 | + * |
| 40 | + * @var string |
| 41 | + */ |
| 42 | + protected $description = 'Set the source directory namespace.'; |
| 43 | + |
| 44 | + /** |
| 45 | + * The Composer class instance. |
| 46 | + * |
| 47 | + * @var \Illuminate\Support\Composer |
| 48 | + */ |
| 49 | + protected $composer; |
| 50 | + |
| 51 | + /** |
| 52 | + * The filesystem instance. |
| 53 | + * |
| 54 | + * @var \Illuminate\Filesystem\Filesystem |
| 55 | + */ |
| 56 | + protected $files; |
| 57 | + |
| 58 | + /** |
| 59 | + * Create a new key generator command. |
| 60 | + */ |
| 61 | + public function __construct() |
| 62 | + { |
| 63 | + parent::__construct(); |
| 64 | + |
| 65 | + $this->files = new Filesystem(); |
| 66 | + $this->composer = new Composer($this->files); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Execute the console command. |
| 71 | + */ |
| 72 | + public function fire() |
| 73 | + { |
| 74 | + try { |
| 75 | + $this->setAppDirectoryNamespace(); |
| 76 | + |
| 77 | + $this->setAppConfigNamespaces(); |
| 78 | + |
| 79 | + $this->setComposerNamespace(); |
| 80 | + |
| 81 | + $this->info('Lucid source directory namespace set!'); |
| 82 | + |
| 83 | + $this->composer->dumpAutoloads(); |
| 84 | + } catch (\Exception $e) { |
| 85 | + $this->error($e->getMessage()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Set the namespace on the files in the app directory. |
| 91 | + */ |
| 92 | + protected function setAppDirectoryNamespace() |
| 93 | + { |
| 94 | + $files = SymfonyFinder::create() |
| 95 | + ->in($this->findSourceRoot()) |
| 96 | + ->contains($this->findRootNamespace()) |
| 97 | + ->name('*.php'); |
| 98 | + |
| 99 | + foreach ($files as $file) { |
| 100 | + $this->replaceNamespace($file->getRealPath()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Replace the App namespace at the given path. |
| 106 | + * |
| 107 | + * @param string $path |
| 108 | + */ |
| 109 | + protected function replaceNamespace($path) |
| 110 | + { |
| 111 | + $search = [ |
| 112 | + 'namespace '.$this->findRootNamespace().';', |
| 113 | + $this->findRootNamespace().'\\', |
| 114 | + ]; |
| 115 | + |
| 116 | + $replace = [ |
| 117 | + 'namespace '.$this->argument('name').';', |
| 118 | + $this->argument('name').'\\', |
| 119 | + ]; |
| 120 | + |
| 121 | + $this->replaceIn($path, $search, $replace); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Set the PSR-4 namespace in the Composer file. |
| 126 | + */ |
| 127 | + protected function setComposerNamespace() |
| 128 | + { |
| 129 | + $this->replaceIn( |
| 130 | + $this->getComposerPath(), str_replace('\\', '\\\\', $this->findRootNamespace()).'\\\\', str_replace('\\', '\\\\', $this->argument('name')).'\\\\' |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Set the namespace in the appropriate configuration files. |
| 136 | + */ |
| 137 | + protected function setAppConfigNamespaces() |
| 138 | + { |
| 139 | + $search = [ |
| 140 | + $this->findRootNamespace().'\\Providers', |
| 141 | + $this->findRootNamespace().'\\Foundation', |
| 142 | + $this->findRootNamespace().'\\Http\\Controllers\\', |
| 143 | + ]; |
| 144 | + |
| 145 | + $replace = [ |
| 146 | + $this->argument('name').'\\Providers', |
| 147 | + $this->argument('name').'\\Foundation', |
| 148 | + $this->argument('name').'\\Http\\Controllers\\', |
| 149 | + ]; |
| 150 | + |
| 151 | + $this->replaceIn($this->getConfigPath('app'), $search, $replace); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Replace the given string in the given file. |
| 156 | + * |
| 157 | + * @param string $path |
| 158 | + * @param string|array $search |
| 159 | + * @param string|array $replace |
| 160 | + */ |
| 161 | + protected function replaceIn($path, $search, $replace) |
| 162 | + { |
| 163 | + if ($this->files->exists($path)) { |
| 164 | + $this->files->put($path, str_replace($search, $replace, $this->files->get($path))); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Get the console command arguments. |
| 170 | + * |
| 171 | + * @return array |
| 172 | + */ |
| 173 | + protected function getArguments() |
| 174 | + { |
| 175 | + return [ |
| 176 | + ['name', InputArgument::REQUIRED, 'The source directory namespace.'], |
| 177 | + ]; |
| 178 | + } |
| 179 | +} |
0 commit comments