Skip to content

Commit

Permalink
Use PSR 4
Browse files Browse the repository at this point in the history
  • Loading branch information
buihanh2304 committed Jul 13, 2023
1 parent f976758 commit 05b3484
Show file tree
Hide file tree
Showing 29 changed files with 146 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace App\Controllers;

use System\Classes\Controller;

class HomeController extends Controller
{
public function index()
Expand Down
15 changes: 12 additions & 3 deletions app/controllers/User.php → app/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace App\Controllers;

use App\Libraries\UserLibrary;
use App\Models\User;
use System\Classes\Captcha;
use System\Classes\Container;
use System\Classes\Controller;

class UserController extends Controller
{
private UserModel $userModel;
private User $userModel;
private UserLibrary $userLibrary;

function __construct()
{
parent::__construct();
$this->userModel = $this->load->model('User');
$this->userLibrary = $this->load->library('User');

$this->userModel = new User;
$this->userLibrary = new UserLibrary;
}

public function logout()
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions app/libraries/User.php → app/Libraries/UserLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace App\Libraries;

class UserLibrary
{
public function validateEmail($email)
Expand Down
6 changes: 5 additions & 1 deletion app/models/User.php → app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

class UserModel extends Model
namespace App\Models;

use System\Classes\Model;

class User extends Model
{
public function logout()
{
Expand Down
25 changes: 25 additions & 0 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Providers;

use System\Classes\Container;
use System\Classes\Router;
use System\Interfaces\ServiceProviderInterface;

class RouteServiceProvider implements ServiceProviderInterface
{
protected $namespace = 'App\\Controllers\\';

public function register()
{
$router = Container::get(Router::class);
$router->setNamespace($this->namespace);

$this->loadRoutes();
}

protected function loadRoutes()
{
require_once ROOT . 'app' . DS . 'routes.php';
}
}
6 changes: 6 additions & 0 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
*/

/** @var Router */

use App\Controllers\HomeController;
use System\Classes\Container;
use System\Classes\Router;
use System\Classes\Template;

$router = Container::get(Router::class);

$router->add('/', 'HomeController@index');
Expand Down
14 changes: 0 additions & 14 deletions app/services/RouteService.php

This file was deleted.

15 changes: 14 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
{
"name": "buihanh2304/simple-php-mvc-framework",
"description": "A simple PHP MVC Framework",
"keywords": ["php", "mvc", "framework"],
"keywords": [
"php",
"mvc",
"framework"
],
"license": "MIT",
"config": {
"sort-packages": true
},
"require": {
"php": "^8.0",
"league/plates": "3.*"
},
"autoload": {
"files": [
"system/functions.php"
],
"psr-4": {
"App\\": "app/",
"System\\": "system/"
}
}
}
7 changes: 5 additions & 2 deletions configs/autoload/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

use App\Providers\RouteServiceProvider;
use System\Providers\CaptchaServiceProvider;

return [
RouteService::class,
CaptchaService::class,
RouteServiceProvider::class,
CaptchaServiceProvider::class,
];
3 changes: 3 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

use System\Classes\Container;
use System\Classes\Kernel;
use System\Classes\Request;

define('_MVC_START', microtime(true));

Expand Down
3 changes: 3 additions & 0 deletions system/classes/Auth.php → system/Classes/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

class Auth
{
public $id = 0;
Expand All @@ -32,6 +34,7 @@ class Auth
public function __construct()
{
$this->db = Container::get(DB::class);

$this->authorize();
}

Expand Down
2 changes: 2 additions & 0 deletions system/classes/Captcha.php → system/Classes/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

class Captcha
{
private $font = 'monofont.ttf';
Expand Down
2 changes: 2 additions & 0 deletions system/classes/Config.php → system/Classes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

class Config
{
private $configs;
Expand Down
2 changes: 2 additions & 0 deletions system/classes/Container.php → system/Classes/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

class Container
{
private static $instances = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

class Controller
{
protected Loader $load;
protected Auth $auth;
protected Request $request;
protected Config $config;
protected Template $view;

function __construct()
{
$this->load = Container::get(Loader::class);
$this->auth = Container::get(Auth::class);
$this->request = Container::get(Request::class);
$this->config = Container::get(Config::class);
Expand Down
5 changes: 5 additions & 0 deletions system/classes/DB.php → system/Classes/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

use PDO;
use PDOException;

class DB
{
public function __invoke()
Expand Down
3 changes: 3 additions & 0 deletions system/classes/Kernel.php → system/Classes/Kernel.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace System\Classes;

class Kernel
{
public function run(Request $request)
Expand Down Expand Up @@ -48,6 +50,7 @@ protected function matchRoute(Request $request, Router $router)
exit;
}
}

/** @var Template */
$view = Container::get(Template::class);

Expand Down
6 changes: 4 additions & 2 deletions system/classes/Model.php → system/Classes/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

use PDO;

class Model
{
protected Loader $load;
protected PDO $db;
protected Config $config;

function __construct()
{
$this->db = Container::get(DB::class);
$this->config = Container::get(Config::class);
$this->load = Container::get(Loader::class);
}
}
2 changes: 2 additions & 0 deletions system/classes/Request.php → system/Classes/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

class Request
{
private $ip;
Expand Down
13 changes: 13 additions & 0 deletions system/classes/Router.php → system/Classes/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

class Router
{
private $basePath = '';

private $routes = [];

protected $namespace;

private $allowedMethods = [];

private $patternMatchers = [
Expand Down Expand Up @@ -191,6 +195,15 @@ private function processHandler($handler)
return $handler;
}

if ($this->namespace) {
return $this->namespace . $handler;
}

return $handler;
}

public function setNamespace(string $namespace)
{
$this->namespace = $namespace;
}
}
13 changes: 9 additions & 4 deletions system/classes/Template.php → system/Classes/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
// docs: https://github.com/buihanh2304/simple-php-mvc-framework/wiki
*/

namespace System\Classes;

use League\Plates\Engine;
use League\Plates\Extension\Asset;

class Template
{
private $plates;
private Engine $plates;

private $global = [];
private $data = [];
Expand All @@ -21,8 +26,8 @@ function __construct()
/** @var Auth */
$auth = Container::get(Auth::class);

$plates = new League\Plates\Engine(ROOT . 'templates');
$plates->loadExtension(new League\Plates\Extension\Asset(ROOT . 'public', true));
$plates = new Engine(ROOT . 'templates');
$plates->loadExtension(new Asset(ROOT . 'public', true));
$plates->addData([
'isLogin' => $auth->isLogin,
'user' => $auth->user,
Expand All @@ -33,7 +38,7 @@ function __construct()
$this->plates = $plates;
}

public function getEngine()
public function getEngine(): Engine
{
return $this->plates;
}
Expand Down
8 changes: 8 additions & 0 deletions system/Interfaces/ServiceProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace System\Interfaces;

interface ServiceProviderInterface
{
public function register();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?php

class CaptchaService implements ServiceInterface
namespace System\Providers;

use System\Classes\Captcha;
use System\Classes\Container;
use System\Classes\Router;
use System\Interfaces\ServiceProviderInterface;

class CaptchaServiceProvider implements ServiceProviderInterface
{
public function register()
{
Expand Down
Loading

0 comments on commit 05b3484

Please # to comment.