Skip to content

Commit

Permalink
Merge pull request #55 from v1p3r75/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
v1p3r75 authored May 21, 2024
2 parents 0a320d1 + 5786cef commit a9fc70e
Show file tree
Hide file tree
Showing 46 changed files with 832 additions and 208 deletions.
46 changes: 27 additions & 19 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Floky\Container\Container;
use Floky\Exceptions\Code;
use Floky\Exceptions\NotFoundException;
use Floky\Facades\Config;
use Floky\Facades\Security;
use Floky\Config\Config;
use Floky\Auth\Security;
use Floky\Http\Controllers\Controller;
use Floky\Http\Kernel;
use Floky\Http\Middlewares\Middlewares;
Expand Down Expand Up @@ -105,9 +105,12 @@ public function run()

$httpKernel = self::getHttpKernel();

$request = $this->runMiddlewares($httpKernel->getAllMiddlewares(), $this->request);
$return = $this->runMiddlewares($httpKernel->getAllMiddlewares(), $this->request);

return $this->dispatch($request, $httpKernel);
if ($return instanceof Request) {

return $this->dispatch($this->request, $httpKernel);
}
}


Expand All @@ -134,24 +137,27 @@ public static function getHttpKernel()
private function loadAppRoutes(): void
{

if(Config::get('app.providers.routing.attributes_in_controllers')){
if (Config::get('app.providers.routing.attributes_in_controllers') || true) {

$this->loadRouteInControllers();
}

$kernel = self::getHttpKernel();
if (Config::get('app.providers.routing.routing_in_files') || false) {

$kernel = self::getHttpKernel();

$path = app_routes_path();
$path = app_routes_path();

foreach ($kernel->getRoutesGroup() as $group) {
foreach ($kernel->getRoutesGroup() as $group) {

$group_file = $path . $group . ".php";
$group_file = $path . $group . ".php";

if (file_exists($group_file)) {
if (file_exists($group_file)) {

require_once $group_file;
} else
throw new NotFoundException("'$group' is registered but its file cannot be found. Make sure to create its file in " . app_routes_path(), Code::FILE_NOT_FOUND);
require_once $group_file;
} else
throw new NotFoundException("'$group' is registered but its file cannot be found. Make sure to create its file in " . app_routes_path(), Code::FILE_NOT_FOUND);
}
}
}

Expand Down Expand Up @@ -197,15 +203,16 @@ private function getControllers(): array

$class = $namespace . pathinfo($file, PATHINFO_FILENAME);

if (pathinfo($file, PATHINFO_EXTENSION) === 'php' &&
if (
pathinfo($file, PATHINFO_EXTENSION) === 'php' &&
class_exists($class) &&
is_subclass_of($class, Controller::class))

return $class;
is_subclass_of($class, Controller::class)
)

return $class;
}, $files);

return array_filter($files, fn($file) => !is_null($file));
return array_filter($files, fn ($file) => !is_null($file));
}

