Skip to content
This repository was archived by the owner on Apr 16, 2024. It is now read-only.

Commit ec86190

Browse files
authored
Merge pull request #18 from harris21/master
add ChangeSourceNamespace command
2 parents 16caa6a + 54e1fd0 commit ec86190

File tree

4 files changed

+208
-1
lines changed

4 files changed

+208
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ To do that, put this in your shell profile (~/.bash_profile, ~/.zshrc, ~/bashrc)
3434
- `delete:model ` Delete an existing Model
3535
- `delete:request ` Delete an existing Request in a service
3636
- `delete:policy ` Delete an existing Policy
37+
- **src**
38+
- `src:name ` Set the source directory namespace.
3739

3840
### Commands Usage
3941

@@ -57,3 +59,6 @@ To do that, put this in your shell profile (~/.bash_profile, ~/.zshrc, ~/bashrc)
5759
- `delete:model <model>`
5860
- `delete:request <request> [<service>]`
5961
- `delete:policy <policy>`
62+
63+
#### Set Source Namespace
64+
- `src:name <name>`

lucid

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ if (file_exists(__DIR__.'/vendor/autoload.php')) {
1010
$laravel = require __DIR__.'/../../../bootstrap/app.php';
1111

1212
$commands = [
13+
new Lucid\Console\Commands\ChangeSourceNamespaceCommand(),
1314
new Lucid\Console\Commands\JobMakeCommand(),
1415
new Lucid\Console\Commands\JobDeleteCommand(),
1516
new Lucid\Console\Commands\ServiceMakeCommand(),
@@ -31,7 +32,7 @@ $commands = [
3132
new \Lucid\Console\Commands\OperationDeleteCommand(),
3233
];
3334

34-
$app = new Symfony\Component\Console\Application('Lucid Console', '0.5.0');
35+
$app = new Symfony\Component\Console\Application('Lucid Console', '0.5.5');
3536
array_walk($commands, [$app, 'add']);
3637

3738
$app->run();
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the lucid-console project.
5+
*
6+
* (c) Vinelab <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace 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+
}

src/Finder.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,4 +824,26 @@ protected function relativeFromReal($path, $needle = '')
824824

825825
return strstr($path, $needle);
826826
}
827+
828+
/**
829+
* Get the path to the Composer.json file.
830+
*
831+
* @return string
832+
*/
833+
protected function getComposerPath()
834+
{
835+
return app()->basePath().'/composer.json';
836+
}
837+
838+
/**
839+
* Get the path to the given configuration file.
840+
*
841+
* @param string $name
842+
*
843+
* @return string
844+
*/
845+
protected function getConfigPath($name)
846+
{
847+
return app()['path.config'].'/'.$name.'.php';
848+
}
827849
}

0 commit comments

Comments
 (0)