3 * Zend Framework (http://framework.zend.com/)
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
10 namespace Zend\Mvc\Service
;
12 use Interop\Container\ContainerInterface
;
14 use Zend\Console\Adapter\AdapterInterface
;
15 use Zend\Console\Console
;
16 use Zend\ServiceManager\FactoryInterface
;
17 use Zend\ServiceManager\ServiceLocatorInterface
;
19 class ConsoleAdapterFactory
implements FactoryInterface
22 * Create and return a Console adapter instance.
23 * In case we're not in a Console environment, return a dummy stdClass object.
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:
29 * 'adapter' => 'MyConsoleAdapter', // always use this console adapter
30 * 'charset' => 'MyConsoleCharset', // always use this console charset
32 * 'service_manager' => array(
33 * 'invokables' => array(
34 * 'MyConsoleAdapter' => 'Zend\Console\Adapter\Windows',
35 * 'MyConsoleCharset' => 'Zend\Console\Charset\DESCG',
39 * @param ContainerInterface $container
41 * @param null|array $options
42 * @return AdapterInterface|stdClass
44 public function __invoke(ContainerInterface
$container, $name, array $options = null)
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();
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']);
58 // try to detect best console adapter
59 $adapter = Console
::detectBestAdapter();
60 $adapter = new $adapter();
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();
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);
80 * Create and return AdapterInterface instance
82 * For use with zend-servicemanager v2; proxies to __invoke().
84 * @param ServiceLocatorInterface $container
85 * @return AdapterInterface|stdClass
87 public function createService(ServiceLocatorInterface
$container)
89 return $this($container, AdapterInterface
::class);