ObjectsProvider.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;
  11. use Symfony\Component\DependencyInjection\Alias;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\EventDispatcher\EventDispatcher;
  17. use Symfony\Component\Routing\CompiledRoute;
  18. use Symfony\Component\Routing\Route;
  19. use Symfony\Component\Routing\RouteCollection;
  20. class ObjectsProvider
  21. {
  22. public static function getRouteCollections()
  23. {
  24. $collection1 = new RouteCollection();
  25. foreach (self::getRoutes() as $name => $route) {
  26. $collection1->add($name, $route);
  27. }
  28. return array('route_collection_1' => $collection1);
  29. }
  30. public static function getRoutes()
  31. {
  32. return array(
  33. 'route_1' => new RouteStub(
  34. '/hello/{name}',
  35. array('name' => 'Joseph'),
  36. array('name' => '[a-z]+'),
  37. array('opt1' => 'val1', 'opt2' => 'val2'),
  38. 'localhost',
  39. array('http', 'https'),
  40. array('get', 'head')
  41. ),
  42. 'route_2' => new RouteStub(
  43. '/name/add',
  44. array(),
  45. array(),
  46. array('opt1' => 'val1', 'opt2' => 'val2'),
  47. 'localhost',
  48. array('http', 'https'),
  49. array('put', 'post')
  50. ),
  51. );
  52. }
  53. public static function getContainerParameters()
  54. {
  55. return array(
  56. 'parameters_1' => new ParameterBag(array(
  57. 'integer' => 12,
  58. 'string' => 'Hello world!',
  59. 'boolean' => true,
  60. 'array' => array(12, 'Hello world!', true),
  61. )),
  62. );
  63. }
  64. public static function getContainerParameter()
  65. {
  66. $builder = new ContainerBuilder();
  67. $builder->setParameter('database_name', 'symfony');
  68. $builder->setParameter('twig.form.resources', array(
  69. 'bootstrap_3_horizontal_layout.html.twig',
  70. 'bootstrap_3_layout.html.twig',
  71. 'form_div_layout.html.twig',
  72. 'form_table_layout.html.twig',
  73. ));
  74. return array(
  75. 'parameter' => $builder,
  76. 'array_parameter' => $builder,
  77. );
  78. }
  79. public static function getContainerBuilders()
  80. {
  81. $builder1 = new ContainerBuilder();
  82. $builder1->setDefinitions(self::getContainerDefinitions());
  83. $builder1->setAliases(self::getContainerAliases());
  84. return array('builder_1' => $builder1);
  85. }
  86. public static function getContainerDefinitions()
  87. {
  88. $definition1 = new Definition('Full\\Qualified\\Class1');
  89. $definition2 = new Definition('Full\\Qualified\\Class2');
  90. return array(
  91. 'definition_1' => $definition1
  92. ->setPublic(true)
  93. ->setSynthetic(false)
  94. ->setLazy(true)
  95. ->setAbstract(true)
  96. ->setFactory(array('Full\\Qualified\\FactoryClass', 'get')),
  97. 'definition_2' => $definition2
  98. ->setPublic(false)
  99. ->setSynthetic(true)
  100. ->setFile('/path/to/file')
  101. ->setLazy(false)
  102. ->setAbstract(false)
  103. ->addTag('tag1', array('attr1' => 'val1', 'attr2' => 'val2'))
  104. ->addTag('tag1', array('attr3' => 'val3'))
  105. ->addTag('tag2')
  106. ->setFactory(array(new Reference('factory.service'), 'get')),
  107. );
  108. }
  109. /**
  110. * @deprecated since version 2.7, to be removed in 3.0
  111. *
  112. * @internal
  113. */
  114. public static function getLegacyContainerDefinitions()
  115. {
  116. $definition1 = new Definition('Full\\Qualified\\Class1');
  117. $definition2 = new Definition('Full\\Qualified\\Class2');
  118. return array(
  119. 'legacy_synchronized_service_definition_1' => $definition1
  120. ->setPublic(true)
  121. ->setSynthetic(false)
  122. ->setLazy(true)
  123. ->setSynchronized(true)
  124. ->setAbstract(true)
  125. ->setFactoryClass('Full\\Qualified\\FactoryClass', 'get')
  126. ->setFactoryMethod('get'),
  127. 'legacy_synchronized_service_definition_2' => $definition2
  128. ->setPublic(false)
  129. ->setSynthetic(true)
  130. ->setFile('/path/to/file')
  131. ->setLazy(false)
  132. ->setSynchronized(false)
  133. ->setAbstract(false)
  134. ->addTag('tag1', array('attr1' => 'val1', 'attr2' => 'val2'))
  135. ->addTag('tag1', array('attr3' => 'val3'))
  136. ->addTag('tag2')
  137. ->setFactoryService('factory.service')
  138. ->setFactoryMethod('get'),
  139. );
  140. }
  141. public static function getContainerAliases()
  142. {
  143. return array(
  144. 'alias_1' => new Alias('service_1', true),
  145. 'alias_2' => new Alias('service_2', false),
  146. );
  147. }
  148. public static function getEventDispatchers()
  149. {
  150. $eventDispatcher = new EventDispatcher();
  151. $eventDispatcher->addListener('event1', 'global_function', 255);
  152. $eventDispatcher->addListener('event1', function () { return 'Closure'; }, -1);
  153. $eventDispatcher->addListener('event2', new CallableClass());
  154. return array('event_dispatcher_1' => $eventDispatcher);
  155. }
  156. public static function getCallables()
  157. {
  158. return array(
  159. 'callable_1' => 'array_key_exists',
  160. 'callable_2' => array('Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\CallableClass', 'staticMethod'),
  161. 'callable_3' => array(new CallableClass(), 'method'),
  162. 'callable_4' => 'Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\CallableClass::staticMethod',
  163. 'callable_5' => array('Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\ExtendedCallableClass', 'parent::staticMethod'),
  164. 'callable_6' => function () { return 'Closure'; },
  165. 'callable_7' => new CallableClass(),
  166. );
  167. }
  168. }
  169. class CallableClass
  170. {
  171. public function __invoke()
  172. {
  173. }
  174. public static function staticMethod()
  175. {
  176. }
  177. public function method()
  178. {
  179. }
  180. }
  181. class ExtendedCallableClass extends CallableClass
  182. {
  183. public static function staticMethod()
  184. {
  185. }
  186. }
  187. class RouteStub extends Route
  188. {
  189. public function compile()
  190. {
  191. return new CompiledRoute('', '#PATH_REGEX#', array(), array(), '#HOST_REGEX#');
  192. }
  193. }