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

Commit

Permalink
Added handling for missing container/router in AppFactory
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
weierophinney committed Dec 7, 2016
1 parent 02c489b commit a6d5518
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/AppFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,31 @@ final class AppFactory
* @param null|Router\RouterInterface $router Router implementation to use;
* defaults to the FastRoute router bridge.
* @return Application
* @throws Exception\MissingDependencyException if the container was not
* provided and the ServiceManager class is not present.
* @throws Exception\MissingDependencyException if the router was not
* provided and the Router\FastRouteRouter class is not present.
*/
public static function create(
ContainerInterface $container = null,
Router\RouterInterface $router = null
) {
if (! $container && ! class_exists(ServiceManager::class)) {
throw new Exception\MissingDependencyException(sprintf(
'%s requires a container, but none was provided and %s is not installed',
__CLASS__,
ServiceManager::class
));
}

if (! $router && ! class_exists(Router\FastRouteRouter::class)) {
throw new Exception\MissingDependencyException(sprintf(
'%s requires a router, but none was provided and %s is not installed',
__CLASS__,
Router\FastRouteRouter::class
));
}

$container = $container ?: new ServiceManager();
$router = $router ?: new Router\FastRouteRouter();
$emitter = new Emitter\EmitterStack();
Expand Down
14 changes: 14 additions & 0 deletions src/Exception/MissingDependencyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Expressive\Exception;

use RuntimeException;

class MissingDependencyException extends RuntimeException implements ExceptionInterface
{
}

0 comments on commit a6d5518

Please sign in to comment.