private function getNamespaceFromPath(string $path): string
Expand Down Expand Up @@ -272,13 +279,14 @@ public function handleError(int $errno, string $errstr, string $errfile, int $er
public function handleException(Exception | Error $err)
{

$exceptsInProduction = [Code::PAGE_NOT_FOUND, Code::UNAUTHORIZED, Code::APP_DOWN];
$exceptsInProduction = [Code::FORBIDDEN, Code::PAGE_NOT_FOUND, Code::UNAUTHORIZED, Code::APP_DOWN];

$currentEnv = Config::get('app.environment') ?? Application::PRODUCTION;

$template = match ($err->getCode()) {

Code::PAGE_NOT_FOUND => 'templates.404',
Code::FORBIDDEN, => 'templates.403',
Code::UNAUTHORIZED => 'templates.401',
Code::APP_DOWN => 'templates.maintenance',

Expand Down
60 changes: 60 additions & 0 deletions src/Auth/Auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Floky\Auth;
use Floky\Collections\Collection;
use Floky\Config\Config;
use Floky\Session\Session;

class Auth
{

const USER_KEY = '_user';

public static function login(
string $id,
string $password,
array $options = ['id' => 'email', 'password' => 'password']
): bool {

$model = Config::get('auth.model');

if (!class_exists($model)) {

throw new \Exception('Auth model not found : ' . $model);
}

$model = new $model;

if ($result = $model->where([$options['id'] => $id])->first()) {

if (Security::check($password, $result[$options['password']])) {

unset($result[$options['password']]);
Session::set(self::USER_KEY, new Collection($result));

return true;
}
}

return false;
}

public static function user() {

return Session::get(self::USER_KEY) ?? null;
}

public static function logout(bool $redirect = true, string $redirect_to = '') {

Session::set(self::USER_KEY, null);

if ($redirect) {

$home = $redirect_to == '' ? \App\Providers\AppServiceProvider::HOME : $redirect_to;

header("Location: $home");
}

return true;
}
}
5 changes: 3 additions & 2 deletions src/Facades/Security.php → src/Auth/Security.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace Floky\Facades;
namespace Floky\Auth;

use Floky\Session\Session;

class Security extends Facades
class Security
{
public static function check(string $password, string $hached_password): bool
{
Expand Down
4 changes: 2 additions & 2 deletions src/Facades/Config.php → src/Config/Config.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Floky\Facades;
namespace Floky\Config;

use Dotenv\Dotenv;
use Floky\Exceptions\Code;
use Floky\Exceptions\NotFoundException;

class Config extends Facades
class Config
{


Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/DownApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Floky\Console\Commands;

use Floky\Console\Command;
use Floky\Facades\Config;
use Floky\Config\Config;
use Symfony\Component\Console\Attribute\AsCommand;
use \Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down
12 changes: 10 additions & 2 deletions src/Console/Commands/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use \Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
Expand All @@ -19,15 +20,22 @@ protected function configure()
{

$this
->addArgument('name', InputArgument::REQUIRED, 'The name of the controller');
->addArgument('name', InputArgument::REQUIRED, 'The name of the controller')
->addOption('resource', 'r', InputOption::VALUE_NONE, 'Make resource controller');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{

$name = $input->getArgument('name');
$name = $input->getArgument('name');
$isResource = $input->getOption('resource');
$path = app_controllers_path( $name . '.php');

if ($isResource) {

return $this->make($output, $name, $path, 'controller-resource.make');
}

return $this->make($output, $name, $path, 'controller.make');
}
}
2 changes: 1 addition & 1 deletion src/Console/Commands/UpApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Floky\Console\Commands;

use Floky\Console\Command;
use Floky\Facades\Config;
use Floky\Config\Config;
use Symfony\Component\Console\Attribute\AsCommand;
use \Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Floky\Console;

use Floky\Application;
use Floky\Facades\Config;
use Floky\Config\Config;
use Symfony\Component\Console\Application as ConsoleApplication;

class Console extends ConsoleApplication
Expand Down
34 changes: 34 additions & 0 deletions src/Console/Stubs/controller-resource.make.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Http\Controllers;

class {{name}} extends Controller
{

public function index() {

}

public function show($id) {

}

public function create() {

}

public function edit($id) {

}

public function update() {

}

public function delete() {

}



}
12 changes: 10 additions & 2 deletions src/Console/Stubs/middleware.make.stub
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
<?php

namespace App\Http\Middlewares;

use Closure;
use Floky\Http\Middlewares\MiddlewareInterface;
use Floky\Http\Requests\Request;

class {{name}} implements MiddlewareInterface
{
public function handle(Request $request): Request {

return $request;

public function handle(Request $request, Closure $next)
{

// handle request

return $next($request);

}
}
28 changes: 22 additions & 6 deletions src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace Floky\Container;

use Closure;
use Psr\Container\ContainerInterface;
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;

class Container
class Container implements ContainerInterface
{
private array $services = [];

Expand All @@ -25,19 +26,19 @@ public static function getInstance(): self {
return self::$instance;
}

public function get($id)
public function get(string $id)
{

if (isset($this->services[$id])) {
if ($this->has($id)) {

return $this->services[$id];
}
return $this->resolveDependencies($id);
}

public function set($id, callable $definition)
public function set(string $id, callable $definition)
{
if (isset($this->services[$id])) {
if ($this->has($id)) {

return false;
}
Expand All @@ -47,10 +48,25 @@ public function set($id, callable $definition)
return true;
}

public function has(string $id): bool
{

return isset($this->services[$id]);
}

private function resolveDependencies($id)
{
$reflection = new ReflectionClass($id);

if ($reflection->isInterface()) {

if ($this->has($id))

return $this->services[$id];

throw new \Exception("Interface [$id] have not a binding class in service container");
}

$constructor = $reflection->getConstructor();

if (!$constructor) {
Expand All @@ -75,7 +91,7 @@ private function resolveParameters(ReflectionClass | ReflectionMethod | Reflecti

if (!$type || $type->isBuiltin()) {

$dependencies[] = null;
$dependencies[] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null;

} else {

Expand Down
10 changes: 10 additions & 0 deletions src/Events/EventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Floky\Events;


interface Event
{

public function handle(Event $event): void;
}
Loading

0 comments on commit a9fc70e

Please sign in to comment.