Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit a6d5518

Browse files
committed
Added handling for missing container/router in AppFactory
Unable to test, because both classes are always present in our test suite! - One tests for `$container` absence, and lack of `ServiceManager` - One tests for `$router` absence, and lack of `Router\FastRouteRouter`
1 parent 02c489b commit a6d5518

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/AppFactory.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,31 @@ final class AppFactory
4040
* @param null|Router\RouterInterface $router Router implementation to use;
4141
* defaults to the FastRoute router bridge.
4242
* @return Application
43+
* @throws Exception\MissingDependencyException if the container was not
44+
* provided and the ServiceManager class is not present.
45+
* @throws Exception\MissingDependencyException if the router was not
46+
* provided and the Router\FastRouteRouter class is not present.
4347
*/
4448
public static function create(
4549
ContainerInterface $container = null,
4650
Router\RouterInterface $router = null
4751
) {
52+
if (! $container && ! class_exists(ServiceManager::class)) {
53+
throw new Exception\MissingDependencyException(sprintf(
54+
'%s requires a container, but none was provided and %s is not installed',
55+
__CLASS__,
56+
ServiceManager::class
57+
));
58+
}
59+
60+
if (! $router && ! class_exists(Router\FastRouteRouter::class)) {
61+
throw new Exception\MissingDependencyException(sprintf(
62+
'%s requires a router, but none was provided and %s is not installed',
63+
__CLASS__,
64+
Router\FastRouteRouter::class
65+
));
66+
}
67+
4868
$container = $container ?: new ServiceManager();
4969
$router = $router ?: new Router\FastRouteRouter();
5070
$emitter = new Emitter\EmitterStack();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
4+
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace Zend\Expressive\Exception;
9+
10+
use RuntimeException;
11+
12+
class MissingDependencyException extends RuntimeException implements ExceptionInterface
13+
{
14+
}

0 commit comments

Comments
 (0)