Skip to content

Commit

Permalink
update: console commands
Browse files Browse the repository at this point in the history
added console configuration file
added routes list console command
  • Loading branch information
eliseekn committed Oct 23, 2021
1 parent 7d5dddd commit 454cfa5
Show file tree
Hide file tree
Showing 24 changed files with 184 additions and 90 deletions.
5 changes: 3 additions & 2 deletions app/Database/Factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

namespace App\Database\Factories;

use App\Database\Models\User;
use Carbon\Carbon;
use Core\Database\Factory;
use App\Database\Models\User;

class UserFactory extends Factory
{
Expand All @@ -26,7 +27,7 @@ public function data()
'name' => $this->faker->name(),
'email' => $this->faker->unique()->email(),
'password' => hash_pwd('password'),
'verified' => true,
'email_verified' => Carbon::now(),
'role' => User::ROLE_USER
];
}
Expand Down
2 changes: 1 addition & 1 deletion app/Database/Migrations/UsersTable_20210403034738.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function create()
->addString('name')
->addString('email')->unique()
->addString('password')
->addBoolean('verified')->default(true)
->addBoolean('email_verified')->nullable()
->addString('role')->default(User::ROLE_USER)
->migrate();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/EmailVerificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function verify(Request $request, Response $response, Mailer $mailer)

$token->delete();

$user = UserActions::update(['verified' => true], $request->queries('email'));
$user = UserActions::update(['email_verified' => Carbon::now()], $request->queries('email'));

