Skip to content

Commit 4fc3088

Browse files
authored
Merge pull request #3585 from paulbalandan/cli-getsegment
Use CLI::getSegment(2) to refer to the first argument of generator command
2 parents 92f7e00 + 0381f4c commit 4fc3088

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

system/CLI/CLI.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,8 @@ public static function getURI(): string
947947
* // segment(3) is 'three', not '-f' or 'anOption'
948948
* > php spark one two -f anOption three
949949
*
950+
* **IMPORTANT:** The index here is one-based instead of zero-based.
951+
*
950952
* @param integer $index
951953
*
952954
* @return mixed|null

system/CLI/GeneratorCommand.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ public function run(array $params)
152152
*/
153153
protected function getClassName(): string
154154
{
155-
$name = $this->params[0] ?? CLI::getSegment(0);
155+
$name = $this->params[0] ?? CLI::getSegment(2);
156+
156157
return $name ?? '';
157158
}
158159

@@ -203,6 +204,7 @@ protected function qualifyClassName(string $class): string
203204
protected function getRootNamespace(): string
204205
{
205206
$rootNamespace = $this->params['n'] ?? CLI::getOption('n') ?? APP_NAMESPACE;
207+
206208
return trim(str_replace('/', '\\', $rootNamespace), '\\');
207209
}
208210

@@ -238,6 +240,7 @@ protected function buildPath(string $class): string
238240

239241
$path = $base . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $name) . '.php';
240242
$filename = $this->modifyBasename(basename($path));
243+
241244
return implode(DIRECTORY_SEPARATOR, array_slice(explode(DIRECTORY_SEPARATOR, $path), 0, -1)) . DIRECTORY_SEPARATOR . $filename;
242245
}
243246

@@ -310,6 +313,7 @@ protected function setReplacements(string $template, string $class): string
310313

311314
$template = str_replace($namespaces, $this->getNamespace($class), $template);
312315
$class = str_replace($this->getNamespace($class) . '\\', '', $class);
316+
313317
return str_replace($classes, $class, $template);
314318
}
315319

@@ -326,11 +330,10 @@ protected function sortImports(string $template): string
326330
{
327331
$imports = explode("\n", trim($match['imports']));
328332
sort($imports);
333+
329334
return str_replace(trim($match['imports']), implode("\n", $imports), $template);
330335
}
331336

332-
// @codeCoverageIgnoreStart
333-
return $template;
334-
// @codeCoverageIgnoreEnd
337+
return $template; // @codeCoverageIgnore
335338
}
336339
}

0 commit comments

Comments
 (0)