-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakePackage.php
186 lines (161 loc) · 5.07 KB
/
MakePackage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace Paulido\Artisan;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Exception;
use InvalidArgumentException;
class MakePackage extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:package {name : The vendor/package name (e.g., vendor/name)}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Scaffold a new package.';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$name = $this->argument('name');
// Validate the package name
if (!$this->isValidPackageName($name)) {
$this->error('Invalid package name format. Please use vendor/package format.');
return 1; // Error code
}
$packagePath = base_path() . '/app/packages/' . $name;
$srcPath = $packagePath . '/src';
// Create the package directory structure
$this->createDirectory($srcPath);
$serviceName = $this->createServiceProvider($srcPath, $name);
// Uncomment if routes file creation is needed
// $this->createRoutesFile($srcPath);
$this->initializeComposer($srcPath, $name);
$this->info("Package created successfully.");
// Register the service provider
try {
$this->appendExtraToComposerJson("{$name}\\{$serviceName}", $serviceName);
} catch (Exception $e) {
$this->error("Error: " . $e->getMessage());
return 1; // Error code
}
return 0; // Success
}
/**
* Validate the package name format.
*
* @param string $name
* @return bool
*/
protected function isValidPackageName($name)
{
return preg_match('/^[a-z0-9-_]+\/[a-z0-9-_]+$/i', $name);
}
/**
* Create a directory if it doesn't exist.
*
* @param string $path
*/
protected function createDirectory($path)
{
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
}
/**
* Create a service provider file.
*
* @param string $srcPath
* @param string $name
*/
protected function createServiceProvider($srcPath, $name)
{
$providerTemplate = $this->getStub();
$providerName = ucfirst(explode('/', $name)[1]) . 'ServiceProvider';
$providerContent = $this->replaceClass($providerTemplate, $providerName);
file_put_contents($srcPath . '/' . $providerName . '.php', $providerContent);
$this->info("Service provider created.");
return $providerName;
}
/**
* Initialize composer in the package directory.
*
* @param string $srcPath
* @param string $name
*/
protected function initializeComposer($srcPath, $name)
{
$command = "composer init --name={$name}";
exec("cd {$srcPath} && {$command}");
$this->info("Composer initialized.");
}
/**
* Append the service provider and alias to the main composer.json.
*
* @param string $provider
* @param string $alias
*/
protected function appendExtraToComposerJson($provider, $alias)
{
$composerPath = base_path('composer.json'); // Update this path
if (!file_exists($composerPath)) {
throw new Exception("File not found: $composerPath");
}
$composerJson = json_decode(file_get_contents($composerPath), true);
if (!isset($composerJson['extra'])) {
$composerJson['extra'] = [];
}
if (!isset($composerJson['extra']['laravel'])) {
$composerJson['extra']['laravel'] = [
'providers' => [],
'aliases' => []
];
}
$composerJson['extra']['laravel']['providers'][] = $provider;
$composerJson['extra']['laravel']['aliases'][$alias] = "{$provider}::class";
file_put_contents($composerPath, json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$this->info("Successfully appended to composer.json.");
}
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return base_path('Paulido/Artisan/stubs/ServiceProvider.stub'); // Corrected path and namespace
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\\Paulido\\Artisan'; // Corrected namespace format
}
/**
* Replace the class name for the given stub.
*
* @param string $stub
* @param string $name
* @return string
*/
protected function replaceClass($stub, $name)
{
if (!$this->argument('name')) {
throw new InvalidArgumentException("Missing required argument package name");
}
$stub = str_replace('DummyServiceProvider', $name, $stub);
return $stub;
}
}