Skip to content

Commit

Permalink
Merge pull request #9 from opencorero/dev
Browse files Browse the repository at this point in the history
Checking route mechanism changed
  • Loading branch information
opencorero authored Jan 26, 2020
2 parents a803e18 + ee6e098 commit 8ad5ada
Show file tree
Hide file tree
Showing 45 changed files with 801 additions and 259 deletions.
97 changes: 25 additions & 72 deletions Framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,26 @@ class Framework
private $response;
private $request = null;
private $kernel;
private $get_routes;

private $registry;
private $route;
private $output;
public $route;
public $output;

public function __construct()
{
$this->app = require __DIR__ . '/bootstrap/app.php';

$this->kernel = $this->app->make(\Illuminate\Contracts\Http\Kernel::class);

$this->app->singleton('OcLoader', function () {
$OcLoader = new \OpenCore\Support\OcLoader();
/**
* set loaded true in order to know the request was done through OpenCart
*/
$OcLoader->set('loaded', true);

return $OcLoader;
});
}

/**
Expand All @@ -56,21 +65,14 @@ public static function getInstance()
return self::$instance;
}

public function initiate($registry, $route, &$output)
{
$this->registry = $registry;
$this->route = $route;
$this->output = $output;
}

/**
* Retrieve Response
*/
public function getResponse()
{
$this->response->sendHeaders();

$content = $this->response->getContent();
$content = $this->response;

$this->kernel->terminate($this->request, $this->response);

Expand All @@ -86,72 +88,15 @@ public function getRegistry($type = null)
return $this->registry;
}

/**
* Run Framework
*/
public function run()
{
$this->response = $this->kernel->handle(
$this->request = \Illuminate\Http\Request::capture()
);

$this->response->send();

$this->kernel->terminate($this->request, $this->response);
}

/**
* Handle Framework Response
*/
public function handle()
public function handle($registry, $route, &$output)
{
$this->response = $this->kernel->handle($this->request);

if ($this->response instanceof \Symfony\Component\HttpFoundation\BinaryFileResponse) {
$this->response->send();

return true;
} else {
return ($this->response->status() != '404') ? true : false;
}
}

/**
* Check Route function
*
* @return bool
*/
public function checkRoute()
{
Framework::getInstance()->initiateRouteRequest();

/**
* TODO: find a better way to check all available routes
*/
if (!isset($this->get_routes)) {
$response = $this->kernel->handle($request = \Illuminate\Http\Request::capture());

$this->kernel->terminate($request, $response);

$this->get_routes = $this->app->router->getRoutes();
};

try {
return (bool) $this->get_routes->match($this->request)->uri();
} catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e) {
return false;
}
}
$this->registry = $registry;
$this->route = $route;
$this->output = $output;