if (!$user) {
Alert::default(__('account_not_found'))->error();
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use App\Mails\TokenMail;
use App\Database\Models\Token;
use App\Http\Actions\UserActions;
use App\Http\Validators\AuthRequest;
use App\Http\Validators\Auth\LoginValidator;
use Core\Http\Response\Response;
use Core\Support\Mailer\Mailer;

Expand Down Expand Up @@ -64,7 +64,7 @@ public function reset(Request $request, Response $response)

public function update(Request $request, Response $response)
{
AuthRequest::make($request->inputs())->redirectBackOnFail($response);
LoginValidator::make($request->inputs())->redirectBackOnFail($response);
$user = UserActions::update(['password' => $request->password], $request->email);

if (!$user) {
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function index(Request $request, Response $response)
public function register(Request $request, Mailer $mailer, Response $response)
{
RegisterValidator::make($request->inputs())->redirectBackOnFail($response);
$user = UserActions::create($request->inputs());
$user = UserActions::create($request->except('csrf_token'));

if (!config('security.auth.email_verification')) {
WelcomeMail::send($mailer, $user->email, $user->name);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middlewares/AccountPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AccountPolicy
public function handle(Request $request, Response $response)
{
if (config('security.auth.email_verification')) {
if (!Auth::get('verified')) {
if (is_null(Auth::get('email_verified'))) {
Alert::default(__('email_not_verifed'))->error();
$response->redirect()->to('login')->intended($request->fullUri())->go();
}
Expand Down
2 changes: 1 addition & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ function handleExceptions($e)
}

Config::loadEnv();
Route::load();
Route::loadFiles();
54 changes: 54 additions & 0 deletions config/console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

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

/**
* Console commands
*/

return [
'core' => [
new \Core\Console\Database\Create(),
new \Core\Console\Database\Delete(),
new \Core\Console\Database\Show(),
new \Core\Console\Database\Query(),
new \Core\Console\Database\Migrations\Delete(),
new \Core\Console\Database\Migrations\Status(),
new \Core\Console\Database\Migrations\Run(),
new \Core\Console\Database\Migrations\Reset(),
new \Core\Console\Database\Seeds(),

new \Core\Console\Make\Migration(),
new \Core\Console\Make\Model(),
new \Core\Console\Make\Controller(),
new \Core\Console\Make\Validator(),
new \Core\Console\Make\Seed(),
new \Core\Console\Make\Factory(),
new \Core\Console\Make\View(),
new \Core\Console\Make\Mail(),
new \Core\Console\Make\Middleware(),
new \Core\Console\Make\Console(),
new \Core\Console\Make\Password(),
new \Core\Console\Make\Helper(),
new \Core\Console\Make\Test(),
new \Core\Console\Make\Actions(),

new \Core\Console\App\Setup(),
new \Core\Console\App\EncryptionKey(),
new \Core\Console\App\Environnement(),

new \Core\Console\Cache(),
new \Core\Console\Server(),
new \Core\Console\Tests(),
new \Core\Console\Logs(),
new \Core\Console\Routes(),
],

'app' => [
//
]
];
2 changes: 1 addition & 1 deletion config/storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
'mails' => absolute_path('app.Mails'),
'helpers' => absolute_path('app.Helpers'),
'tests' => absolute_path('tests'),
'commands' => absolute_path('app.Commands'),
'console' => absolute_path('app.Console'),
'sqlite' => absolute_path('storage.sqlite'),
'actions' => absolute_path('app.Http.Actions')
];
36 changes: 1 addition & 35 deletions console
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,5 @@ require 'vendor/autoload.php';
require_once 'bootstrap.php';

$console = new \Symfony\Component\Console\Application();

$console->add(new \Core\Console\Database\Create());
$console->add(new \Core\Console\Database\Delete());
$console->add(new \Core\Console\Database\Show());
$console->add(new \Core\Console\Database\Query());
$console->add(new \Core\Console\Database\Migrations\Delete());
$console->add(new \Core\Console\Database\Migrations\Status());
$console->add(new \Core\Console\Database\Migrations\Run());
$console->add(new \Core\Console\Database\Migrations\Reset());
$console->add(new \Core\Console\Database\Seeds());

$console->add(new \Core\Console\Make\Migration());
$console->add(new \Core\Console\Make\Model());
$console->add(new \Core\Console\Make\Controller());
$console->add(new \Core\Console\Make\Validator());
$console->add(new \Core\Console\Make\Seed());
$console->add(new \Core\Console\Make\Factory());
$console->add(new \Core\Console\Make\View());
$console->add(new \Core\Console\Make\Mail());
$console->add(new \Core\Console\Make\Middleware());
$console->add(new \Core\Console\Make\Cmd());
$console->add(new \Core\Console\Make\Password());
$console->add(new \Core\Console\Make\Helper());
$console->add(new \Core\Console\Make\Test());
$console->add(new \Core\Console\Make\Actions());

$console->add(new \Core\Console\App\Setup());
$console->add(new \Core\Console\App\EncryptionKey());
$console->add(new \Core\Console\App\Environnement());

$console->add(new \Core\Console\Cache());
$console->add(new \Core\Console\Server());
$console->add(new \Core\Console\Tests());
$console->add(new \Core\Console\Logs());

$console->addCommands(array_merge(config('console.core'), config('console.app')));
$console->run();
2 changes: 1 addition & 1 deletion core/Console/Database/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace Core\Console\Database;

use Core\Database\Connection\Connection;
use Core\Support\Storage;
use Core\Database\Connection\Connection;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down
16 changes: 8 additions & 8 deletions core/Console/Make/Cmd.php → core/Console/Make/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@
/**
* Create new console command
*/
class Cmd extends Command
class Console extends Command
{
protected static $defaultName = 'make:cmd';
protected static $defaultName = 'make:console';

protected function configure()
{
$this->setDescription('Create new console command');
$this->addArgument('cmd', InputArgument::REQUIRED, 'The command class name');
$this->addOption('name', null, InputOption::VALUE_REQUIRED, 'The command name');
$this->addOption('description', null, InputOption::VALUE_REQUIRED, 'The command description (inside "")');
$this->addOption('namespace', null, InputOption::VALUE_OPTIONAL, 'Specify namespace (base: App\Commands)');
$this->addArgument('console', InputArgument::REQUIRED, 'The console class name');
$this->addOption('command', null, InputOption::VALUE_REQUIRED, 'The console name');
$this->addOption('description', null, InputOption::VALUE_REQUIRED, 'The console description (inside "")');
$this->addOption('namespace', null, InputOption::VALUE_OPTIONAL, 'Specify namespace (base: App\Console)');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
list($name, $class) = Make::generateClass($input->getArgument('cmd'), '', true);
list($name, $class) = Make::generateClass($input->getArgument('console'), '', true);

if (!Make::createCommand($input->getArgument('cmd'), $input->getOption('name'), $input->getOption('description'), $input->getOption('namespace'))) {
if (!Make::createConsole($input->getArgument('console'), $input->getOption('command'), $input->getOption('description'), $input->getOption('namespace'))) {
$output->writeln('<fg=yellow>Failed to create command "' . $class . '"</fg>');
}

Expand Down
4 changes: 3 additions & 1 deletion core/Console/Make/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand All @@ -24,6 +25,7 @@ protected function configure()
{
$this->setDescription('Create new model factory');
$this->addArgument('factory', InputArgument::REQUIRED|InputArgument::IS_ARRAY, 'The name of model factory table (separated by space if many)');
$this->addOption('namespace', null, InputOption::VALUE_OPTIONAL, 'Specify namespace (base: App\Database\Factories)');
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand All @@ -33,7 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($factorys as $factory) {
list($name, $class) = Make::generateClass($factory, 'factory', true, true);

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

Expand Down
49 changes: 31 additions & 18 deletions core/Console/Make/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static function createController(string $controller, ?string $namespace =
$storage = Storage::path(config('storage.controllers'));

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

if (!$storage->writeFile($class . '.php', $data)) {
Expand All @@ -121,7 +121,7 @@ public static function createModel(string $model, ?string $namespace = null)
$storage = Storage::path(config('storage.models'));

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

if (!$storage->writeFile(self::fixPluralTypo($class, true) . '.php', $data)) {
Expand Down Expand Up @@ -161,16 +161,23 @@ public static function createSeed(string $seed)
return true;
}

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

$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);

if (!Storage::path(config('storage.factories'))->writeFile(self::fixPluralTypo($class, true) . '.php', $data)) {
$storage = Storage::path(config('storage.factories'));

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

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

Expand All @@ -191,17 +198,23 @@ public static function createHelper(string $helper)
return true;
}

public static function createTest(string $test, bool $unit_test)
public static function createTest(string $test, bool $unit_test, ?string $namespace = null)
{
list($name, $class) = self::generateClass($test, 'test', true);

$data = $unit_test
? self::stubs()->addPath('tests')->readFile('UnitTest.stub')
: self::stubs()->addPath('tests')->readFile('ApplicationTest.stub');
$storage = Storage::path(config('storage.tests'));

if ($unit_test) {
$data = self::stubs()->addPath('tests')->readFile('UnitTest.stub');
$storage = $storage->addPath('Unit')->addPath(str_replace('\\', '/', $namespace));
} else {
$data = self::stubs()->addPath('tests')->readFile('ApplicationTest.stub');
$storage = $storage->addPath('Application')->addPath(str_replace('\\', '/', $namespace));
}

$data = str_replace('CLASSNAME', $class, $data);

if (!Storage::path(config('storage.tests'))->addPath($unit_test ? 'Unit' : 'Application')->writeFile($class . '.php', $data)) {
if (!$storage->writeFile($class . '.php', $data)) {
return false;
}

Expand All @@ -219,7 +232,7 @@ public static function createValidator(string $validator, ?string $namespace = n
$storage = Storage::path(config('storage.validators'));

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

if (!$storage->writeFile($class . '.php', $data)) {
Expand Down Expand Up @@ -288,20 +301,20 @@ public static function createView(?string $view, ?string $layout)
return true;
}

public static function createCommand(string $cmd, string $name, string $description, ?string $namespace = null)
public static function createConsole(string $console, string $command, string $description, ?string $namespace = null)
{
list($_name, $class) = self::generateClass($cmd, '', true);
list($_name, $class) = self::generateClass($console, '', true);

$data = self::stubs()->readFile('Command.stub');
$data = self::addNamespace($data, 'App\Commands', $namespace);
$data = self::stubs()->readFile('Console.stub');
$data = self::addNamespace($data, 'App\Console', $namespace);
$data = str_replace('CLASSNAME', $class, $data);
$data = str_replace('COMMANDNAME', $name, $data);
$data = str_replace('COMMANDNAME', $command, $data);
$data = str_replace('COMMANDDESCPTION', $description, $data);

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

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

if (!$storage->writeFile($class . '.php', $data)) {
Expand All @@ -324,7 +337,7 @@ public static function createActions(string $model, ?string $namespace = null)
$storage = Storage::path(config('storage.actions'));

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

if (!$storage->writeFile($class . '.php', $data)) {
Expand Down
3 changes: 2 additions & 1 deletion core/Console/Make/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected function configure()
$this->setDescription('Create new PHPUnit test case');
$this->addArgument('test', InputArgument::REQUIRED|InputArgument::IS_ARRAY, 'The name of test (separated by space if many)');
$this->addOption('unit', 'u', InputOption::VALUE_NONE, 'Setup for unit test');
$this->addOption('subdir', null, InputOption::VALUE_OPTIONAL, 'Specify subdirectory');
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand All @@ -35,7 +36,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($tests as $test) {
list($name, $class) = Make::generateClass($test, 'test', true);

if (!Make::createTest($test, $input->getOption('unit'))) {
if (!Make::createTest($test, $input->getOption('unit'), $input->getOption('subdir'))) {
$output->writeln('<fg=yellow>Failed to create test "' . $class . '"</fg>');
}

Expand Down
Loading

0 comments on commit 454cfa5

Please sign in to comment.