Skip to content

Commit

Permalink
Merge pull request #98 from lion-packages/new
Browse files Browse the repository at this point in the history
The body of generated classes (Controllers) is validated
  • Loading branch information
GabrielPalac authored Nov 18, 2024
2 parents 7acb806 + daf3457 commit 2eeed82
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 51 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"lion/database": "^10.3",
"lion/dependency-injection": "^3.0",
"lion/exceptions": "^1.3",
"lion/files": "^7.0",
"lion/files": "^7.1",
"lion/helpers": "^4.0",
"lion/mailer": "^6.3",
"lion/request": "^6.9",
Expand Down
19 changes: 10 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/LionBundle/Commands/CommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ private function getCommands(string $pathCommands, string $namespace, string $pa
/** @var array<int, Command> $commands */
$commands = [];

foreach ($this->store->view($pathCommands) as $file) {
foreach ($this->store->getFiles($pathCommands) as $file) {
if (isSuccess($this->store->validate([$file], ['php']))) {
$class = getNamespaceFromFile($file, $namespace, $pathSplit);
$class = $this->store->getNamespaceFromFile($file, $namespace, $pathSplit);

$commands[] = $this->container->resolve($class);
}
Expand Down
11 changes: 9 additions & 2 deletions src/LionBundle/Commands/Lion/DB/CrudCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Lion\Command\Command;
use Lion\Database\Connection;
use Lion\Database\Driver;
use LogicException;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -113,9 +115,10 @@ protected function configure(): void
* @param OutputInterface $output [OutputInterface is the interface
* implemented by all Output classes]
*
* @return int [0 if everything went fine, or an exit code]
* @return int
*
* @throws LogicException [When this abstract method is not implemented]
* @throws LogicException|ExceptionInterface [When this abstract method is
* not implemented]
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
Expand Down Expand Up @@ -171,6 +174,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
* implemented by all Output classes]
*
* @return void
*
* @throws ExceptionInterface
*/
private function addDBRules(string $entity, OutputInterface $output): void
{
Expand All @@ -197,6 +202,8 @@ private function addDBRules(string $entity, OutputInterface $output): void
* implemented by all Output classes]
*
* @return void
*
* @throws ExceptionInterface
*/
private function addControllerAndModel(
string $driver,
Expand Down
4 changes: 2 additions & 2 deletions src/LionBundle/Commands/Lion/DB/DBSeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/** @var array<int, SeedInterface> $files */
$files = [];

foreach ($this->store->view('./database/Seed/') as $seed) {
foreach ($this->store->getFiles('./database/Seed/') as $seed) {
if (isSuccess($this->store->validate([$seed], ['php']))) {
$class = getNamespaceFromFile($seed, 'Database\\Seed\\', 'Seed/');
$class = $this->store->getNamespaceFromFile($seed, 'Database\\Seed\\', 'Seed/');

/** @var SeedInterface $seedInterface */
$seedInterface = new $class();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ private function getMigrations(): array
StoreProcedureInterface::class => [],
];

foreach ($this->store->view('./database/Migrations/') as $migration) {
foreach ($this->store->getFiles('./database/Migrations/') as $migration) {
if (isSuccess($this->store->validate([$migration], ['php']))) {
$namespace = getNamespaceFromFile($migration, 'Database\\Migrations\\', 'Migrations/');
$namespace = $this->store->getNamespaceFromFile($migration, 'Database\\Migrations\\', 'Migrations/');

$tableMigration = include_once($migration);

Expand Down
10 changes: 7 additions & 3 deletions src/LionBundle/Commands/Lion/New/ControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Lion\Command\Command;
use Lion\Files\Store;
use Lion\Helpers\Str;
use LogicException;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -110,9 +112,10 @@ protected function configure(): void
* @param OutputInterface $output [OutputInterface is the interface
* implemented by all Output classes]
*
* @return int 0 if everything went fine, or an exit code
* @return int
*
* @throws LogicException When this abstract method is not implemented
* @throws LogicException [When this abstract method is not implemented]
* @throws ExceptionInterface
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
Expand Down Expand Up @@ -165,12 +168,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
);

if ('none' != $model) {
$factoryController->add("\nuse {$dataModel->namespace}\\{$dataModel->class};\n");
$factoryController->add("\nuse {$dataModel->namespace}\\{$dataModel->class};");
}

$factoryController
->add(
<<<EOT
use Lion\Database\Interface\DatabaseCapsuleInterface;
use stdClass;
Expand Down
6 changes: 6 additions & 0 deletions src/LionBundle/Commands/Lion/New/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
namespace {$namespace};
use Lion\Test\Test;
use PHPUnit\Framework\Attributes\Test as Testing;
class {$class} extends Test
{
Expand All @@ -118,6 +119,11 @@ protected function setUp(): void
protected function tearDown(): void
{
}
#[Testing]
public function example(): void
{
}
}
PHP
Expand Down
4 changes: 2 additions & 2 deletions src/LionBundle/Commands/Lion/Schedule/ListScheduleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$size = 0;

foreach ($this->store->view('app/Console/Cron/') as $file) {
foreach ($this->store->getFiles('app/Console/Cron/') as $file) {
if (isSuccess($this->store->validate([$file], ['php']))) {
$namespace = getNamespaceFromFile(
$namespace = $this->store->getNamespaceFromFile(
(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? str_replace('\\', '/', $file) : $file),
'App\\Console\\Cron\\',
$this->store->normalizePath('Cron/')
Expand Down
4 changes: 2 additions & 2 deletions src/LionBundle/Commands/Lion/Schedule/UpScheduleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/** @var array<int, ScheduleInterface> $files */
$files = [];

foreach ($this->store->view('app/Console/Cron/') as $file) {
foreach ($this->store->getFiles('app/Console/Cron/') as $file) {
if (isSuccess($this->store->validate([$file], ['php']))) {
$namespace = getNamespaceFromFile(
$namespace = $this->store->getNamespaceFromFile(
$this->store->normalizePath($file),
'App\\Console\\Cron\\',
$this->store->normalizePath('Cron/')
Expand Down
4 changes: 2 additions & 2 deletions src/LionBundle/Commands/Lion/Sockets/ServerSocketCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ private function selectSocket(InputInterface $input, OutputInterface $output): s
{
$classList = [];

foreach ($this->store->view('./app/Sockets/') as $file) {
foreach ($this->store->getFiles('./app/Sockets/') as $file) {
if (isSuccess($this->store->validate([$file], [ClassFactory::PHP_EXTENSION]))) {
$classList[] = getNamespaceFromFile($file, 'App\\Sockets\\', 'Sockets/');
$classList[] = $this->store->getNamespaceFromFile($file, 'App\\Sockets\\', 'Sockets/');
}
}

Expand Down
24 changes: 0 additions & 24 deletions src/LionBundle/Helpers/Bundle/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,27 +355,3 @@ function env(string $key, mixed $default = null): mixed
return Env::get($key, $default);
}
}

if (!function_exists('getNamespaceFromFile')) {
/**
* Gets the namespace of a class through a defined path
*
* @param string $file [File path]
* @param string $namespace [Namespace for the file]
* @param string $split [Separator to obtain the namespace]
*
* @return string
*
* @internal
*/
function getNamespaceFromFile(string $file, string $namespace, string $split = '/'): string
{
$splitFile = explode($split, $file);

$namespace = str_replace("/", "\\", "{$namespace}{$splitFile[1]}");

$namespace = str_replace('.php', '', $namespace);

return trim($namespace);
}
}

0 comments on commit 2eeed82

Please sign in to comment.