/**
* Initiate Route request function
*
* @param string $route
* @param string &$output
* @return void
*/
public function initiateRouteRequest()
{
$this->request = \Illuminate\Http\Request::capture();

/**
Expand All @@ -170,5 +115,13 @@ public function initiateRouteRequest()
if ($this->output !== false) {
$this->request->server->set('REQUEST_URI', $this->route);
}

$this->response = $this->kernel->handle($this->request);

if ($this->response instanceof \Symfony\Component\HttpFoundation\BinaryFileResponse) {
$this->response->send();
}

$this->kernel->terminate($this->request, $this->response);
}
}
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,25 @@ check Laravel requirements because they may differ depending on the used version
4. `cd core/`
5. `composer update`
6. `php artisan key:generate`
7. `php artisan migrate:install`
8. `php artisan migrate`
7. `php artisan migrate`
8. `php artisan opencore:register-routes`
9. copy OpenCart extension files from core/opencart-module/(2.x|3.x)/upload to you OpenCart root folder
10. go to OpenCart admin panel / extensions / extensions / modules, find OpenCore module and install it
11. click on the OpenCore icon from admin / left column / section "System Requirements" and make sure there's nothing marked with red
12. Optional: in order to enable Developer & Example modules you need to access admin / user / user groups section and add permission for them

Enjoy!

## Update

1. `cd core/`
2. `git update`
3. `composer update`
4. `php artisan migrate`
5. `php artisan opencore:register-routes`
6. copy OpenCart extension files from core/opencart-module/(2.x|3.x)/upload to you OpenCart root folder
7. Optional: go to OpenCart / extensions / modules, disable and then re-enable the OpenCore module

## How does it works ?

Let's take this file from OpenCart system: `/admin/controller/common/header.php`
Expand Down Expand Up @@ -109,6 +119,8 @@ MIT license. Please see the [license file](LICENSE) for more information.

## TODO

* create a console command for faster instalation / update
* force paginator->appends to automatically add token/user_token param for admin requests only
* when enabling a module the permission must be added automatically for the user who made the action
* Example & Developer modules should be disabled by default
* display jobs & failed_jobs lists in developer module
Expand Down
55 changes: 55 additions & 0 deletions app/Console/Commands/ClearCacheCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/*
* Created on Fri Jan 23 2020 by DaRock
*
* Copyright (c) Aweb Design
* https://www.awebdesign.ro
*/

namespace App\Console\Commands;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Console\Command;

class ClearCacheCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'opencore:clear-cache';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Clear all cache options';

/**
* Create a new route command instance.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
Artisan::call('config:clear');
Artisan::call('route:clear');
Artisan::call('view:clear');
Artisan::call('cache:clear');

return $this->info("Cache has been deleted!");
}
}
125 changes: 125 additions & 0 deletions app/Console/Commands/RegisterRoutesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/*
* Created on Fri Jan 23 2020 by DaRock
*
* Copyright (c) Aweb Design
* https://www.awebdesign.ro
*/

namespace App\Console\Commands;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Routing\Router;
use Illuminate\Console\Command;
use OpenCore\Support\Entities\OpencoreRoute;

class RegisterRoutesCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'opencore:register-routes';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Register all routes into databse';

/**
* Ignore methods
*/
protected $ignore_methods = [
'HEAD'
];

/**
* The router instance.
*
* @var \Illuminate\Routing\Router
*/
protected $router;

/**
* Create a new route command instance.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function __construct(Router $router)
{
parent::__construct();

$this->router = $router;
}

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->router->compiled = false;
Artisan::call('cache:clear');
Artisan::call('route:clear');

// $this->router->setRoutes($this->router->getRoutes());
if (empty($this->router->getRoutes())) {
$this->deleteAllRoutes();

return $this->error("Your application doesn't have any routes.");
}

$this->registerRoutes();

return $this->info("Your route have been registered.");
}

/**
* Register all routes into database
*
* @return void
*/
protected function registerRoutes()
{
$registeredRoutes = OpencoreRoute::get()->pluck('id', 'unique_key')->toArray();

$keep = [];
$routes = $this->router->getRoutes();
foreach ($routes as $route) {
foreach ($route->methods() as $method) {
if (!in_array($method, $this->ignore_methods)) {
$data = [
'method' => $method,
'uri' => $route->uri(),
'name' => $route->getName()
];

$key = implode('', $data);

$id = $registeredRoutes[$key] ?? null;
if (is_null($id)) {
$id = OpencoreRoute::insertGetId($data);
}
$keep[] = $id;
}
}
}

OpencoreRoute::whereNotIn('id', $keep)->delete();
}

/**
* Delete all routes from database
*
* @return void
*/
protected function deleteAllRoutes()
{
OpencoreRoute::truncate();
}
}
5 changes: 4 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Console\Commands\ClearCacheCommand;
use App\Console\Commands\RegisterRoutesCommand;

class Kernel extends ConsoleKernel
{
Expand All @@ -13,7 +15,8 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
//
ClearCacheCommand::class,
RegisterRoutesCommand::class
];

/**
Expand Down
Loading

0 comments on commit 8ad5ada

Please sign in to comment.