Skip to content

Commit

Permalink
feat: added factory, controller, seeder, actions, migration on make m…
Browse files Browse the repository at this point in the history
…odel console command
  • Loading branch information
eliseekn committed Oct 23, 2021
1 parent 391befa commit 7d5dddd
Show file tree
Hide file tree
Showing 17 changed files with 196 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,22 @@
use Core\Support\Auth;
use Core\Support\Alert;
use Core\Support\Session;
use App\Mails\WelcomeMail;
use Core\Support\Mailer\Mailer;
use Core\Http\Response\Response;
use App\Http\Actions\UserActions;
use App\Http\Validators\AuthRequest;
use App\Http\Validators\RegisterUser;
use App\Http\Validators\Auth\LoginValidator;

/**
* Manage user authentication
*/
class AuthController
class LoginController
{
public function login(Request $request, Response $response)
public function index(Request $request, Response $response)
{
if (!Auth::check($request)) $response->view('auth.login');

$uri = !Session::has('intended') ? Auth::HOME : Session::pull('intended');
$response->redirect()->to($uri)->go();
}

public function signup(Request $request, Response $response)
{
if (!Auth::check($request)) $response->view('auth.signup');

$uri = !Session::has('intended') ? Auth::HOME : Session::pull('intended');
$response->redirect()->to($uri)->go();
}

public function authenticate(Request $request, Response $response)
{
AuthRequest::make($request->inputs())->redirectBackOnFail($response);
LoginValidator::make($request->inputs())->redirectBackOnFail($response);

if (Auth::attempt($request->only('email', 'password'), $request->has('remember'))) {
$uri = !Session::has('intended') ? Auth::HOME : Session::pull('intended');
Expand All @@ -59,27 +44,4 @@ public function authenticate(Request $request, Response $response)
Alert::default(__('login_failed'))->error();
$response->redirect()->to('login')->withInputs($request->only('email', 'password'))->withErrors([__('login_failed')])->go();
}

public function register(Request $request, Mailer $mailer, Response $response)
{
RegisterUser::make($request->inputs())->redirectBackOnFail($response);
$user = UserActions::create($request->inputs());

if (!config('security.auth.email_verification')) {
WelcomeMail::send($mailer, $user->email, $user->name);

Alert::default(__('account_created'))->success();
$response->redirect()->to('login')->go();
}

$response->redirect()->to('email/notify')->go();
}

public function logout(Response $response)
{
Auth::forget();

Alert::toast(__('logged_out'))->success();
$response->redirect()->to(Auth::HOME)->go();
}
}
24 changes: 24 additions & 0 deletions app/Http/Controllers/Auth/LogoutController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

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

namespace App\Http\Controllers\Auth;

use Core\Support\Auth;
use Core\Support\Alert;
use Core\Http\Response\Response;

class LogoutController
{
public function __invoke(Response $response)
{
Auth::forget();

Alert::toast(__('logged_out'))->success();
$response->redirect()->to(Auth::HOME)->go();
}
}
45 changes: 45 additions & 0 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

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

namespace App\Http\Controllers\Auth;

use Core\Http\Request;
use Core\Support\Auth;
use Core\Support\Alert;
use Core\Support\Session;
use App\Mails\WelcomeMail;
use Core\Support\Mailer\Mailer;
use Core\Http\Response\Response;
use App\Http\Actions\UserActions;
use App\Http\Validators\Auth\RegisterValidator;

class RegisterController
{
public function index(Request $request, Response $response)
{
if (!Auth::check($request)) $response->view('auth.signup');

$uri = !Session::has('intended') ? Auth::HOME : Session::pull('intended');
$response->redirect()->to($uri)->go();
}

public function register(Request $request, Mailer $mailer, Response $response)
{
RegisterValidator::make($request->inputs())->redirectBackOnFail($response);
$user = UserActions::create($request->inputs());

if (!config('security.auth.email_verification')) {
WelcomeMail::send($mailer, $user->email, $user->name);

Alert::default(__('account_created'))->success();
$response->redirect()->to('login')->go();
}

$response->redirect()->to('email/notify')->go();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* @link https://github.com/eliseekn/tinymvc
*/

namespace App\Http\Validators;
namespace App\Http\Validators\Auth;

use Core\Http\Validator\GUMPValidator;

class AuthRequest extends GUMPValidator
class LoginValidator extends GUMPValidator
{
/**
* Validation rules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* @link https://github.com/eliseekn/tinymvc
*/

namespace App\Http\Validators;
namespace App\Http\Validators\Auth;

use Core\Http\Validator\GUMPValidator;
use Core\Database\Repository;

class RegisterUser extends GUMPValidator
class RegisterValidator extends GUMPValidator
{
/**
* Validation rules
Expand Down
4 changes: 3 additions & 1 deletion core/Console/Make/Actions.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 actions');
$this->addArgument('model', InputArgument::REQUIRED|InputArgument::IS_ARRAY, 'The name of model (separated by space if many)');
$this->addOption('namespace', null, InputOption::VALUE_OPTIONAL, 'Specify namespace (base: App\Http\Actions)');
}

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

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

Expand Down
3 changes: 2 additions & 1 deletion core/Console/Make/Cmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ protected function configure()
$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)');
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion core/Console/Make/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function configure()
{
$this->setDescription('Create new controller');
$this->addArgument('controller', InputArgument::REQUIRED|InputArgument::IS_ARRAY, 'The name of controller (separated by space if many)');
$this->addOption('namespace', null, InputOption::VALUE_OPTIONAL, 'Specify controller namespace');
$this->addOption('namespace', null, InputOption::VALUE_OPTIONAL, 'Specify namespace (base: App\Http\Controllers)');
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand Down
64 changes: 47 additions & 17 deletions core/Console/Make/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ private static function removeUnderscore(string $word)
return $word;
}

private static function addNamespace(string $data, string $base, ?string $namespace = null)
{
return is_null($namespace) ? str_replace('NAMESPACE', $base, $data)
: str_replace('NAMESPACE', "{$base}\\{$namespace}", $data);
}

public static function fixPluralTypo(string $word, bool $remove = false)
{
if (!$remove) {
Expand Down Expand Up @@ -87,35 +93,38 @@ public static function createController(string $controller, ?string $namespace =
list($name, $class) = self::generateClass($controller, 'controller', true, true);

$data = self::stubs()->readFile('Controller.stub');

$data = is_null($namespace)
? str_replace('NAMESPACE', 'App\Http\Controllers', $data)
: str_replace('NAMESPACE', 'App\Http\Controllers\\' . ucfirst($namespace), $data);

$data = self::addNamespace($data, 'App\Http\Controllers', $namespace);
$data = str_replace('CLASSNAME', $class, $data);

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

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

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

return true;
}

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

$data = self::stubs()->readFile('Model.stub');
$data = self::addNamespace($data, 'App\Database\Models', $namespace);
$data = str_replace('CLASSNAME', self::fixPluralTypo($class, true), $data);
$data = str_replace('TABLENAME', $name, $data);

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

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

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

Expand Down Expand Up @@ -199,14 +208,21 @@ public static function createTest(string $test, bool $unit_test)
return true;
}

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

$data = self::stubs()->readFile('Validator.stub');
$data = self::addNamespace($data, 'App\Http\Validators', $namespace);
$data = str_replace('CLASSNAME', $class, $data);

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

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

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

Expand Down Expand Up @@ -272,32 +288,46 @@ public static function createView(?string $view, ?string $layout)
return true;
}

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

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

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

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

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

return true;
}

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

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

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

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

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

Expand Down
Loading

0 comments on commit 7d5dddd

Please sign in to comment.