Skip to content

Commit

Permalink
Merge pull request #529 from flightphp/coding-style
Browse files Browse the repository at this point in the history
added PSR12 coding style to files.
  • Loading branch information
n0nag0n authored Jan 25, 2024
2 parents 0d7b79b + cc4338a commit 8f7c48c
Show file tree
Hide file tree
Showing 34 changed files with 2,071 additions and 1,819 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1.10",
"phpstan/extension-installer": "^1.3",
"rregeer/phpunit-coverage-check": "^0.3.1"
"rregeer/phpunit-coverage-check": "^0.3.1",
"squizlabs/php_codesniffer": "^3.8"
},
"config": {
"allow-plugins": {
Expand All @@ -47,7 +48,9 @@
"scripts": {
"test": "phpunit",
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100",
"lint": "phpstan --no-progress -cphpstan.neon"
"lint": "phpstan --no-progress -cphpstan.neon",
"beautify": "phpcbf --standard=phpcs.xml",
"phpcs": "phpcs --standard=phpcs.xml"
},
"suggest": {
"latte/latte": "Latte template engine",
Expand Down
138 changes: 68 additions & 70 deletions flight/Engine.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

declare(strict_types=1);

/**
* Flight: An extensible micro-framework.
*
Expand Down Expand Up @@ -32,7 +33,7 @@
* @method void start() Starts engine
* @method void stop() Stops framework and outputs current response
* @method void halt(int $code = 200, string $message = '') Stops processing and returns a given response.
*
*
* Routing
* @method Route route(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a URL to a callback function with all applicable methods
* @method void group(string $pattern, callable $callback, array $group_middlewares = []) Groups a set of routes together under a common prefix.
Expand Down Expand Up @@ -78,12 +79,12 @@ class Engine
*/
protected Dispatcher $dispatcher;

/**
* If the framework has been initialized or not
*
* @var boolean
*/
protected bool $initialized = false;
/**
* If the framework has been initialized or not
*
* @var boolean
*/
protected bool $initialized = false;

/**
* Constructor.
Expand Down Expand Up @@ -377,7 +378,7 @@ public function _start(): void
ob_start();

// Route the request
$failed_middleware_check = false;
$failed_middleware_check = false;
while ($route = $router->route($request)) {
$params = array_values($route->params);

Expand All @@ -386,55 +387,52 @@ public function _start(): void
$params[] = $route;
}

// Run any before middlewares
if(count($route->middleware) > 0) {
foreach($route->middleware as $middleware) {

$middleware_object = (is_callable($middleware) === true ? $middleware : (method_exists($middleware, 'before') === true ? [ $middleware, 'before' ]: false));
// Run any before middlewares
if (count($route->middleware) > 0) {
foreach ($route->middleware as $middleware) {
$middleware_object = (is_callable($middleware) === true ? $middleware : (method_exists($middleware, 'before') === true ? [ $middleware, 'before' ] : false));

if($middleware_object === false) {
continue;
}
if ($middleware_object === false) {
continue;
}

// It's assumed if you don't declare before, that it will be assumed as the before method
$middleware_result = $middleware_object($route->params);
// It's assumed if you don't declare before, that it will be assumed as the before method
$middleware_result = $middleware_object($route->params);

if ($middleware_result === false) {
$failed_middleware_check = true;
break 2;
}
}
}
if ($middleware_result === false) {
$failed_middleware_check = true;
break 2;
}
}
}

// Call route handler
$continue = $this->dispatcher->execute(
$route->callback,
$params
);


// Run any before middlewares
if(count($route->middleware) > 0) {

// process the middleware in reverse order now
foreach(array_reverse($route->middleware) as $middleware) {

// must be an object. No functions allowed here
$middleware_object = is_object($middleware) === true && !($middleware instanceof Closure) && method_exists($middleware, 'after') === true ? [ $middleware, 'after' ] : false;
// Run any before middlewares
if (count($route->middleware) > 0) {
// process the middleware in reverse order now
foreach (array_reverse($route->middleware) as $middleware) {
// must be an object. No functions allowed here
$middleware_object = is_object($middleware) === true && !($middleware instanceof Closure) && method_exists($middleware, 'after') === true ? [ $middleware, 'after' ] : false;

// has to have the after method, otherwise just skip it
if($middleware_object === false) {
continue;
}
// has to have the after method, otherwise just skip it
if ($middleware_object === false) {
continue;
}

$middleware_result = $middleware_object($route->params);
$middleware_result = $middleware_object($route->params);

if ($middleware_result === false) {
$failed_middleware_check = true;
break 2;
}
}
}
if ($middleware_result === false) {
$failed_middleware_check = true;
break 2;
}
}
}

$dispatched = true;

Expand All @@ -447,9 +445,9 @@ public function _start(): void
$dispatched = false;
}

if($failed_middleware_check === true) {
$this->halt(403, 'Forbidden');
} else if($dispatched === false) {
if ($failed_middleware_check === true) {
$this->halt(403, 'Forbidden');
} elseif ($dispatched === false) {
$this->notFound();
}
}
Expand All @@ -476,11 +474,11 @@ public function _error($e): void
->status(500)
->write($msg)
->send();
// @codeCoverageIgnoreStart
// @codeCoverageIgnoreStart
} catch (Throwable $t) {
exit($msg);
}
// @codeCoverageIgnoreEnd
// @codeCoverageIgnoreEnd
}

/**
Expand Down Expand Up @@ -512,20 +510,20 @@ public function _stop(?int $code = null): void
* @param string $pattern URL pattern to match
* @param callable $callback Callback function
* @param bool $pass_route Pass the matching route object to the callback
* @param string $alias the alias for the route
* @return Route
* @param string $alias the alias for the route
* @return Route
*/
public function _route(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route
{
return $this->router()->map($pattern, $callback, $pass_route, $alias);
}

/**
/**
* Routes a URL to a callback function.
*
* @param string $pattern URL pattern to match
* @param callable $callback Callback function that includes the Router class as first parameter
* @param array<callable> $group_middlewares The middleware to be applied to the route
* @param string $pattern URL pattern to match
* @param callable $callback Callback function that includes the Router class as first parameter
* @param array<callable> $group_middlewares The middleware to be applied to the route
*/
public function _group(string $pattern, callable $callback, array $group_middlewares = []): void
{
Expand Down Expand Up @@ -585,7 +583,7 @@ public function _delete(string $pattern, callable $callback, bool $pass_route =
*
* @param int $code HTTP status code
* @param string $message Response message
*
*
*/
public function _halt(int $code = 200, string $message = ''): void
{
Expand All @@ -594,10 +592,10 @@ public function _halt(int $code = 200, string $message = ''): void
->status($code)
->write($message)
->send();
// apologies for the crappy hack here...
if($message !== 'skip---exit') {
exit(); // @codeCoverageIgnore
}
// apologies for the crappy hack here...
if ($message !== 'skip---exit') {
exit(); // @codeCoverageIgnore
}
}

/**
Expand Down Expand Up @@ -728,7 +726,7 @@ public function _etag(string $id, string $type = 'strong'): void
{
$id = (('weak' === $type) ? 'W/' : '') . $id;

$this->response()->header('ETag', '"'.str_replace('"', '\"', $id).'"');
$this->response()->header('ETag', '"' . str_replace('"', '\"', $id) . '"');

if (
isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
Expand All @@ -755,14 +753,14 @@ public function _lastModified(int $time): void
}
}

/**
* Gets a url from an alias that's supplied.
*
* @param string $alias the route alias.
* @param array<string, mixed> $params The params for the route if applicable.
*/
public function _getUrl(string $alias, array $params = []): string
{
return $this->router()->getUrlByAlias($alias, $params);
}
/**
* Gets a url from an alias that's supplied.
*
* @param string $alias the route alias.
* @param array<string, mixed> $params The params for the route if applicable.
*/
public function _getUrl(string $alias, array $params = []): string
{
return $this->router()->getUrlByAlias($alias, $params);
}
}
61 changes: 31 additions & 30 deletions flight/Flight.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

declare(strict_types=1);

/**
* Flight: An extensible micro-framework.
*
Expand Down Expand Up @@ -61,34 +62,34 @@ class Flight
{
/**
* Framework engine.
*
* @var Engine $engine
*
* @var Engine $engine
*/
private static Engine $engine;

/**
* Whether or not the app has been initialized
*
* @var boolean
*/
private static bool $initialized = false;
/**
* Whether or not the app has been initialized
*
* @var boolean
*/
private static bool $initialized = false;

/**
* Don't allow object instantiation
*
* @codeCoverageIgnore
* @return void
*/
/**
* Don't allow object instantiation
*
* @codeCoverageIgnore
* @return void
*/
private function __construct()
{
}

/**
* Forbid cloning the class
*
* @codeCoverageIgnore
* @return void
*/
/**
* Forbid cloning the class
*
* @codeCoverageIgnore
* @return void
*/
private function __clone()
{
}
Expand Down Expand Up @@ -145,14 +146,14 @@ public static function app(): Engine
return self::$engine;
}

/**
* Set the engine instance
*
* @param Engine $engine Vroom vroom!
* @return void
*/
public static function setEngine(Engine $engine): void
{
self::$engine = $engine;
}
/**
* Set the engine instance
*
* @param Engine $engine Vroom vroom!
* @return void
*/
public static function setEngine(Engine $engine): void
{
self::$engine = $engine;
}
}
1 change: 1 addition & 0 deletions flight/autoload.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

declare(strict_types=1);

/**
* Flight: An extensible micro-framework.
*
Expand Down
Loading

0 comments on commit 8f7c48c

Please sign in to comment.