use zend router
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Mvc / Service / ConsoleAdapterFactory.php
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 stdClass;
14 use Zend\Console\Adapter\AdapterInterface;
15 use Zend\Console\Console;
16 use Zend\ServiceManager\FactoryInterface;
17 use Zend\ServiceManager\ServiceLocatorInterface;
18
19 class ConsoleAdapterFactory implements FactoryInterface
20 {
21 /**
22 * Create and return a Console adapter instance.
23 * In case we're not in a Console environment, return a dummy stdClass object.
24 *
25 * In order to disable adapter auto-detection and use a specific adapter (and charset),
26 * add the following fields to application configuration, for example:
27 *
28 * 'console' => array(
29 * 'adapter' => 'MyConsoleAdapter', // always use this console adapter
30 * 'charset' => 'MyConsoleCharset', // always use this console charset
31 * ),
32 * 'service_manager' => array(
33 * 'invokables' => array(
34 * 'MyConsoleAdapter' => 'Zend\Console\Adapter\Windows',
35 * 'MyConsoleCharset' => 'Zend\Console\Charset\DESCG',
36 * )
37 * )
38 *
39 * @param ContainerInterface $container
40 * @param string $name
41 * @param null|array $options
42 * @return AdapterInterface|stdClass
43 */
44 public function __invoke(ContainerInterface $container, $name, array $options = null)
45 {
46 // First, check if we're actually in a Console environment
47 if (! Console::isConsole()) {
48 // SM factory cannot currently return null, so we return dummy object
49 return new stdClass();
50 }
51
52 // Read app config and determine Console adapter to use
53 $config = $container->get('config');
54 if (! empty($config['console']) && ! empty($config['console']['adapter'])) {
55 // use the adapter supplied in application config
56 $adapter = $container->get($config['console']['adapter']);
57 } else {
58 // try to detect best console adapter
59 $adapter = Console::detectBestAdapter();
60 $adapter = new $adapter();
61 }
62
63 // check if we have a valid console adapter
64 if (! $adapter instanceof AdapterInterface) {
65 // SM factory cannot currently return null, so we convert it to dummy object
66 return new stdClass();
67 }
68
69 // Optionally, change Console charset
70 if (! empty($config['console']) && ! empty($config['console']['charset'])) {
71 // use the charset supplied in application config
72 $charset = $container->get($config['console']['charset']);
73 $adapter->setCharset($charset);
74 }
75
76 return $adapter;
77 }
78
79 /**
80 * Create and return AdapterInterface instance
81 *
82 * For use with zend-servicemanager v2; proxies to __invoke().
83 *
84 * @param ServiceLocatorInterface $container
85 * @return AdapterInterface|stdClass
86 */
87 public function createService(ServiceLocatorInterface $container)
88 {
89 return $this($container, AdapterInterface::class);
90 }
91 }