Skip to content

Commit cdf165d

Browse files
authored
Fixed the php-di integration example
The previous version of the example in the README used exceptions that were not imported (NotFoundHttpException) to wrap existing exceptions. I've adapted the example to be similar to the current version of Pecee\SimpleRouter\ClassLoader\ClassLoader. That includes returning strings from loadClassMethod and loadClosure and not wrapping every exception, thrown by the called functions, into NotFoundHttpException exceptions, as well as using the exception ClassNotFoundHttpException when a class cannot be found, instead of the generic NotFoundHttpException. I also changed the way the ClassLoader checks if the container can resolve the class by using the container's method "has" instead of the php function class_exists.
1 parent d3b1577 commit cdf165d

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

README.md

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,7 @@ SimpleRouter::setCustomClassLoader(new MyCustomClassLoader());
17421742
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:
17431743

17441744
```php
1745+
use Pecee\SimpleRouter\ClassLoader\IClassLoader;
17451746
use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException;
17461747

17471748
class MyCustomClassLoader implements IClassLoader
@@ -1762,51 +1763,38 @@ class MyCustomClassLoader implements IClassLoader
17621763
*
17631764
* @param string $class
17641765
* @return object
1765-
* @throws NotFoundHttpException
1766+
* @throws ClassNotFoundHttpException
17661767
*/
17671768
public function loadClass(string $class)
17681769
{
1769-
if (class_exists($class) === false) {
1770-
throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404);
1770+
if ($this->container->has($class) === false) {
1771+
throw new ClassNotFoundHttpException($class, null, sprintf('Class "%s" does not exist', $class), 404, null);
17711772
}
1772-
1773-
try {
1774-
return $this->container->get($class);
1775-
} catch (\Exception $e) {
1776-
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
1777-
}
1773+
return $this->container->get($class);
17781774
}
17791775

17801776
/**
17811777
* Called when loading class method
17821778
* @param object $class
17831779
* @param string $method
17841780
* @param array $parameters
1785-
* @return object
1781+
* @return string
17861782
*/
17871783
public function loadClassMethod($class, string $method, array $parameters)
17881784
{
1789-
try {
1790-
return $this->container->call([$class, $method], $parameters);
1791-
} catch (\Exception $e) {
1792-
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
1793-
}
1785+
return (string)$this->container->call([$class, $method], $parameters);
17941786
}
17951787

17961788
/**
17971789
* Load closure
17981790
*
17991791
* @param Callable $closure
18001792
* @param array $parameters
1801-
* @return mixed
1793+
* @return string
18021794
*/
18031795
public function loadClosure(callable $closure, array $parameters)
18041796
{
1805-
try {
1806-
return $this->container->call($closure, $parameters);
1807-
} catch (\Exception $e) {
1808-
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
1809-
}
1797+
return (string)$this->container->call($closure, $parameters);
18101798
}
18111799
}
18121800
```

0 commit comments

Comments
 (0)