Helix SwarmHelix Swarm

ConsoleAdapterFactory.php

  • //
  • guest/
  • thomas_gray/
  • jambox/
  • main/
  • swarm/
  • library/
  • Zend/
  • Mvc/
  • Service/
  • ConsoleAdapterFactory.php #1
  • View
  • Commits
  • Open Download .zip Download (3 KB)
  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-2014 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 stdClass;
  13. use Zend\Console\Adapter\AdapterInterface;
  14. use Zend\Console\Console;
  15. use Zend\ServiceManager\FactoryInterface;
  16. use Zend\ServiceManager\ServiceLocatorInterface;
  17.  
  18. class ConsoleAdapterFactory implements FactoryInterface
  19. {
  20. /**
  21. * Create and return a Console adapter instance.
  22. * In case we're not in a Console environment, return a dummy stdClass object.
  23. *
  24. * In order to disable adapter auto-detection and use a specific adapter (and charset),
  25. * add the following fields to application configuration, for example:
  26. *
  27. * 'console' => array(
  28. * 'adapter' => 'MyConsoleAdapter', // always use this console adapter
  29. * 'charset' => 'MyConsoleCharset', // always use this console charset
  30. * ),
  31. * 'service_manager' => array(
  32. * 'invokables' => array(
  33. * 'MyConsoleAdapter' => 'Zend\Console\Adapter\Windows',
  34. * 'MyConsoleCharset' => 'Zend\Console\Charset\DESCG',
  35. * )
  36. * )
  37. *
  38. * @param ServiceLocatorInterface $serviceLocator
  39. * @return AdapterInterface|stdClass
  40. */
  41. public function createService(ServiceLocatorInterface $serviceLocator)
  42. {
  43. // First, check if we're actually in a Console environment
  44. if (!Console::isConsole()) {
  45. // SM factory cannot currently return null, so we return dummy object
  46. return new stdClass();
  47. }
  48.  
  49. // Read app config and determine Console adapter to use
  50. $config = $serviceLocator->get('Config');
  51. if (!empty($config['console']) && !empty($config['console']['adapter'])) {
  52. // use the adapter supplied in application config
  53. $adapter = $serviceLocator->get($config['console']['adapter']);
  54. } else {
  55. // try to detect best console adapter
  56. $adapter = Console::detectBestAdapter();
  57. $adapter = new $adapter();
  58. }
  59.  
  60. // check if we have a valid console adapter
  61. if (!$adapter instanceof AdapterInterface) {
  62. // SM factory cannot currently return null, so we convert it to dummy object
  63. return new stdClass();
  64. }
  65.  
  66. // Optionally, change Console charset
  67. if (!empty($config['console']) && !empty($config['console']['charset'])) {
  68. // use the charset supplied in application config
  69. $charset = $serviceLocator->get($config['console']['charset']);
  70. $adapter->setCharset($charset);
  71. }
  72.  
  73. return $adapter;
  74. }
  75. }
# Change User Description Committed
#1 18334 Liz Lam initial add of jambox 9 years ago