Skip to content

Commit

Permalink
Release a better version
Browse files Browse the repository at this point in the history
  • Loading branch information
hsemix committed Jun 22, 2022
1 parent fb5453b commit a723e98
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 38 deletions.
61 changes: 61 additions & 0 deletions app/Middleware/ApiMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Middleware;

use Closure;
use Yuga\Http\Request;
use Yuga\JWTAuth\UsesJWTTokens;
use Yuga\Application\Application;
use Yuga\Http\Middleware\IMiddleware;

class ApiMiddleware implements IMiddleware
{
use UsesJWTTokens;
/**
* @var \Yuga\Application\Application | null
*/
protected $app;
/**
* -------------------------------------------------------------------------
* Inject any objects (in the contructor) you want to use in this middleware
* We will worry about instantiating them for you
* -------------------------------------------------------------------------
*/
public function __construct(Application $app)
{
$this->app = $app;
}

/**
* Handle an incoming request.
*
* @param \Yuga\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function run(Request $request, Closure $next)
{
try {
$token = $request->getBearerToken();

try {
$this->verifyToken($token);
return $next($request);
} catch (\Exception $e) {
return response()->json([
'app_status' => false,
'message' => $e->getMessage(),
'data' => null
]);
}

} catch (\Exception $e) {
return response()->json([
'app_status' => false,
'message' => 'Unauthorized Access',
'data' => null
], null, 401);
}

}
}
10 changes: 10 additions & 0 deletions app/Middleware/WebMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Middleware;

use Yuga\Http\Middleware\BaseCsrfVerifier;

class WebMiddleware extends BaseCsrfVerifier
{
protected $except = [];
}
39 changes: 39 additions & 0 deletions app/Providers/RouteProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Providers;

use Yuga\Route\Route;
use App\Middleware\WebMiddleware;
use Yuga\Providers\ServiceProvider;
use Yuga\Interfaces\Application\Application;
use Yuga\Route\Exceptions\NotFoundHttpExceptionHandler;

class RouteProvider extends ServiceProvider
{
protected $users = [];
protected $namespace = 'App\Controllers';
/**
* Register a service to the application
*
* @param \Yuga\Interfaces\Application\Application
*
* @return mixed
*/
public function load(Application $app)
{
return $app;
}

public function boot(Route $router)
{
$_ENV['ROUTER_BOOTED'] = true;
$router->group(['prefix' => 'api', 'middleware' => 'api', 'namespace' => $this->namespace], function () {
require path('routes/api.php');
});

$router->group(['middleware' => 'web', 'namespace' => $this->namespace, 'exceptionHandler' => NotFoundHttpExceptionHandler::class], function () use ($router) {
$router->csrfVerifier(new WebMiddleware);
require path('routes/web.php');
});
}
}
41 changes: 40 additions & 1 deletion boot/app.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
<?php
require_once 'autoload.php';

use josegonzalez\Dotenv\Loader;

define('YUGA_START_TIME', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Composer Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so we do not have to manually load any of
| our application's Main classes.
|
*/
$_ENV['base_path'] = realpath(__DIR__.'/../');
$_ENV['yuga_path'] = __DIR__;
$loader = require $_ENV['base_path'].'/vendor/autoload.php';

if (class_exists(Loader::class)) {
$env = $_ENV['base_path'] . '/environment/.env';
if (file_exists($env)) {
$Load = new Loader($env);
// Parse the .env file
$Load->parse();
// Send the parsed .env file to the $_ENV variable
$Load->toEnv();

$loader->addPsr4(env('APP_NAMESPACE', 'App') . '\\', $_ENV['base_path'] . '/app/');
}

} else {
$vars = json_decode(file_get_contents($_ENV['base_path'] . '/environment/config.json'), true);
array_map(function ($var, $value) {
$_ENV[$var] = $value;
}, array_keys($vars), array_values($vars));
}

$app = new \Yuga\Application\Application(
realpath(__DIR__.'/../')
);
Expand All @@ -10,4 +47,6 @@
|--------------------------------------------------------------------------
*/

$app->run();

return $app;
28 changes: 0 additions & 28 deletions boot/autoload.php

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"yuga/framework": "^3.0",
"yuga/framework": "^4.0",
"phpunit/phpunit": "^7.4"
},
"autoload": {
Expand Down
4 changes: 3 additions & 1 deletion config/AppMiddleWare.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
*/
return [
'guest' => \App\Middleware\RedirectIfAuthenticated::class,
];
'api' => \App\Middleware\ApiMiddleware::class,
'web' => \App\Middleware\WebMiddleware::class,
];
1 change: 1 addition & 0 deletions config/ServiceProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
\Yuga\Mailables\MailableServiceProvider::class,
\Yuga\Validate\ValidateServiceProvider::class,
\Yuga\Route\Rewrite\RouteRewriteServiceProvider::class,
\App\Providers\RouteProvider::class,
];
10 changes: 9 additions & 1 deletion environment/.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ DATABASE_USERNAME=root # database username
DATABASE_PASSWORD= # database password
DATABASE_CHARSET=utf8 # database character set
DATABASE_PREFIX= # database table prefix e.g yuga_users where "yuga_" is the prefix on every table found in the defined database
DATABASE_DB_API_ACTIVE_RECORD=false # Use Active Record implementation in DB API
MYSQL_DUMP_COMMAND_PATH= # location of the mysql binary
MYSQL_RESTORE_COMMAND_PATH= # location of the mysql binary for restore

# Set whether or not your application is in debug mode
DEBUG_MODE=true
Expand All @@ -15,11 +18,16 @@ DEBUG_MODE=true
DEBUG_MODE_SETTINGS= #Expects a json-object N.B. Defaults to {"controller_missing": true, "method_missing": true}

# Set a route for the redirect when the user is automatically logged out
DEFAULT_LOGIN_REDIRECT=login # a redirect route managed by the auth middleware when there's been an automatic logout.
DEFAULT_LOGIN_REDIRECT=login # a redirect route managed by the auth middleware when there's been an automatic logout or after a period of inactivity.

# Provide a namespace for you app other than App
APP_NAMESPACE=

# Support for MVP (Mostly for designers)
# PREFIX_MVP_ROUTE="/pages"
ENABLE_MVP_ROUTES=false
MVP_CONTROLLER= # Defaults to Controller

# Provide a 404 page that you want to use, otherwise default to yuga's
NOT_FOUND_404_FILE=

Expand Down
3 changes: 0 additions & 3 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
| loading any of our classes later on. It feels great to relax.
|
*/

require __DIR__.'/../boot/autoload.php';

/**
* Start the application
*/
Expand Down
7 changes: 7 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

Route::get('/', function (Response $response) {
return $response->json([
'data' => 'api-data',
]);
});
3 changes: 0 additions & 3 deletions yuga
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#!/usr/bin/env php
<?php
require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/boot/autoload.php';

/**
* Start the application
*/

$app = require __DIR__.'/boot/app.php';

//--------------------------------------------------------------------------
Expand Down

0 comments on commit a723e98

Please sign in to comment.