Skip to content
4 changes: 4 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ jobs:
run: |
composer install --no-interaction --prefer-dist --no-progress --no-suggest

- name: Check code formatting with PHP CS Fixer
run: |
vendor/bin/php-cs-fixer fix --dry-run --diff --ansi

- name: Check quality code with PHPInsights
run: |
vendor/bin/phpinsights -n --ansi --format=github-action
Expand Down
50 changes: 50 additions & 0 deletions src/Console/Commands/MakeCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Phenix\Console\Commands;

use Phenix\Console\Maker;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class MakeCollection extends Maker
{
/**
* @var string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint
*/
protected static $defaultName = 'make:collection';

/**
* @var string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint
*/
protected static $defaultDescription = 'Creates a new collection.';

protected function configure(): void
{
$this->setHelp('This command allows you to create a new collection.');

$this->addArgument('name', InputArgument::REQUIRED, 'The collection name');

$this->addOption('force', 'f', InputOption::VALUE_NONE, 'Force to create collections');
}

protected function outputDirectory(): string
{
return 'app' . DIRECTORY_SEPARATOR . 'Collections';
}

protected function stub(): string
{
return 'collection.stub';
}

protected function commonName(): string
{
return 'Collection';
}
}
129 changes: 129 additions & 0 deletions src/Console/Commands/MakeModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

declare(strict_types=1);

namespace Phenix\Console\Commands;

use Phenix\Facades\File;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class MakeModel extends CommonMaker
{
/**
* @var string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint
*/
protected static $defaultName = 'make:model';

/**
* @var string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint
*/
protected static $defaultDescription = 'Creates a new model.';

protected function configure(): void
{
parent::configure();

$this->addOption('collection', 'cn', InputOption::VALUE_NONE, 'Create a collection for the model');

$this->addOption('query', 'qb', InputOption::VALUE_NONE, 'Create a query builder for the model');

$this->addOption('all', 'a', InputOption::VALUE_NONE, 'Create a model with custom query builder and collection');
}

protected function outputDirectory(): string
{
return 'app' . DIRECTORY_SEPARATOR . 'Models';
}

protected function stub(): string
{
$stub = 'model.stub';

if ($this->input->getOption('all')) {
$stub = 'model.all.stub';
} elseif ($this->input->getOption('collection')) {
$stub = 'model.collection.stub';
} elseif ($this->input->getOption('query')) {
$stub = 'model.query.stub';
}

return $stub;
}

protected function commonName(): string
{
return 'Model';
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->input = $input;

$search = parent::SEARCH;

$name = $this->input->getArgument('name');
$force = $this->input->getOption('force');

$namespace = explode(DIRECTORY_SEPARATOR, $name);
$className = array_pop($namespace);
$fileName = $this->getCustomFileName() ?? $className;

$filePath = $this->preparePath($namespace) . DIRECTORY_SEPARATOR . "{$fileName}.php";
$namespace = $this->prepareNamespace($namespace);

$replace = [$namespace, $className];

if (File::exists($filePath) && ! $force) {
$output->writeln(["<comment>{$this->commonName()} already exists!</comment>", self::EMPTY_LINE]);

return parent::SUCCESS;
}

$application = $this->getApplication();

if ($input->getOption('collection') || $input->getOption('all')) {
$command = $application->find('make:collection');
$collectionName = "{$name}Collection";

$arguments = new ArrayInput([
'name' => $collectionName,
]);

$command->run($arguments, $output);

$search[] = '{collection_name}';
$replace[] = $collectionName;
}

if ($input->getOption('query') || $input->getOption('all')) {
$command = $application->find('make:query');
$queryName = "{$name}Query";

$arguments = new ArrayInput([
'name' => $queryName,
]);

$command->run($arguments, $output);

$search[] = '{query_name}';
$replace[] = $queryName;
}

$stub = $this->getStubContent();
$stub = str_replace($search, $replace, $stub);

File::put($filePath, $stub);

$output->writeln(["<info>{$this->commonName()} successfully generated!</info>", self::EMPTY_LINE]);


return parent::SUCCESS;
}
}
50 changes: 50 additions & 0 deletions src/Console/Commands/MakeQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Phenix\Console\Commands;

use Phenix\Console\Maker;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class MakeQuery extends Maker
{
/**
* @var string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint
*/
protected static $defaultName = 'make:query';

/**
* @var string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint
*/
protected static $defaultDescription = 'Creates a new query.';

protected function configure(): void
{
$this->setHelp('This command allows you to create a new query.');

$this->addArgument('name', InputArgument::REQUIRED, 'The query name');

$this->addOption('force', 'f', InputOption::VALUE_NONE, 'Force to create queries');
}

protected function outputDirectory(): string
{
return 'app' . DIRECTORY_SEPARATOR . 'Queries';
}

protected function stub(): string
{
return 'query.stub';
}

protected function commonName(): string
{
return 'Query';
}
}
6 changes: 6 additions & 0 deletions src/Providers/CommandsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace Phenix\Providers;

use Phenix\Console\Commands\MakeCollection;
use Phenix\Console\Commands\MakeController;
use Phenix\Console\Commands\MakeMiddleware;
use Phenix\Console\Commands\MakeModel;
use Phenix\Console\Commands\MakeQuery;
use Phenix\Console\Commands\MakeRequest;
use Phenix\Console\Commands\MakeServiceProvider;
use Phenix\Console\Commands\MakeTest;
Expand All @@ -19,6 +22,9 @@ public function boot(): void
MakeRequest::class,
MakeController::class,
MakeMiddleware::class,
MakeModel::class,
MakeCollection::class,
MakeQuery::class,
MakeServiceProvider::class,
]);
}
Expand Down
12 changes: 12 additions & 0 deletions src/stubs/collection.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace {namespace};

use Phenix\Database\Models\Collection;

class {name} extends Collection
{
//
}
27 changes: 27 additions & 0 deletions src/stubs/model.all.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace {namespace};

use App\Collections\{collection_name};
use App\Queries\{query_name};
use Phenix\Database\Models\DatabaseModel;

class {name} extends DatabaseModel
{
public static function table(): string
{
//
}

protected static function newQueryBuilder(): {query_name}
{
return new {query_name}();
}

public function newCollection(): {collection_name}
{
return new {collection_name}();
}
}
21 changes: 21 additions & 0 deletions src/stubs/model.collection.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace {namespace};

use App\Collections\{collection_name};
use Phenix\Database\Models\DatabaseModel;

class {name} extends DatabaseModel
{
public static function table(): string
{
//
}

public function newCollection(): {collection_name}
{
return new {collection_name}();
}
}
21 changes: 21 additions & 0 deletions src/stubs/model.query.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace {namespace};

use App\Queries\{query_name};
use Phenix\Database\Models\DatabaseModel;

class {name} extends DatabaseModel
{
public static function table(): string
{
//
}

protected static function newQueryBuilder(): {query_name}
{
return new {query_name}();
}
}
15 changes: 15 additions & 0 deletions src/stubs/model.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace {namespace};

use Phenix\Database\Models\DatabaseModel;

class {name} extends DatabaseModel
{
public static function table(): string
{
//
}
}
12 changes: 12 additions & 0 deletions src/stubs/query.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace {namespace};

use Phenix\Database\Models\QueryBuilders\DatabaseQueryBuilder;

class {name} extends DatabaseQueryBuilder
{
//
}
Loading
Loading