From be2d45f0ad266630759ea342a06c039e15eea016 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 29 Mar 2021 22:24:02 +0200 Subject: [PATCH] Updated documentation --- README.md | 65 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 1938140..4ef8f16 100644 --- a/README.md +++ b/README.md @@ -1495,6 +1495,18 @@ class MyCustomClassLoader implements IClassLoader return new $class(); } + + /** + * Called when loading class method + * @param object $class + * @param string $method + * @param array $parameters + * @return object + */ + public function loadClassMethod($class, string $method, array $parameters) + { + return call_user_func_array([$class, $method], array_values($parameters)); + } /** * Load closure @@ -1505,7 +1517,7 @@ class MyCustomClassLoader implements IClassLoader */ public function loadClosure(Callable $closure, array $parameters) { - return \call_user_func_array($closure, $parameters); + return \call_user_func_array($closure, array_values($parameters)); } } @@ -1522,6 +1534,8 @@ SimpleRouter::setCustomClassLoader(new MyCustomClassLoader()); php-di support was discontinued by version 4.3, however you can easily add it again by creating your own class-loader like the example below: ```php +use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException; + class MyCustomClassLoader implements IClassLoader { @@ -1530,7 +1544,7 @@ class MyCustomClassLoader implements IClassLoader public function __construct() { // Create our new php-di container - $container = (new \DI\ContainerBuilder()) + $this->container = (new \DI\ContainerBuilder()) ->useAutowiring(true) ->build(); } @@ -1548,15 +1562,27 @@ class MyCustomClassLoader implements IClassLoader throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404); } - if ($this->container !== null) { - try { - return $this->container->get($class); - } catch (\Exception $e) { - throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious()); - } - } - - return new $class(); + try { + return $this->container->get($class); + } catch (\Exception $e) { + throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious()); + } + } + + /** + * Called when loading class method + * @param object $class + * @param string $method + * @param array $parameters + * @return object + */ + public function loadClassMethod($class, string $method, array $parameters) + { + try { + return $this->container->call([$class, $method], $parameters); + } catch (\Exception $e) { + throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious()); + } } /** @@ -1566,19 +1592,14 @@ class MyCustomClassLoader implements IClassLoader * @param array $parameters * @return mixed */ - public function loadClosure(Callable $closure, array $parameters) + public function loadClosure(callable $closure, array $parameters) { - if ($this->container !== null) { - try { - return $this->container->call($closure, $parameters); - } catch (\Exception $e) { - throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious()); - } - } - - return \call_user_func_array($closure, $parameters); + try { + return $this->container->call($closure, $parameters); + } catch (\Exception $e) { + throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious()); + } } - } ```