Commit | Line | Data |
---|---|---|
44d399bc S |
1 | <?php |
2 | /** | |
3 | * Zend Framework (http://framework.zend.com/) | |
4 | * | |
5 | * @link http://github.com/zendframework/zf2 for the canonical source repository | |
6 | * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | |
7 | * @license http://framework.zend.com/license/new-bsd New BSD License | |
8 | */ | |
9 | ||
10 | namespace Zend\Mvc\Service; | |
11 | ||
12 | use Interop\Container\ContainerInterface; | |
13 | use Zend\Mvc\View\Http\ExceptionStrategy; | |
14 | use Zend\ServiceManager\FactoryInterface; | |
15 | use Zend\ServiceManager\ServiceLocatorInterface; | |
16 | ||
17 | class HttpExceptionStrategyFactory implements FactoryInterface | |
18 | { | |
19 | use HttpViewManagerConfigTrait; | |
20 | ||
21 | /** | |
22 | * @param ContainerInterface $container | |
23 | * @param string $name | |
24 | * @param null|array $options | |
25 | * @return ExceptionStrategy | |
26 | */ | |
27 | public function __invoke(ContainerInterface $container, $name, array $options = null) | |
28 | { | |
29 | $strategy = new ExceptionStrategy(); | |
30 | $config = $this->getConfig($container); | |
31 | ||
32 | $this->injectDisplayExceptions($strategy, $config); | |
33 | $this->injectExceptionTemplate($strategy, $config); | |
34 | ||
35 | return $strategy; | |
36 | } | |
37 | ||
38 | /** | |
39 | * Create and return ExceptionStrategy instance | |
40 | * | |
41 | * For use with zend-servicemanager v2; proxies to __invoke(). | |
42 | * | |
43 | * @param ServiceLocatorInterface $container | |
44 | * @return ExceptionStrategy | |
45 | */ | |
46 | public function createService(ServiceLocatorInterface $container) | |
47 | { | |
48 | return $this($container, ExceptionStrategy::class); | |
49 | } | |
50 | ||
51 | /** | |
52 | * Inject strategy with configured display_exceptions flag. | |
53 | * | |
54 | * @param ExceptionStrategy $strategy | |
55 | * @param array $config | |
56 | */ | |
57 | private function injectDisplayExceptions(ExceptionStrategy $strategy, array $config) | |
58 | { | |
59 | $flag = isset($config['display_exceptions']) ? $config['display_exceptions'] : false; | |
60 | $strategy->setDisplayExceptions($flag); | |
61 | } | |
62 | ||
63 | /** | |
64 | * Inject strategy with configured exception_template | |
65 | * | |
66 | * @param ExceptionStrategy $strategy | |
67 | * @param array $config | |
68 | */ | |
69 | private function injectExceptionTemplate(ExceptionStrategy $strategy, array $config) | |
70 | { | |
71 | $template = isset($config['exception_template']) ? $config['exception_template'] : 'error'; | |
72 | $strategy->setExceptionTemplate($template); | |
73 | } | |
74 | } |