Skip to content

Commit

Permalink
Merge pull request #559 from flightphp/container
Browse files Browse the repository at this point in the history
Added Containerization to Core
  • Loading branch information
n0nag0n authored Mar 22, 2024
2 parents 412596e + 54403be commit 8d772b5
Show file tree
Hide file tree
Showing 12 changed files with 616 additions and 131 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
},
"require-dev": {
"ext-pdo_sqlite": "*",
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1.10",
"league/container": "^4.2",
"level-2/dice": "^4.0",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.5",
"rregeer/phpunit-coverage-check": "^0.3.1",
"squizlabs/php_codesniffer": "^3.8"
},
Expand Down
45 changes: 30 additions & 15 deletions flight/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@
* @method void halt(int $code = 200, string $message = '', bool $actuallyExit = true) Stops processing and returns a given response.
*
* # Routing
* @method Route route(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
* @method Route route(string $pattern, callable|string $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<int, callable|object> $group_middlewares = [])
* Groups a set of routes together under a common prefix.
* @method Route post(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
* @method Route post(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '')
* Routes a POST URL to a callback function.
* @method Route put(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
* @method Route put(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '')
* Routes a PUT URL to a callback function.
* @method Route patch(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
* @method Route patch(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '')
* Routes a PATCH URL to a callback function.
* @method Route delete(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
* @method Route delete(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '')
* Routes a DELETE URL to a callback function.
* @method Router router() Gets router
* @method string getUrl(string $alias) Gets a url from an alias
Expand Down Expand Up @@ -138,6 +138,9 @@ public function init(): void
$this->dispatcher->reset();
}

// Add this class to Dispatcher
$this->dispatcher->setEngine($this);

// Register default components
$this->loader->register('request', Request::class);
$this->loader->register('response', Response::class);
Expand Down Expand Up @@ -217,6 +220,18 @@ public function handleException(Throwable $e): void
$this->error($e);
}

/**
* Registers the container handler
*
* @param callable|object $containerHandler Callback function or PSR-11 Container object that sets the container and how it will inject classes
*
* @return void
*/
public function registerContainerHandler($containerHandler): void
{
$this->dispatcher->setContainerHandler($containerHandler);
}

/**
* Maps a callback to a framework method.
*
Expand Down Expand Up @@ -605,11 +620,11 @@ public function _stop(?int $code = null): void
* Routes a URL to a callback function.
*
* @param string $pattern URL pattern to match
* @param callable $callback Callback function
* @param callable|string $callback Callback function
* @param bool $pass_route Pass the matching route object to the callback
* @param string $alias The alias for the route
*/
public function _route(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route
public function _route(string $pattern, $callback, bool $pass_route = false, string $alias = ''): Route
{
return $this->router()->map($pattern, $callback, $pass_route, $alias);
}
Expand All @@ -630,10 +645,10 @@ public function _group(string $pattern, callable $callback, array $group_middlew
* Routes a URL to a callback function.
*
* @param string $pattern URL pattern to match
* @param callable $callback Callback function
* @param callable|string $callback Callback function or string class->method
* @param bool $pass_route Pass the matching route object to the callback
*/
public function _post(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): void
public function _post(string $pattern, $callback, bool $pass_route = false, string $route_alias = ''): void
{
$this->router()->map('POST ' . $pattern, $callback, $pass_route, $route_alias);
}
Expand All @@ -642,10 +657,10 @@ public function _post(string $pattern, callable $callback, bool $pass_route = fa
* Routes a URL to a callback function.
*
* @param string $pattern URL pattern to match
* @param callable $callback Callback function
* @param callable|string $callback Callback function or string class->method
* @param bool $pass_route Pass the matching route object to the callback
*/
public function _put(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): void
public function _put(string $pattern, $callback, bool $pass_route = false, string $route_alias = ''): void
{
$this->router()->map('PUT ' . $pattern, $callback, $pass_route, $route_alias);
}
Expand All @@ -654,10 +669,10 @@ public function _put(string $pattern, callable $callback, bool $pass_route = fal
* Routes a URL to a callback function.
*
* @param string $pattern URL pattern to match
* @param callable $callback Callback function
* @param callable|string $callback Callback function or string class->method
* @param bool $pass_route Pass the matching route object to the callback
*/
public function _patch(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): void
public function _patch(string $pattern, $callback, bool $pass_route = false, string $route_alias = ''): void
{
$this->router()->map('PATCH ' . $pattern, $callback, $pass_route, $route_alias);
}
Expand All @@ -666,10 +681,10 @@ public function _patch(string $pattern, callable $callback, bool $pass_route = f
* Routes a URL to a callback function.
*
* @param string $pattern URL pattern to match
* @param callable $callback Callback function
* @param callable|string $callback Callback function or string class->method
* @param bool $pass_route Pass the matching route object to the callback
*/
public function _delete(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): void
public function _delete(string $pattern, $callback, bool $pass_route = false, string $route_alias = ''): void
{
$this->router()->map('DELETE ' . $pattern, $callback, $pass_route, $route_alias);
}
Expand Down
49 changes: 11 additions & 38 deletions flight/Flight.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,25 @@
* @copyright Copyright (c) 2011, Mike Cao <[email protected]>
*
* # Core methods
* @method static void start() Starts the framework.
* @method static void path(string $path) Adds a path for autoloading classes.
* @method static void stop(?int $code = null) Stops the framework and sends a response.
* @method static void halt(int $code = 200, string $message = '', bool $actuallyExit = true)
* @method static void start() Starts the framework.
* @method static void path(string $path) Adds a path for autoloading classes.
* @method static void stop(?int $code = null) Stops the framework and sends a response.
* @method static void halt(int $code = 200, string $message = '', bool $actuallyExit = true)
* Stop the framework with an optional status code and message.
* @method static void registerContainerHandler(callable|object $containerHandler) Registers a container handler.
*
* # Routing
* @method static Route route(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
* @method static Route route(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '')
* Maps a URL pattern to a callback with all applicable methods.
* @method static void group(string $pattern, callable $callback, callable[] $group_middlewares = [])
* Groups a set of routes together under a common prefix.
* @method static Route post(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
* @method static Route post(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '')
* Routes a POST URL to a callback function.
* @method static Route put(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
* @method static Route put(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '')
* Routes a PUT URL to a callback function.
* @method static Route patch(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
* @method static Route patch(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '')
* Routes a PATCH URL to a callback function.
* @method static Route delete(string $pattern, callable $callback, bool $pass_route = false, string $alias = '')
* @method static Route delete(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '')
* Routes a DELETE URL to a callback function.
* @method static Router router() Returns Router instance.
* @method static string getUrl(string $alias, array<string, mixed> $params = []) Gets a url from an alias
Expand Down Expand Up @@ -101,34 +102,6 @@ private function __clone()
{
}

/**
* Registers a class to a framework method.
*
* # Usage example:
* ```
* Flight::register('user', User::class);
*
* Flight::user(); # <- Return a User instance
* ```
*
* @param string $name Static method name
* @param class-string<T> $class Fully Qualified Class Name
* @param array<int, mixed> $params Class constructor params
* @param ?Closure(T $instance): void $callback Perform actions with the instance
*
* @template T of object
*/
public static function register($name, $class, $params = [], $callback = null): void
{
static::__callStatic('register', [$name, $class, $params, $callback]);
}

/** Unregisters a class. */
public static function unregister(string $methodName): void
{
static::__callStatic('unregister', [$methodName]);
}

/**
* Handles calls to static methods.
*
Expand All @@ -140,7 +113,7 @@ public static function unregister(string $methodName): void
*/
public static function __callStatic(string $name, array $params)
{
return Dispatcher::invokeMethod([self::app(), $name], $params);
return self::app()->{$name}(...$params);
}

/** @return Engine Application instance */
Expand Down
Loading

0 comments on commit 8d772b5

Please sign in to comment.