From 2b325d39ded72c0ed4aea9c62d0c92e901cbeb77 Mon Sep 17 00:00:00 2001 From: Ariful Islam Date: Mon, 13 May 2024 23:26:16 +0600 Subject: [PATCH 1/2] application directory changed --- App/System/Application.php | 13 +++++++------ App/System/Controller.php | 14 +++++++------- App/System/Helpers/AppHelper.php | 13 +++++++++++++ composer.json | 2 +- {App/config => config}/database.php | 0 {App/config => config}/environment.php | 0 public/index.php | 1 + 7 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 App/System/Helpers/AppHelper.php rename {App/config => config}/database.php (100%) rename {App/config => config}/environment.php (100%) diff --git a/App/System/Application.php b/App/System/Application.php index 1708980..6aee0c0 100644 --- a/App/System/Application.php +++ b/App/System/Application.php @@ -8,7 +8,6 @@ use App\System\Exception\MethodNotFoundException; use App\System\exception\MvcException; -use ArgumentCountError; use Exception; use Whoops\Handler\PrettyPageHandler; use Whoops\Run as WhoopsRun; @@ -33,12 +32,11 @@ class Application */ private array $params = []; - public function __construct() - { - - } - private function initialize() + /** + * @throws Exception + */ + private function initialize(): void { $controller = new Controller(); $controller->config('environment'); @@ -135,6 +133,9 @@ private function parseUrl() ]; } + /** + * @throws Exception + */ public function run() { $this->initialize(); diff --git a/App/System/Controller.php b/App/System/Controller.php index cf97e0d..e938b36 100644 --- a/App/System/Controller.php +++ b/App/System/Controller.php @@ -3,6 +3,7 @@ namespace App\System; use App\System\exception\MvcException; +use App\System\Helpers\AppHelper; class Controller { @@ -115,25 +116,24 @@ public function helpers($helper = '') /** * @param $config + * @throws \Exception */ public function config($config) { + $configurationFilePath = AppHelper::getAppPath() . '/../../../config/' . $config . '.php'; + try { - if (file_exists('../App/config/' . $config . '.php')) { + if (file_exists($configurationFilePath)) { + require_once $configurationFilePath; - require_once '../App/config/' . $config . '.php'; } else { throw new MvcException("config file '$config' does not exist"); } } catch (MvcException $exception) { - - echo '
';
-            print_r($exception->showException([
-                debug_print_backtrace()
-            ]));
+            throw new \Exception($exception);
 
         }
     }
diff --git a/App/System/Helpers/AppHelper.php b/App/System/Helpers/AppHelper.php
new file mode 100644
index 0000000..d3bedae
--- /dev/null
+++ b/App/System/Helpers/AppHelper.php
@@ -0,0 +1,13 @@
+run();

From d293c51fd208a3abd2fd4a6a8bc2952ceac84150 Mon Sep 17 00:00:00 2001
From: Ariful Islam 
Date: Tue, 14 May 2024 01:40:25 +0600
Subject: [PATCH 2/2] Class and Method loaded usign ReflectionClassMethod

---
 .gitignore                                    |  5 +++
 App/System/Exception/MvcException.php         | 34 -------------------
 {App => app}/.htaccess                        |  0
 {App => app}/Controllers/Home.php             | 19 +++++------
 {App => app}/Models/Book.php                  |  0
 {App => app}/Models/User.php                  |  0
 {App => app}/System/Application.php           | 34 ++++++++++++++++---
 {App => app}/System/Controller.php            | 14 +++-----
 .../Exception/MethodNotFoundException.php     |  0
 .../MvcDatabaseConnectionException.php        |  0
 app/System/Exception/MvcException.php         | 16 +++++++++
 {App => app}/System/Helpers/AppHelper.php     |  0
 {App => app}/System/Helpers/FormHelper.php    |  0
 app/System/Http/Request.php                   | 18 ++++++++++
 .../System/Libraries/Database/Database.php    |  0
 {App => app}/System/Model.php                 |  1 -
 {App => app}/System/Validator/Form.php        |  0
 {App => app}/init.php                         |  0
 {App => app}/views/home/index.php             |  4 ---
 {App => app}/views/home/see_report.php        |  0
 composer.json                                 |  3 +-
 config/database.php                           |  2 +-
 config/environment.php                        |  6 ++--
 23 files changed, 86 insertions(+), 70 deletions(-)
 delete mode 100644 App/System/Exception/MvcException.php
 rename {App => app}/.htaccess (100%)
 rename {App => app}/Controllers/Home.php (88%)
 rename {App => app}/Models/Book.php (100%)
 rename {App => app}/Models/User.php (100%)
 rename {App => app}/System/Application.php (70%)
 rename {App => app}/System/Controller.php (91%)
 rename {App => app}/System/Exception/MethodNotFoundException.php (100%)
 rename {App => app}/System/Exception/MvcDatabaseConnectionException.php (100%)
 create mode 100644 app/System/Exception/MvcException.php
 rename {App => app}/System/Helpers/AppHelper.php (100%)
 rename {App => app}/System/Helpers/FormHelper.php (100%)
 create mode 100644 app/System/Http/Request.php
 rename {App => app}/System/Libraries/Database/Database.php (100%)
 rename {App => app}/System/Model.php (99%)
 rename {App => app}/System/Validator/Form.php (100%)
 rename {App => app}/init.php (100%)
 rename {App => app}/views/home/index.php (75%)
 rename {App => app}/views/home/see_report.php (100%)

diff --git a/.gitignore b/.gitignore
index 8b167f5..5466b56 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,8 @@
 .idea
 *.log
 *.iml
+*.zip
+*.iml
+*.log
+node_modules
+composer.lock
diff --git a/App/System/Exception/MvcException.php b/App/System/Exception/MvcException.php
deleted file mode 100644
index 0b098b4..0000000
--- a/App/System/Exception/MvcException.php
+++ /dev/null
@@ -1,34 +0,0 @@
- $this->getMessage(),
-            'used_file' => [
-                'file' => $bt[0]['file'] . ' at line: ' . $bt[0]['line'],
-                'class' => $bt[1]['class'],
-                'method' => $bt[1]['function'] . '()',
-                // 'called by' => $this->getCaller()
-            ],
-            'trace' => $this->getTraceAsString(),
-        ];
-    }
-
-}
diff --git a/App/.htaccess b/app/.htaccess
similarity index 100%
rename from App/.htaccess
rename to app/.htaccess
diff --git a/App/Controllers/Home.php b/app/Controllers/Home.php
similarity index 88%
rename from App/Controllers/Home.php
rename to app/Controllers/Home.php
index 15bacf6..fdc5638 100644
--- a/App/Controllers/Home.php
+++ b/app/Controllers/Home.php
@@ -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();
     }
 
