-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from gacela-project/feature/generate-command-w…
…ith-one-arg Pass only one argument to the "maker command"
- Loading branch information
Showing
20 changed files
with
347 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,5 @@ | |
/.idea/ | ||
/.vscode/ | ||
/vendor/ | ||
/TestModule/ | ||
.phpunit.* | ||
.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/CodeGenerator/Domain/Io/CommandArguments/CommandArgumentsParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gacela\CodeGenerator\Domain\Io\CommandArguments; | ||
|
||
use Gacela\CodeGenerator\Domain\Io\CommandArguments\Exception\CommandArgumentsException; | ||
use Gacela\CodeGenerator\Domain\ReadModel\CommandArguments; | ||
use InvalidArgumentException; | ||
|
||
final class CommandArgumentsParser | ||
{ | ||
private array $composerJson; | ||
|
||
public function __construct(array $composerJson) | ||
{ | ||
$this->composerJson = $composerJson; | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function parse(array $arguments): CommandArguments | ||
{ | ||
if (empty($arguments)) { | ||
throw new InvalidArgumentException('Expected argument to be the location of the new module. For example: App/TestModule'); | ||
} | ||
|
||
return $this->createCommandArguments($arguments[0]); | ||
} | ||
|
||
private function createCommandArguments(string $desiredNamespace): CommandArguments | ||
{ | ||
if (!isset($this->composerJson['autoload'])) { | ||
throw CommandArgumentsException::noAutoloadFound(); | ||
} | ||
|
||
if (!isset($this->composerJson['autoload']['psr-4'])) { | ||
throw CommandArgumentsException::noAutoloadPsr4Found(); | ||
} | ||
|
||
$psr4 = $this->composerJson['autoload']['psr-4']; | ||
$allPsr4Combinations = $this->allPossiblePsr4Combinations($desiredNamespace); | ||
|
||
foreach ($allPsr4Combinations as $psr4Combination) { | ||
$psr4Key = $psr4Combination . '\\'; | ||
if (isset($psr4[$psr4Key])) { | ||
return $this->foundPsr4($psr4Key, $psr4[$psr4Key], $desiredNamespace); | ||
} | ||
} | ||
|
||
throw CommandArgumentsException::noAutoloadPsr4MatchFound($desiredNamespace); | ||
} | ||
|
||
/** | ||
* Combine all possible psr-4 combinations and return them ordered by longer to shorter. | ||
* This way we'll be able to find the longer match first. | ||
* For example: App/TestModule/TestSubModule will produce an array such as: | ||
* [ | ||
* 'App/TestModule/TestSubModule', | ||
* 'App/TestModule', | ||
* 'App', | ||
* ]. | ||
*/ | ||
private function allPossiblePsr4Combinations(string $desiredNamespace): array | ||
{ | ||
$result = []; | ||
|
||
foreach (explode('/', $desiredNamespace) as $explodedArg) { | ||
if (empty($result)) { | ||
$result[] = $explodedArg; | ||
} else { | ||
$prevValue = $result[count($result) - 1]; | ||
$result[] = $prevValue . '\\' . $explodedArg; | ||
} | ||
} | ||
|
||
return array_reverse($result); | ||
} | ||
|
||
private function foundPsr4(string $psr4Key, string $psr4Value, string $desiredNamespace): CommandArguments | ||
{ | ||
$rootDir = substr($psr4Value, 0, -1); | ||
$rootNamespace = substr($psr4Key, 0, -1); | ||
$targetDirectory = str_replace(['/', $rootNamespace, '\\'], ['\\', $rootDir, '/'], $desiredNamespace); | ||
$namespace = str_replace([$rootDir, '/'], [$rootNamespace, '\\'], $targetDirectory); | ||
|
||
return new CommandArguments($namespace, $targetDirectory); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/CodeGenerator/Domain/Io/CommandArguments/Exception/CommandArgumentsException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gacela\CodeGenerator\Domain\Io\CommandArguments\Exception; | ||
|
||
use RuntimeException; | ||
|
||
final class CommandArgumentsException extends RuntimeException | ||
{ | ||
public static function noAutoloadFound(): self | ||
{ | ||
return new self('No autoload found in your composer.json'); | ||
} | ||
|
||
public static function noAutoloadPsr4Found(): self | ||
{ | ||
return new self('No autoload psr-4 match found in your composer.json'); | ||
} | ||
|
||
public static function noAutoloadPsr4MatchFound(string $desiredNamespace): self | ||
{ | ||
return new self('No autoload psr-4 match found for ' . $desiredNamespace); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.