Skip to content

Commit

Permalink
Merge pull request #682 from ms-afk/fix-readme-php-di
Browse files Browse the repository at this point in the history
Fixed the php-di integration example in the README
  • Loading branch information
skipperbent authored Nov 21, 2023
2 parents 49b132d + cdf165d commit 0970bd0
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,7 @@ 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\ClassLoader\IClassLoader;
use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException;

class MyCustomClassLoader implements IClassLoader
Expand All @@ -1762,51 +1763,38 @@ class MyCustomClassLoader implements IClassLoader
*
* @param string $class
* @return object
* @throws NotFoundHttpException
* @throws ClassNotFoundHttpException
*/
public function loadClass(string $class)
{
if (class_exists($class) === false) {
throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404);
if ($this->container->has($class) === false) {
throw new ClassNotFoundHttpException($class, null, sprintf('Class "%s" does not exist', $class), 404, null);
}

try {
return $this->container->get($class);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
return $this->container->get($class);
}

/**
* Called when loading class method
* @param object $class
* @param string $method
* @param array $parameters
* @return object
* @return string
*/
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());
}
return (string)$this->container->call([$class, $method], $parameters);
}

/**
* Load closure
*
* @param Callable $closure
* @param array $parameters
* @return mixed
* @return string
*/
public function loadClosure(callable $closure, array $parameters)
{
try {
return $this->container->call($closure, $parameters);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
return (string)$this->container->call($closure, $parameters);
}
}
```
Expand Down

0 comments on commit 0970bd0

Please sign in to comment.