diff --git a/App/Models/Book.php b/app/Models/Book.php
similarity index 100%
rename from App/Models/Book.php
rename to app/Models/Book.php
diff --git a/App/Models/User.php b/app/Models/User.php
similarity index 100%
rename from App/Models/User.php
rename to app/Models/User.php
diff --git a/App/System/Application.php b/app/System/Application.php
similarity index 70%
rename from App/System/Application.php
rename to app/System/Application.php
index 6aee0c0..785f29b 100644
--- a/App/System/Application.php
+++ b/app/System/Application.php
@@ -8,7 +8,9 @@
 
 use App\System\Exception\MethodNotFoundException;
 use App\System\exception\MvcException;
+use App\System\Http\Request;
 use Exception;
+use ReflectionMethod;
 use Whoops\Handler\PrettyPageHandler;
 use Whoops\Run as WhoopsRun;
 
@@ -39,8 +41,12 @@ class Application
     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()
@@ -77,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);
@@ -88,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);
         }
diff --git a/App/System/Controller.php b/app/System/Controller.php
similarity index 91%
rename from App/System/Controller.php
rename to app/System/Controller.php
index e938b36..4db7866 100644
--- a/App/System/Controller.php
+++ b/app/System/Controller.php
@@ -46,7 +46,7 @@ public function model($model)
      * @param $view
      * @param array $data
      */
-    public function view($view, $data = [])
+    public function view($view, array $data = [])
     {
         try {
 
@@ -120,23 +120,17 @@ public function helpers($helper = '')
      */
     public function config($config)
     {
-        $configurationFilePath = AppHelper::getAppPath() . '/../../../config/' . $config . '.php';
+        $configurationFilePath = AppHelper::getAppPath() . '/../../../config/' . $config. '.php';
 
         try {
 
             if (file_exists($configurationFilePath)) {
                 require_once $configurationFilePath;
-
-            } else {
-
-                throw new MvcException("config file '$config' does not exist");
             }
 
-        } catch (MvcException $exception) {
-            throw new \Exception($exception);
-
+        } catch (\Exception $exception) {
+            throw new MvcException($exception->getMessage());
         }
     }
 
-
 }
diff --git a/App/System/Exception/MethodNotFoundException.php b/app/System/Exception/MethodNotFoundException.php
similarity index 100%
rename from App/System/Exception/MethodNotFoundException.php
rename to app/System/Exception/MethodNotFoundException.php
diff --git a/App/System/Exception/MvcDatabaseConnectionException.php b/app/System/Exception/MvcDatabaseConnectionException.php
similarity index 100%
rename from App/System/Exception/MvcDatabaseConnectionException.php
rename to app/System/Exception/MvcDatabaseConnectionException.php
diff --git a/app/System/Exception/MvcException.php b/app/System/Exception/MvcException.php
new file mode 100644
index 0000000..8d87e79
--- /dev/null
+++ b/app/System/Exception/MvcException.php
@@ -0,0 +1,16 @@
+statement == null) {
 
                 throw new MvcDatabaseConnectionException('someting wrong');
diff --git a/App/System/Validator/Form.php b/app/System/Validator/Form.php
similarity index 100%
rename from App/System/Validator/Form.php
rename to app/System/Validator/Form.php
diff --git a/App/init.php b/app/init.php
similarity index 100%
rename from App/init.php
rename to app/init.php
diff --git a/App/views/home/index.php b/app/views/home/index.php
similarity index 75%
rename from App/views/home/index.php
rename to app/views/home/index.php
index e29eabf..86f29c4 100644
--- a/App/views/home/index.php
+++ b/app/views/home/index.php
@@ -18,9 +18,5 @@
     

Developed BY: Ariful Islam

- - diff --git a/App/views/home/see_report.php b/app/views/home/see_report.php similarity index 100% rename from App/views/home/see_report.php rename to app/views/home/see_report.php diff --git a/composer.json b/composer.json index 6a94ef0..f5bab02 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/config/database.php b/config/database.php index 6f5d5c7..ed2b200 100644 --- a/config/database.php +++ b/config/database.php @@ -5,4 +5,4 @@ define('HOST', "localhost"); define('DB_USER', "root"); define('DB_PASS', ""); -define('DB_NAME', "mvc_app"); +define('DB_NAME', "laravel"); diff --git a/config/environment.php b/config/environment.php index 4791056..8b0557c 100644 --- a/config/environment.php +++ b/config/environment.php @@ -1,6 +1,4 @@