PhpDumper.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Component\DependencyInjection\Dumper;
  11. use Symfony\Component\DependencyInjection\Variable;
  12. use Symfony\Component\DependencyInjection\Definition;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Container;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. use Symfony\Component\DependencyInjection\Parameter;
  18. /**
  19. * PhpDumper dumps a service container as a PHP class.
  20. *
  21. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  23. */
  24. class PhpDumper extends Dumper
  25. {
  26. /**
  27. * Characters that might appear in the generated variable name as first character
  28. * @var string
  29. */
  30. const FIRST_CHARS = 'abcdefghijklmnopqrstuvwxyz';
  31. /**
  32. * Characters that might appear in the generated variable name as any but the first character
  33. * @var string
  34. */
  35. const NON_FIRST_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789_';
  36. protected $inlinedDefinitions;
  37. protected $definitionVariables;
  38. protected $referenceVariables;
  39. protected $variableCount;
  40. protected $reservedVariables = array('instance', 'class');
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function __construct(ContainerBuilder $container)
  45. {
  46. parent::__construct($container);
  47. $this->inlinedDefinitions = new \SplObjectStorage;
  48. }
  49. /**
  50. * Dumps the service container as a PHP class.
  51. *
  52. * Available options:
  53. *
  54. * * class: The class name
  55. * * base_class: The base class name
  56. *
  57. * @param array $options An array of options
  58. *
  59. * @return string A PHP class representing of the service container
  60. */
  61. public function dump(array $options = array())
  62. {
  63. $options = array_merge(array(
  64. 'class' => 'ProjectServiceContainer',
  65. 'base_class' => 'Container',
  66. ), $options);
  67. $code = $this->startClass($options['class'], $options['base_class']);
  68. if ($this->container->isFrozen()) {
  69. $code .= $this->addFrozenConstructor();
  70. } else {
  71. $code .= $this->addConstructor();
  72. }
  73. $code .=
  74. $this->addServices().
  75. $this->addDefaultParametersMethod().
  76. $this->addInterfaceInjectors().
  77. $this->endClass()
  78. ;
  79. return $code;
  80. }
  81. /**
  82. * Returns applyInterfaceInjectors function for the dumper.
  83. *
  84. * @return string
  85. */
  86. protected function addInterfaceInjectors()
  87. {
  88. if ($this->container->isFrozen() || 0 === count($this->container->getInterfaceInjectors())) {
  89. return;
  90. }
  91. $code = <<<EOF
  92. /**
  93. * Applies all known interface injection calls
  94. *
  95. * @param Object \$instance
  96. */
  97. protected function applyInterfaceInjectors(\$instance)
  98. {
  99. EOF;
  100. foreach ($this->container->getInterfaceInjectors() as $injector) {
  101. $code .= sprintf(" if (\$instance instanceof \\%s) {\n", $injector->getClass());
  102. foreach ($injector->getMethodCalls() as $call) {
  103. foreach ($call[1] as $value) {
  104. $arguments[] = $this->dumpValue($value);
  105. }
  106. $code .= $this->wrapServiceConditionals($call[1], sprintf(" \$instance->%s(%s);\n", $call[0], implode(', ', $arguments)));
  107. }
  108. $code .= sprintf(" }\n");
  109. }
  110. $code .= <<<EOF
  111. }
  112. EOF;
  113. return $code;
  114. }
  115. /**
  116. * Generates Service local temp variables.
  117. *
  118. * @param string $cId
  119. * @param string $definition
  120. * @return string
  121. */
  122. protected function addServiceLocalTempVariables($cId, $definition)
  123. {
  124. static $template = " \$%s = %s;\n";
  125. $localDefinitions = array_merge(
  126. array($definition),
  127. $this->getInlinedDefinitions($definition)
  128. );
  129. $calls = $behavior = array();
  130. foreach ($localDefinitions as $iDefinition) {
  131. $this->getServiceCallsFromArguments($iDefinition->getArguments(), $calls, $behavior);
  132. $this->getServiceCallsFromArguments($iDefinition->getMethodCalls(), $calls, $behavior);
  133. $this->getServiceCallsFromArguments($iDefinition->getProperties(), $calls, $behavior);
  134. }
  135. $code = '';
  136. foreach ($calls as $id => $callCount) {
  137. if ('service_container' === $id || $id === $cId) {
  138. continue;
  139. }
  140. if ($callCount > 1) {
  141. $name = $this->getNextVariableName();
  142. $this->referenceVariables[$id] = new Variable($name);
  143. if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $behavior[$id]) {
  144. $code .= sprintf($template, $name, $this->getServiceCall($id));
  145. } else {
  146. $code .= sprintf($template, $name, $this->getServiceCall($id, new Reference($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)));
  147. }
  148. }
  149. }
  150. if ('' !== $code) {
  151. $code .= "\n";
  152. }
  153. return $code;
  154. }
  155. /**
  156. * Generates the require_once statement for service includes.
  157. *
  158. * @param string $id The service id
  159. * @param Definition $definition
  160. * @return string
  161. */
  162. protected function addServiceInclude($id, $definition)
  163. {
  164. $template = " require_once %s;\n";
  165. $code = '';
  166. if (null !== $file = $definition->getFile()) {
  167. $code .= sprintf($template, $this->dumpValue($file));
  168. }
  169. foreach ($this->getInlinedDefinitions($definition) as $definition) {
  170. if (null !== $file = $definition->getFile()) {
  171. $code .= sprintf($template, $this->dumpValue($file));
  172. }
  173. }
  174. if ('' !== $code) {
  175. $code .= "\n";
  176. }
  177. return $code;
  178. }
  179. /**
  180. * Generates the inline definition of a service.
  181. *
  182. * @param string $id
  183. * @param Definition $definition
  184. * @return string
  185. */
  186. protected function addServiceInlinedDefinitions($id, $definition)
  187. {
  188. $code = '';
  189. $variableMap = $this->definitionVariables;
  190. $nbOccurrences = new \SplObjectStorage();
  191. $processed = new \SplObjectStorage();
  192. $inlinedDefinitions = $this->getInlinedDefinitions($definition);
  193. foreach ($inlinedDefinitions as $definition) {
  194. if (false === $nbOccurrences->contains($definition)) {
  195. $nbOccurrences->offsetSet($definition, 1);
  196. } else {
  197. $i = $nbOccurrences->offsetGet($definition);
  198. $nbOccurrences->offsetSet($definition, $i+1);
  199. }
  200. }
  201. foreach ($inlinedDefinitions as $sDefinition) {
  202. if ($processed->contains($sDefinition)) {
  203. continue;
  204. }
  205. $processed->offsetSet($sDefinition);
  206. $class = $this->dumpValue($sDefinition->getClass());
  207. if ($nbOccurrences->offsetGet($sDefinition) > 1 || count($sDefinition->getMethodCalls()) > 0 || $sDefinition->getProperties() || null !== $sDefinition->getConfigurator() || false !== strpos($class, '$')) {
  208. $name = $this->getNextVariableName();
  209. $variableMap->offsetSet($sDefinition, new Variable($name));
  210. // a construct like:
  211. // $a = new ServiceA(ServiceB $b); $b = new ServiceB(ServiceA $a);
  212. // this is an indication for a wrong implementation, you can circumvent this problem
  213. // by setting up your service structure like this:
  214. // $b = new ServiceB();
  215. // $a = new ServiceA(ServiceB $b);
  216. // $b->setServiceA(ServiceA $a);
  217. if ($this->hasReference($id, $sDefinition->getArguments())) {
  218. throw new \RuntimeException('Unresolvable reference detected in service definition for '.$id);
  219. }
  220. $arguments = array();
  221. foreach ($sDefinition->getArguments() as $argument) {
  222. $arguments[] = $this->dumpValue($argument);
  223. }
  224. if (null !== $sDefinition->getFactoryMethod()) {
  225. if (null !== $sDefinition->getFactoryClass()) {
  226. $code .= sprintf(" \$%s = call_user_func(array(%s, '%s')%s);\n", $name, $this->dumpValue($sDefinition->getFactoryClass()), $sDefinition->getFactoryMethod(), count($arguments) > 0 ? ', '.implode(', ', $arguments) : '');
  227. } elseif (null !== $sDefinition->getFactoryService()) {
  228. $code .= sprintf(" \$%s = %s->%s(%s);\n", $name, $this->getServiceCall($sDefinition->getFactoryService()), $sDefinition->getFactoryMethod(), implode(', ', $arguments));
  229. } else {
  230. throw new \RuntimeException('Factory service or factory class must be defined in service definition for '.$id);
  231. }
  232. } elseif (false !== strpos($class, '$')) {
  233. $code .= sprintf(" \$class = %s;\n \$%s = new \$class(%s);\n", $class, $name, implode(', ', $arguments));
  234. } else {
  235. $code .= sprintf(" \$%s = new \\%s(%s);\n", $name, substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
  236. }
  237. if (!$this->hasReference($id, $sDefinition->getMethodCalls()) && !$this->hasReference($id, $sDefinition->getProperties())) {
  238. $code .= $this->addServiceMethodCalls(null, $sDefinition, $name);
  239. $code .= $this->addServiceProperties(null, $sDefinition, $name);
  240. $code .= $this->addServiceConfigurator(null, $sDefinition, $name);
  241. }
  242. $code .= "\n";
  243. }
  244. }
  245. return $code;
  246. }
  247. /**
  248. * Adds the service return statement.
  249. *
  250. * @param string $id Service id
  251. * @param Definition $definition
  252. * @return string
  253. */
  254. protected function addServiceReturn($id, $definition)
  255. {
  256. if ($this->isSimpleInstance($id, $definition)) {
  257. return " }\n";
  258. }
  259. return "\n return \$instance;\n }\n";
  260. }
  261. /**
  262. * Generates the service instance.
  263. *
  264. * @param string $id
  265. * @param Definition $definition
  266. * @return string
  267. *
  268. * @throws \InvalidArgumentException
  269. * @throws \RuntimeException
  270. */
  271. protected function addServiceInstance($id, $definition)
  272. {
  273. $class = $this->dumpValue($definition->getClass());
  274. if (0 === strpos($class, "'") && !preg_match('/^\'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
  275. throw new \InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
  276. }
  277. $arguments = array();
  278. foreach ($definition->getArguments() as $value) {
  279. $arguments[] = $this->dumpValue($value);
  280. }
  281. $simple = $this->isSimpleInstance($id, $definition);
  282. $instantiation = '';
  283. if (ContainerInterface::SCOPE_CONTAINER === $definition->getScope()) {
  284. $instantiation = "\$this->services['$id'] = ".($simple ? '' : '$instance');
  285. } else if (ContainerInterface::SCOPE_PROTOTYPE !== $scope = $definition->getScope()) {
  286. $instantiation = "\$this->services['$id'] = \$this->scopedServices['$scope']['$id'] = ".($simple ? '' : '$instance');
  287. } elseif (!$simple) {
  288. $instantiation = '$instance';
  289. }
  290. $return = '';
  291. if ($simple) {
  292. $return = 'return ';
  293. } else {
  294. $instantiation .= ' = ';
  295. }
  296. if (null !== $definition->getFactoryMethod()) {
  297. if (null !== $definition->getFactoryClass()) {
  298. $code = sprintf(" $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($definition->getFactoryClass()), $definition->getFactoryMethod(), $arguments ? ', '.implode(', ', $arguments) : '');
  299. } elseif (null !== $definition->getFactoryService()) {
  300. $code = sprintf(" $return{$instantiation}%s->%s(%s);\n", $this->getServiceCall($definition->getFactoryService()), $definition->getFactoryMethod(), implode(', ', $arguments));
  301. } else {
  302. throw new \RuntimeException('Factory method requires a factory service or factory class in service definition for '.$id);
  303. }
  304. } elseif (false !== strpos($class, '$')) {
  305. $code = sprintf(" \$class = %s;\n $return{$instantiation}new \$class(%s);\n", $class, implode(', ', $arguments));
  306. } else {
  307. $code = sprintf(" $return{$instantiation}new \\%s(%s);\n", substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
  308. }
  309. if (!$simple) {
  310. $code .= "\n";
  311. }
  312. return $code;
  313. }
  314. /**
  315. * Checks if the definition is a simple instance.
  316. *
  317. * @param string $id
  318. * @param Definition $definition
  319. * @return boolean
  320. */
  321. protected function isSimpleInstance($id, $definition)
  322. {
  323. foreach (array_merge(array($definition), $this->getInlinedDefinitions($definition)) as $sDefinition) {
  324. if ($definition !== $sDefinition && !$this->hasReference($id, $sDefinition->getMethodCalls())) {
  325. continue;
  326. }
  327. if ($sDefinition->getMethodCalls() || $sDefinition->getProperties() || $sDefinition->getConfigurator()) {
  328. return false;
  329. }
  330. }
  331. return true;
  332. }
  333. /**
  334. * Adds method calls to a service definition.
  335. *
  336. * @param string $id
  337. * @param Definition $definition
  338. * @param string $variableName
  339. * @return string
  340. */
  341. protected function addServiceMethodCalls($id, $definition, $variableName = 'instance')
  342. {
  343. $calls = '';
  344. foreach ($definition->getMethodCalls() as $call) {
  345. $arguments = array();
  346. foreach ($call[1] as $value) {
  347. $arguments[] = $this->dumpValue($value);
  348. }
  349. $calls .= $this->wrapServiceConditionals($call[1], sprintf(" \$%s->%s(%s);\n", $variableName, $call[0], implode(', ', $arguments)));
  350. }
  351. if (!$this->container->isFrozen() && count($this->container->getInterfaceInjectors()) > 0) {
  352. $calls = sprintf("\n \$this->applyInterfaceInjectors(\$%s);\n", $variableName);
  353. }
  354. return $calls;
  355. }
  356. protected function addServiceProperties($id, $definition, $variableName = 'instance')
  357. {
  358. $code = '';
  359. foreach ($definition->getProperties() as $name => $value) {
  360. $code .= sprintf(" \$%s = new \ReflectionProperty(\$%s, %s);\n", $refName = $this->getNextVariableName(), $variableName, var_export($name, true));
  361. $code .= sprintf(" \$%s->setAccessible(true);\n", $refName);
  362. $code .= sprintf(" \$%s->setValue(\$%s, %s);\n", $refName, $variableName, $this->dumpValue($value));
  363. }
  364. return $code;
  365. }
  366. /**
  367. * Generates the inline definition setup.
  368. *
  369. * @param string $id
  370. * @param Definition $definition
  371. * @return string
  372. */
  373. protected function addServiceInlinedDefinitionsSetup($id, $definition)
  374. {
  375. $this->referenceVariables[$id] = new Variable('instance');
  376. $code = '';
  377. $processed = new \SplObjectStorage();
  378. foreach ($this->getInlinedDefinitions($definition) as $iDefinition) {
  379. if ($processed->contains($iDefinition)) {
  380. continue;
  381. }
  382. $processed->offsetSet($iDefinition);
  383. if (!$this->hasReference($id, $iDefinition->getMethodCalls())) {
  384. continue;
  385. }
  386. if ($iDefinition->getMethodCalls()) {
  387. $code .= $this->addServiceMethodCalls(null, $iDefinition, (string) $this->definitionVariables->offsetGet($iDefinition));
  388. }
  389. if ($iDefinition->getConfigurator()) {
  390. $code .= $this->addServiceConfigurator(null, $iDefinition, (string) $this->definitionVariables->offsetGet($iDefinition));
  391. }
  392. }
  393. if ('' !== $code) {
  394. $code .= "\n";
  395. }
  396. return $code;
  397. }
  398. /**
  399. * Adds configurator definition
  400. *
  401. * @param string $id
  402. * @param Definition $definition
  403. * @param string $variableName
  404. * @return string
  405. */
  406. protected function addServiceConfigurator($id, $definition, $variableName = 'instance')
  407. {
  408. if (!$callable = $definition->getConfigurator()) {
  409. return '';
  410. }
  411. if (is_array($callable)) {
  412. if (is_object($callable[0]) && $callable[0] instanceof Reference) {
  413. return sprintf(" %s->%s(\$%s);\n", $this->getServiceCall((string) $callable[0]), $callable[1], $variableName);
  414. }
  415. return sprintf(" call_user_func(array(%s, '%s'), \$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
  416. }
  417. return sprintf(" %s(\$%s);\n", $callable, $variableName);
  418. }
  419. /**
  420. * Adds a service
  421. *
  422. * @param string $id
  423. * @param Definition $definition
  424. * @return string
  425. */
  426. protected function addService($id, $definition)
  427. {
  428. $name = Container::camelize($id);
  429. $this->definitionVariables = new \SplObjectStorage();
  430. $this->referenceVariables = array();
  431. $this->variableCount = 0;
  432. $return = '';
  433. if ($definition->isSynthetic()) {
  434. $return = sprintf('@throws \RuntimeException always since this service is expected to be injected dynamically');
  435. } elseif ($class = $definition->getClass()) {
  436. $return = sprintf("@return %s A %s instance.", 0 === strpos($class, '%') ? 'Object' : $class, $class);
  437. } elseif ($definition->getFactoryClass()) {
  438. $return = sprintf('@return Object An instance returned by %s::%s().', $definition->getFactoryClass(), $definition->getFactoryMethod());
  439. } elseif ($definition->getFactoryService()) {
  440. $return = sprintf('@return Object An instance returned by %s::%s().', $definition->getFactoryService(), $definition->getFactoryMethod());
  441. }
  442. $doc = '';
  443. if (ContainerInterface::SCOPE_PROTOTYPE !== $definition->getScope()) {
  444. $doc .= <<<EOF
  445. *
  446. * This service is shared.
  447. * This method always returns the same instance of the service.
  448. EOF;
  449. }
  450. if (!$definition->isPublic()) {
  451. $doc .= <<<EOF
  452. *
  453. * This service is private.
  454. * If you want to be able to request this service from the container directly,
  455. * make it public, otherwise you might end up with broken code.
  456. EOF;
  457. }
  458. $code = <<<EOF
  459. /**
  460. * Gets the '$id' service.$doc
  461. *
  462. * $return
  463. */
  464. protected function get{$name}Service()
  465. {
  466. EOF;
  467. $scope = $definition->getScope();
  468. if (ContainerInterface::SCOPE_CONTAINER !== $scope && ContainerInterface::SCOPE_PROTOTYPE !== $scope) {
  469. $code .= <<<EOF
  470. if (!isset(\$this->scopedServices['$scope'])) {
  471. throw new \RuntimeException('You cannot create a service ("$id") of an inactive scope ("$scope").');
  472. }
  473. EOF;
  474. }
  475. if ($definition->isSynthetic()) {
  476. $code .= sprintf(" throw new \RuntimeException('You have requested a synthetic service (\"%s\"). The DIC does not know how to construct this service.');\n }\n", $id);
  477. } else {
  478. $code .=
  479. $this->addServiceInclude($id, $definition).
  480. $this->addServiceLocalTempVariables($id, $definition).
  481. $this->addServiceInlinedDefinitions($id, $definition).
  482. $this->addServiceInstance($id, $definition).
  483. $this->addServiceInlinedDefinitionsSetup($id, $definition).
  484. $this->addServiceMethodCalls($id, $definition).
  485. $this->addServiceProperties($id, $definition).
  486. $this->addServiceConfigurator($id, $definition).
  487. $this->addServiceReturn($id, $definition)
  488. ;
  489. }
  490. $this->definitionVariables = null;
  491. $this->referenceVariables = null;
  492. return $code;
  493. }
  494. /**
  495. * Adds a service alias.
  496. *
  497. * @param string $alias
  498. * @param string $id
  499. * @return string
  500. */
  501. protected function addServiceAlias($alias, $id)
  502. {
  503. $name = Container::camelize($alias);
  504. $type = 'Object';
  505. if ($this->container->hasDefinition($id)) {
  506. $class = $this->container->getDefinition($id)->getClass();
  507. $type = 0 === strpos($class, '%') ? 'Object' : $class;
  508. }
  509. return <<<EOF
  510. /**
  511. * Gets the $alias service alias.
  512. *
  513. * @return $type An instance of the $id service
  514. */
  515. protected function get{$name}Service()
  516. {
  517. return {$this->getServiceCall($id)};
  518. }
  519. EOF;
  520. }
  521. /**
  522. * Adds multiple services
  523. *
  524. * @return string
  525. */
  526. protected function addServices()
  527. {
  528. $publicServices = $privateServices = $aliasServices = '';
  529. $definitions = $this->container->getDefinitions();
  530. ksort($definitions);
  531. foreach ($definitions as $id => $definition) {
  532. if ($definition->isPublic()) {
  533. $publicServices .= $this->addService($id, $definition);
  534. } else {
  535. $privateServices .= $this->addService($id, $definition);
  536. }
  537. }
  538. $aliases = $this->container->getAliases();
  539. ksort($aliases);
  540. foreach ($aliases as $alias => $id) {
  541. $aliasServices .= $this->addServiceAlias($alias, $id);
  542. }
  543. return $publicServices.$aliasServices.$privateServices;
  544. }
  545. /**
  546. * Adds the class headers.
  547. *
  548. * @param string $class Class name
  549. * @param string $baseClass The name of the base class
  550. * @return string
  551. */
  552. protected function startClass($class, $baseClass)
  553. {
  554. $bagClass = $this->container->isFrozen() ? '' : 'use Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag;';
  555. return <<<EOF
  556. <?php
  557. use Symfony\Component\DependencyInjection\ContainerInterface;
  558. use Symfony\Component\DependencyInjection\Container;
  559. use Symfony\Component\DependencyInjection\Reference;
  560. use Symfony\Component\DependencyInjection\Parameter;
  561. $bagClass
  562. /**
  563. * $class
  564. *
  565. * This class has been auto-generated
  566. * by the Symfony Dependency Injection Component.
  567. */
  568. class $class extends $baseClass
  569. {
  570. EOF;
  571. }
  572. /**
  573. * Adds the constructor.
  574. *
  575. * @return string
  576. */
  577. protected function addConstructor()
  578. {
  579. $code = <<<EOF
  580. /**
  581. * Constructor.
  582. */
  583. public function __construct()
  584. {
  585. parent::__construct(new ParameterBag(\$this->getDefaultParameters()));
  586. EOF;
  587. if (count($scopes = $this->container->getScopes()) > 0) {
  588. $code .= "\n";
  589. $code .= " \$this->scopes = ".$this->dumpValue($scopes).";\n";
  590. $code .= " \$this->scopeChildren = ".$this->dumpValue($this->container->getScopeChildren()).";\n";
  591. }
  592. $code .= <<<EOF
  593. }
  594. EOF;
  595. return $code;
  596. }
  597. /**
  598. * Adds the constructor for a frozen container.
  599. *
  600. * @return string
  601. */
  602. protected function addFrozenConstructor()
  603. {
  604. $code = <<<EOF
  605. /**
  606. * Constructor.
  607. */
  608. public function __construct()
  609. {
  610. \$this->parameters = \$this->getDefaultParameters();
  611. \$this->services =
  612. \$this->scopedServices =
  613. \$this->scopeStacks = array();
  614. \$this->set('service_container', \$this);
  615. EOF;
  616. $code .= "\n";
  617. if (count($scopes = $this->container->getScopes()) > 0) {
  618. $code .= " \$this->scopes = ".$this->dumpValue($scopes).";\n";
  619. $code .= " \$this->scopeChildren = ".$this->dumpValue($this->container->getScopeChildren()).";\n";
  620. } else {
  621. $code .= " \$this->scopes = array();\n";
  622. $code .= " \$this->scopeChildren = array();\n";
  623. }
  624. $code .= <<<EOF
  625. }
  626. EOF;
  627. return $code;
  628. }
  629. /**
  630. * Adds default parameters method.
  631. *
  632. * @return string
  633. */
  634. protected function addDefaultParametersMethod()
  635. {
  636. if (!$this->container->getParameterBag()->all()) {
  637. return '';
  638. }
  639. $parameters = $this->exportParameters($this->container->getParameterBag()->all());
  640. $code = '';
  641. if ($this->container->isFrozen()) {
  642. $code .= <<<EOF
  643. /**
  644. * {@inheritdoc}
  645. */
  646. public function getParameter(\$name)
  647. {
  648. \$name = strtolower(\$name);
  649. if (!array_key_exists(\$name, \$this->parameters)) {
  650. throw new \InvalidArgumentException(sprintf('The parameter "%s" must be defined.', \$name));
  651. }
  652. return \$this->parameters[\$name];
  653. }
  654. /**
  655. * {@inheritdoc}
  656. */
  657. public function hasParameter(\$name)
  658. {
  659. return array_key_exists(strtolower(\$name), \$this->parameters);
  660. }
  661. /**
  662. * {@inheritdoc}
  663. */
  664. public function setParameter(\$name, \$value)
  665. {
  666. throw new \LogicException('Impossible to call set() on a frozen ParameterBag.');
  667. }
  668. EOF;
  669. }
  670. $code .= <<<EOF
  671. /**
  672. * Gets the default parameters.
  673. *
  674. * @return array An array of the default parameters
  675. */
  676. protected function getDefaultParameters()
  677. {
  678. return $parameters;
  679. }
  680. EOF;
  681. return $code;
  682. }
  683. /**
  684. * Exports parameters.
  685. *
  686. * @param string $parameters
  687. * @param integer $indent
  688. * @return string
  689. */
  690. protected function exportParameters($parameters, $indent = 12)
  691. {
  692. $php = array();
  693. foreach ($parameters as $key => $value) {
  694. if (is_array($value)) {
  695. $value = $this->exportParameters($value, $indent + 4);
  696. } elseif ($value instanceof Variable) {
  697. throw new \InvalidArgumentException(sprintf('you cannot dump a container with parameters that contain variable references. Variable "%s" found.', $value));
  698. } elseif ($value instanceof Definition) {
  699. throw new \InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain service definitions. Definition for "%s" found.', $value->getClass()));
  700. } elseif ($value instanceof Reference) {
  701. throw new \InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain references to other services (reference to service %s found).', $value));
  702. } else {
  703. $value = var_export($value, true);
  704. }
  705. $php[] = sprintf('%s%s => %s,', str_repeat(' ', $indent), var_export($key, true), $value);
  706. }
  707. return sprintf("array(\n%s\n%s)", implode("\n", $php), str_repeat(' ', $indent - 4));
  708. }
  709. /**
  710. * Ends the class definition.
  711. *
  712. * @return void
  713. */
  714. protected function endClass()
  715. {
  716. return <<<EOF
  717. }
  718. EOF;
  719. }
  720. /**
  721. * Wraps the service conditionals.
  722. *
  723. * @param string $value
  724. * @param string $code
  725. * @return string
  726. */
  727. protected function wrapServiceConditionals($value, $code)
  728. {
  729. if (!$services = ContainerBuilder::getServiceConditionals($value)) {
  730. return $code;
  731. }
  732. $conditions = array();
  733. foreach ($services as $service) {
  734. $conditions[] = sprintf("\$this->has('%s')", $service);
  735. }
  736. // re-indent the wrapped code
  737. $code = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code)));
  738. return sprintf(" if (%s) {\n%s }\n", implode(' && ', $conditions), $code);
  739. }
  740. /**
  741. * Builds service calls from arguments
  742. *
  743. * @param array $arguments
  744. * @param string $calls By reference
  745. * @param string $behavior By reference
  746. * @return void
  747. */
  748. protected function getServiceCallsFromArguments(array $arguments, array &$calls, array &$behavior)
  749. {
  750. foreach ($arguments as $argument) {
  751. if (is_array($argument)) {
  752. $this->getServiceCallsFromArguments($argument, $calls, $behavior);
  753. } else if ($argument instanceof Reference) {
  754. $id = (string) $argument;
  755. if (!isset($calls[$id])) {
  756. $calls[$id] = 0;
  757. }
  758. if (!isset($behavior[$id])) {
  759. $behavior[$id] = $argument->getInvalidBehavior();
  760. } else if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $behavior[$id]) {
  761. $behavior[$id] = $argument->getInvalidBehavior();
  762. }
  763. $calls[$id] += 1;
  764. }
  765. }
  766. }
  767. /**
  768. * Returns the inline definition
  769. *
  770. * @param Definition $definition
  771. * @return string
  772. */
  773. protected function getInlinedDefinitions(Definition $definition)
  774. {
  775. if (false === $this->inlinedDefinitions->contains($definition)) {
  776. $definitions = array_merge(
  777. $this->getDefinitionsFromArguments($definition->getArguments()),
  778. $this->getDefinitionsFromArguments($definition->getMethodCalls()),
  779. $this->getDefinitionsFromArguments($definition->getProperties())
  780. );
  781. $this->inlinedDefinitions->offsetSet($definition, $definitions);
  782. return $definitions;
  783. }
  784. return $this->inlinedDefinitions->offsetGet($definition);
  785. }
  786. /**
  787. * Gets the definition from arguments
  788. *
  789. * @param array $arguments
  790. * @return array
  791. */
  792. protected function getDefinitionsFromArguments(array $arguments)
  793. {
  794. $definitions = array();
  795. foreach ($arguments as $argument) {
  796. if (is_array($argument)) {
  797. $definitions = array_merge($definitions, $this->getDefinitionsFromArguments($argument));
  798. } else if ($argument instanceof Definition) {
  799. $definitions = array_merge(
  800. $definitions,
  801. $this->getInlinedDefinitions($argument),
  802. array($argument)
  803. );
  804. }
  805. }
  806. return $definitions;
  807. }
  808. /**
  809. * Checks if a service id has a reference
  810. *
  811. * @param string $id
  812. * @param array $arguments
  813. * @return boolean
  814. */
  815. protected function hasReference($id, array $arguments)
  816. {
  817. foreach ($arguments as $argument) {
  818. if (is_array($argument)) {
  819. if ($this->hasReference($id, $argument)) {
  820. return true;
  821. }
  822. } else if ($argument instanceof Reference) {
  823. if ($id === (string) $argument) {
  824. return true;
  825. }
  826. }
  827. }
  828. return false;
  829. }
  830. /**
  831. * Dumps values.
  832. *
  833. * @param string $value
  834. * @param boolean $interpolate
  835. * @return string
  836. */
  837. protected function dumpValue($value, $interpolate = true)
  838. {
  839. if (is_array($value)) {
  840. $code = array();
  841. foreach ($value as $k => $v) {
  842. $code[] = sprintf('%s => %s', $this->dumpValue($k, $interpolate), $this->dumpValue($v, $interpolate));
  843. }
  844. return sprintf('array(%s)', implode(', ', $code));
  845. } elseif (is_object($value) && $value instanceof Definition) {
  846. if (null !== $this->definitionVariables && $this->definitionVariables->contains($value)) {
  847. return $this->dumpValue($this->definitionVariables->offsetGet($value), $interpolate);
  848. }
  849. if (count($value->getMethodCalls()) > 0) {
  850. throw new \RuntimeException('Cannot dump definitions which have method calls.');
  851. }
  852. if (null !== $value->getConfigurator()) {
  853. throw new \RuntimeException('Cannot dump definitions which have a configurator.');
  854. }
  855. $arguments = array();
  856. foreach ($value->getArguments() as $argument) {
  857. $arguments[] = $this->dumpValue($argument);
  858. }
  859. $class = $this->dumpValue($value->getClass());
  860. if (false !== strpos($class, '$')) {
  861. throw new \RuntimeException('Cannot dump definitions which have a variable class name.');
  862. }
  863. if (null !== $value->getFactoryMethod()) {
  864. if (null !== $value->getFactoryClass()) {
  865. return sprintf("call_user_func(array(%s, '%s')%s)", $this->dumpValue($value->getFactoryClass()), $value->getFactoryMethod(), count($arguments) > 0 ? ', '.implode(', ', $arguments) : '');
  866. } elseif (null !== $value->getFactoryService()) {
  867. return sprintf("%s->%s(%s)", $this->getServiceCall($value->getFactoryService()), $value->getFactoryMethod(), implode(', ', $arguments));
  868. } else {
  869. throw new \RuntimeException('Cannot dump definitions which have factory method without factory service or factory class.');
  870. }
  871. }
  872. return sprintf("new \\%s(%s)", substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
  873. } elseif (is_object($value) && $value instanceof Variable) {
  874. return '$'.$value;
  875. } elseif (is_object($value) && $value instanceof Reference) {
  876. if (null !== $this->referenceVariables && isset($this->referenceVariables[$id = (string) $value])) {
  877. return $this->dumpValue($this->referenceVariables[$id], $interpolate);
  878. }
  879. return $this->getServiceCall((string) $value, $value);
  880. } elseif (is_object($value) && $value instanceof Parameter) {
  881. return $this->dumpParameter($value);
  882. } elseif (true === $interpolate && is_string($value)) {
  883. if (preg_match('/^%([^%]+)%$/', $value, $match)) {
  884. // we do this to deal with non string values (Boolean, integer, ...)
  885. // the preg_replace_callback converts them to strings
  886. return $this->dumpParameter(strtolower($match[1]));
  887. } else {
  888. $that = $this;
  889. $replaceParameters = function ($match) use ($that)
  890. {
  891. return sprintf("'.".$that->dumpParameter(strtolower($match[2])).".'");
  892. };
  893. $code = str_replace('%%', '%', preg_replace_callback('/(?<!%)(%)([^%]+)\1/', $replaceParameters, var_export($value, true)));
  894. // optimize string
  895. $code = preg_replace(array("/^''\./", "/\.''$/", "/'\.'/", "/\.''\./"), array('', '', '', '.'), $code);
  896. return $code;
  897. }
  898. } elseif (is_object($value) || is_resource($value)) {
  899. throw new \RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
  900. } else {
  901. return var_export($value, true);
  902. }
  903. }
  904. /**
  905. * Dumps a parameter
  906. *
  907. * @param string $name
  908. * @return string
  909. */
  910. public function dumpParameter($name)
  911. {
  912. if ($this->container->isFrozen() && $this->container->hasParameter($name)) {
  913. return $this->dumpValue($this->container->getParameter($name), false);
  914. }
  915. return sprintf("\$this->getParameter('%s')", strtolower($name));
  916. }
  917. /**
  918. * Gets a service call
  919. *
  920. * @param string $id
  921. * @param Reference $reference
  922. * @return string
  923. */
  924. protected function getServiceCall($id, Reference $reference = null)
  925. {
  926. if ('service_container' === $id) {
  927. return '$this';
  928. }
  929. if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
  930. return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
  931. } else {
  932. if ($this->container->hasAlias($id)) {
  933. $id = (string) $this->container->getAlias($id);
  934. }
  935. return sprintf('$this->get(\'%s\')', $id);
  936. }
  937. }
  938. /**
  939. * Returns the next name to use
  940. *
  941. * @return string
  942. */
  943. protected function getNextVariableName()
  944. {
  945. $firstChars = self::FIRST_CHARS;
  946. $firstCharsLength = strlen($firstChars);
  947. $nonFirstChars = self::NON_FIRST_CHARS;
  948. $nonFirstCharsLength = strlen($nonFirstChars);
  949. while (true) {
  950. $name = '';
  951. $i = $this->variableCount;
  952. if ('' === $name) {
  953. $name .= $firstChars[$i%$firstCharsLength];
  954. $i = intval($i/$firstCharsLength);
  955. }
  956. while ($i > 0) {
  957. $i -= 1;
  958. $name .= $nonFirstChars[$i%$nonFirstCharsLength];
  959. $i = intval($i/$nonFirstCharsLength);
  960. }
  961. $this->variableCount += 1;
  962. // check that the name is not reserved
  963. if (in_array($name, $this->reservedVariables, true)) {
  964. continue;
  965. }
  966. return $name;
  967. }
  968. }
  969. }