Skip to content

Commit

Permalink
Merge pull request #1 from arif98741/dev
Browse files Browse the repository at this point in the history
Class and Method Loaded Using ReflectionMethod
  • Loading branch information
arif98741 authored May 13, 2024
2 parents a304c3e + d293c51 commit 4941c70
Show file tree
Hide file tree
Showing 25 changed files with 115 additions and 84 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
.idea
*.log
*.iml
*.zip
*.iml
*.log
node_modules
composer.lock
34 changes: 0 additions & 34 deletions App/System/Exception/MvcException.php

This file was deleted.

6 changes: 0 additions & 6 deletions App/config/environment.php

This file was deleted.

File renamed without changes.
19 changes: 8 additions & 11 deletions App/Controllers/Home.php → app/Controllers/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,29 @@
use App\Models\Book;
use app\System\Controller;
use App\System\Helpers\FormHelper;
use App\System\Http\Request;
use App\System\Libraries\Database\Database;

class Home extends Controller
{
/**
* Index Method for showing homepage and base
* @param Request $request
* @param $id
* @param string $name
*/
public function index()
public function index(Request $request, $id, string $name)
{
$hello = 'helo';
$this->view('home/index');
}

/**
* Ajax Form Submit
*/
public function ajax_submission()
{
$form = $this->helpers('FormHelper');
$form->validate($_POST);
}

public function data()
public function data(Request $request, $id,string $name)
{
$book = new Book;
$book->table('users');
$book->select(['mobile','pincode']);
$book->select(['mobile', 'pincode']);
$book->get();
}

Expand Down
File renamed without changes.
File renamed without changes.
47 changes: 37 additions & 10 deletions App/System/Application.php → app/System/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

use App\System\Exception\MethodNotFoundException;
use App\System\exception\MvcException;
use ArgumentCountError;
use App\System\Http\Request;
use Exception;
use ReflectionMethod;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run as WhoopsRun;

Expand All @@ -33,16 +34,19 @@ class Application
*/
private array $params = [];

public function __construct()
{

}

private function initialize()
/**
* @throws Exception
*/
private function initialize(): void
{
$controller = new Controller();
$controller->config('environment');
$controller->config('database');
try {
$controller->config('environment');
$controller->config('database');
} catch (\Throwable $e) {
$this->handleException($e);
}
}

private function handleRequest()
Expand Down Expand Up @@ -79,7 +83,7 @@ private function callMethod($method, $url)
try {

if (!method_exists($this->controller, $method)) {
throw new MethodNotFoundException("Method $method not found from " . $this->getClassName());
throw new MethodNotFoundException("Method $method not found in controller " . $this->getClassName());
}
} catch (Exception $exception) {
$this->handleException($exception);
Expand All @@ -90,7 +94,27 @@ private function callMethod($method, $url)
$this->params = $url ? array_values($url) : [];

try {
call_user_func_array([$this->controller, $this->method], $this->params);

$reflectionMethod = new ReflectionMethod($this->controller, $this->method);
$parameters = $reflectionMethod->getParameters();

$passRequest = false;
if (!empty($parameters)) {
$firstParameter = $parameters[0]; // Assuming first parameter
$type = $firstParameter->getType();
if ($type !== null && !$type->isBuiltin() && $type->getName() === Request::class) {
$passRequest = true;
}
}

// If typehint class(App\System\Http\Request) exists in the first param, pass new Request as the second param
if ($passRequest) {
$reflectionMethod->invoke(new $this->controller, new Request(), ...$this->params);
} else {
// Otherwise, pass only the params
$reflectionMethod->invoke(new $this->controller, ...$this->params);
}

} catch (\Throwable $exception) {
$this->handleException($exception);
}
Expand Down Expand Up @@ -135,6 +159,9 @@ private function parseUrl()
];
}

/**
* @throws Exception
*/
public function run()
{
$this->initialize();
Expand Down
24 changes: 9 additions & 15 deletions App/System/Controller.php → app/System/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\System;

use App\System\exception\MvcException;
use App\System\Helpers\AppHelper;

class Controller
{
Expand Down Expand Up @@ -45,7 +46,7 @@ public function model($model)
* @param $view
* @param array $data
*/
public function view($view, $data = [])
public function view($view, array $data = [])
{
try {

Expand Down Expand Up @@ -115,28 +116,21 @@ public function helpers($helper = '')

/**
* @param $config
* @throws \Exception
*/
public function config($config)
{
try {

if (file_exists('../App/config/' . $config . '.php')) {
$configurationFilePath = AppHelper::getAppPath() . '/../../../config/' . $config. '.php';

require_once '../App/config/' . $config . '.php';
} else {
try {

throw new MvcException("config file '$config' does not exist");
if (file_exists($configurationFilePath)) {
require_once $configurationFilePath;
}

} catch (MvcException $exception) {

echo '<pre>';
print_r($exception->showException([
debug_print_backtrace()
]));

} catch (\Exception $exception) {
throw new MvcException($exception->getMessage());
}
}


}
16 changes: 16 additions & 0 deletions app/System/Exception/MvcException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\System\Exception;

use Exception;
use Throwable;

class MvcException extends Exception
{
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}


}
13 changes: 13 additions & 0 deletions app/System/Helpers/AppHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


namespace App\System\Helpers;


class AppHelper
{
public static function getAppPath()
{
return realpath(__DIR__);
}
}
File renamed without changes.
18 changes: 18 additions & 0 deletions app/System/Http/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\System\Http;

use Symfony\Component\HttpFoundation\Request as SymphonyHttpRequestFoundation;

class Request extends SymphonyHttpRequestFoundation
{
public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
{
parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
}

public function all()
{
return new SymphonyHttpRequestFoundation;
}
}
File renamed without changes.
1 change: 0 additions & 1 deletion App/System/Model.php → app/System/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public function get()
{
try {

dd($this);
if ($this->statement == null) {

throw new MvcDatabaseConnectionException('someting wrong');
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 0 additions & 4 deletions App/views/home/index.php → app/views/home/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,5 @@
<p class="text-center">Developed BY: Ariful Islam</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf"
crossorigin="anonymous"></script>
</body>
</html>
File renamed without changes.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"autoload": {
"psr-4": {
"App\\": "App/"
"App\\": "app/"
},
"files": [

Expand All @@ -20,7 +20,8 @@
"minimum-stability": "stable",
"require": {
"ext-pdo": "*",
"php": ">=8.1"
"php": ">=8.1",
"symfony/http-foundation": "^7.0"
},
"require-dev": {
"filp/whoops": "^2.15",
Expand Down
2 changes: 1 addition & 1 deletion App/config/database.php → config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
define('HOST', "localhost");
define('DB_USER', "root");
define('DB_PASS', "");
define('DB_NAME', "mvc_app");
define('DB_NAME', "laravel");
4 changes: 4 additions & 0 deletions config/environment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

const MVC_DEBUG = 1;
const APP_URL = 'http://localhost/mvc-pattern/';
1 change: 1 addition & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
use App\System\Application;

$app = new Application();

$app->run();

0 comments on commit 4941c70

Please sign in to comment.