Skip to content

Commit

Permalink
feat: models repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseekn committed Nov 13, 2021
1 parent c5b498a commit e96e32c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 19 deletions.
1 change: 1 addition & 0 deletions config/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
new \Core\Console\Make\Validator(),
new \Core\Console\Make\Seed(),
new \Core\Console\Make\Factory(),
new \Core\Console\Make\Repository(),
new \Core\Console\Make\View(),
new \Core\Console\Make\Mail(),
new \Core\Console\Make\Middleware(),
Expand Down
4 changes: 2 additions & 2 deletions core/Console/Make/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$factorys = $input->getArgument('factory');
$factories = $input->getArgument('factory');

foreach ($factorys as $factory) {
foreach ($factories as $factory) {
list(, $class) = Make::generateClass($factory, 'factory', true, true);

if (!Make::createFactory($factory, $input->getOption('namespace'))) {
Expand Down
23 changes: 22 additions & 1 deletion core/Console/Make/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ public static function createFactory(string $factory, ?string $namespace = null)
$data = self::stubs()->readFile('Factory.stub');
$data = self::addNamespace($data, 'App\Database\Factories', $namespace);
$data = str_replace('CLASSNAME', self::fixPluralTypo($class, true), $data);
$data = str_replace('TABLENAME', self::fixPluralTypo($name), $data);
$data = str_replace('MODELNAME', self::fixPluralTypo(ucfirst($name), true), $data);

$storage = Storage::path(config('storage.factories'));
Expand All @@ -184,6 +183,28 @@ public static function createFactory(string $factory, ?string $namespace = null)
return true;
}

public static function createRepository(string $repository, ?string $namespace = null)
{
list($name, $class) = self::generateClass($repository, 'repository', true, true);

$data = self::stubs()->readFile('Repository.stub');
$data = self::addNamespace($data, 'App\Database\Repositories', $namespace);
$data = str_replace('CLASSNAME', self::fixPluralTypo($class, true), $data);
$data = str_replace('MODELNAME', self::fixPluralTypo(ucfirst($name), true), $data);

$storage = Storage::path(config('storage.repositories'));

if (!is_null($namespace)) {
$storage = $storage->addPath(str_replace('\\', '/', $namespace));
}

if (!$storage->writeFile(self::fixPluralTypo($class, true) . '.php', $data)) {
return false;
}

return true;
}

public static function createHelper(string $helper)
{
list(, $class) = self::generateClass($helper, 'helper', true);
Expand Down
47 changes: 47 additions & 0 deletions core/Console/Make/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* @copyright 2021 - N'Guessan Kouadio Elisée ([email protected])
* @license MIT (https://opensource.org/licenses/MIT)
* @link https://github.com/eliseekn/tinymvc
*/

namespace Core\Console\Make;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Create new model repository
*/
class Repository extends Command
{
protected static $defaultName = 'make:repository';

protected function configure()
{
$this->setDescription('Create new model repository');
$this->addArgument('repository', InputArgument::REQUIRED|InputArgument::IS_ARRAY, 'The name of model repository table (separated by space if many)');
$this->addOption('namespace', null, InputOption::VALUE_OPTIONAL, 'Specify namespace (base: App\Database\Repositories)');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$repositories = $input->getArgument('repository');

foreach ($repositories as $repository) {
list(, $class) = Make::generateClass($repository, 'repository', true, true);

if (!Make::createRepository($repository, $input->getOption('namespace'))) {
$output->writeln('<fg=yellow>Failed to create repository "' . Make::fixPluralTypo($class, true) . '"</fg>');
}

$output->writeln('<info>repository "' . Make::fixPluralTypo($class, true) . '" has been created</info>');
}

return Command::SUCCESS;
}
}
32 changes: 16 additions & 16 deletions core/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,24 @@ public static function last()
return self::select('*')->last();
}

public static function take(int $count, $callback = null)
public static function take(int $count, $subquery = null)
{
return self::select('*')->subQuery($callback)->take($count);
return self::select('*')->subQuery($subquery)->take($count);
}

public static function oldest(string $column = 'created_at', $callback = null)
public static function oldest(string $column = 'created_at', $subquery = null)
{
return self::select('*')->subQuery($callback)->oldest($column)->getAll();
return self::select('*')->subQuery($subquery)->oldest($column)->getAll();
}

public static function newest(string $column = 'created_at', $callback = null)
public static function newest(string $column = 'created_at', $subquery = null)
{
return self::select('*')->subQuery($callback)->newest($column)->getAll();
return self::select('*')->subQuery($subquery)->newest($column)->getAll();
}

public static function latest(string $column = 'id', $callback = null)
public static function latest(string $column = 'id', $subquery = null)
{
return self::select('*')->subQuery($callback)->latest($column)->getAll();
return self::select('*')->subQuery($subquery)->latest($column)->getAll();
}

public static function select(string ...$columns)
Expand All @@ -96,24 +96,24 @@ public static function where(string $column, $operator = null, $value = null)
return self::select('*')->where($column, $operator, $value);
}

public static function count(string $column = 'id', $callback = null)
public static function count(string $column = 'id', $subquery = null)
{
return (new Repository(static::$table))->count($column)->subQuery($callback)->get()->value;
return (new Repository(static::$table))->count($column)->subQuery($subquery)->get()->value;
}

public static function sum(string $column, $callback = null)
public static function sum(string $column, $subquery = null)
{
return (new Repository(static::$table))->sum($column)->subQuery($callback)->get()->value;
return (new Repository(static::$table))->sum($column)->subQuery($subquery)->get()->value;
}

public static function max(string $column, $callback = null)
public static function max(string $column, $subquery = null)
{
return (new Repository(static::$table))->max($column)->subQuery($callback)->get()->value;
return (new Repository(static::$table))->max($column)->subQuery($subquery)->get()->value;
}

public static function min(string $column, $callback = null)
public static function min(string $column, $subquery = null)
{
return (new Repository(static::$table))->min($column)->subQuery($callback)->get()->value;
return (new Repository(static::$table))->min($column)->subQuery($subquery)->get()->value;
}

public static function metrics(string $column, string $type, string $period, int $interval = 0, ?array $query = null)
Expand Down
19 changes: 19 additions & 0 deletions resources/stubs/Repository.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @copyright 2021 - N'Guessan Kouadio Elisée ([email protected])
* @license MIT (https://opensource.org/licenses/MIT)
* @link https://github.com/eliseekn/tinymvc
*/

namespace NAMESPACE;

use App\Database\Models\MODELNAME;

class CLASSNAME
{
public static function findAllByColumn(string $column)
{
return MODELNAME::findBy('column', $column)->latest()->getAll();
}
}

0 comments on commit e96e32c

Please sign in to comment.