mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 18:02:09 +00:00
Merge pull request #682 from ms-afk/fix-readme-php-di
Fixed the php-di integration example in the README
This commit is contained in:
@@ -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-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
|
```php
|
||||||
|
use Pecee\SimpleRouter\ClassLoader\IClassLoader;
|
||||||
use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException;
|
use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException;
|
||||||
|
|
||||||
class MyCustomClassLoader implements IClassLoader
|
class MyCustomClassLoader implements IClassLoader
|
||||||
@@ -1762,19 +1763,14 @@ class MyCustomClassLoader implements IClassLoader
|
|||||||
*
|
*
|
||||||
* @param string $class
|
* @param string $class
|
||||||
* @return object
|
* @return object
|
||||||
* @throws NotFoundHttpException
|
* @throws ClassNotFoundHttpException
|
||||||
*/
|
*/
|
||||||
public function loadClass(string $class)
|
public function loadClass(string $class)
|
||||||
{
|
{
|
||||||
if (class_exists($class) === false) {
|
if ($this->container->has($class) === false) {
|
||||||
throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404);
|
throw new ClassNotFoundHttpException($class, null, sprintf('Class "%s" does not exist', $class), 404, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
return $this->container->get($class);
|
return $this->container->get($class);
|
||||||
} catch (\Exception $e) {
|
|
||||||
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1782,15 +1778,11 @@ class MyCustomClassLoader implements IClassLoader
|
|||||||
* @param object $class
|
* @param object $class
|
||||||
* @param string $method
|
* @param string $method
|
||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return object
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function loadClassMethod($class, string $method, array $parameters)
|
public function loadClassMethod($class, string $method, array $parameters)
|
||||||
{
|
{
|
||||||
try {
|
return (string)$this->container->call([$class, $method], $parameters);
|
||||||
return $this->container->call([$class, $method], $parameters);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1798,15 +1790,11 @@ class MyCustomClassLoader implements IClassLoader
|
|||||||
*
|
*
|
||||||
* @param Callable $closure
|
* @param Callable $closure
|
||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return mixed
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function loadClosure(callable $closure, array $parameters)
|
public function loadClosure(callable $closure, array $parameters)
|
||||||
{
|
{
|
||||||
try {
|
return (string)$this->container->call($closure, $parameters);
|
||||||
return $this->container->call($closure, $parameters);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user