MetadataConverter.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\DiExtraBundle\Metadata;
  18. use Symfony\Component\DependencyInjection\Reference;
  19. use Symfony\Component\DependencyInjection\DefinitionDecorator;
  20. use Symfony\Component\DependencyInjection\Definition;
  21. use Metadata\ClassHierarchyMetadata;
  22. class MetadataConverter
  23. {
  24. /**
  25. * Converts class hierarchy metadata to definition instances.
  26. *
  27. * @param ClassHierarchyMetadata $metadata
  28. * @return array an array of Definition instances
  29. */
  30. public function convert(ClassHierarchyMetadata $metadata)
  31. {
  32. static $count = 0;
  33. $definitions = array();
  34. $previous = null;
  35. foreach ($metadata->classMetadata as $classMetadata) {
  36. if (null === $previous && null === $classMetadata->parent) {
  37. $definition = new Definition();
  38. } else {
  39. $definition = new DefinitionDecorator(
  40. $classMetadata->parent ?: $previous->id
  41. );
  42. }
  43. $definition->setClass($classMetadata->name);
  44. if (null !== $classMetadata->scope) {
  45. $definition->setScope($classMetadata->scope);
  46. }
  47. if (null !== $classMetadata->public) {
  48. $definition->setPublic($classMetadata->public);
  49. }
  50. if (null !== $classMetadata->abstract) {
  51. $definition->setAbstract($classMetadata->abstract);
  52. }
  53. if (null !== $classMetadata->arguments) {
  54. $definition->setArguments($classMetadata->arguments);
  55. }
  56. $definition->setMethodCalls($classMetadata->methodCalls);
  57. $definition->setTags($classMetadata->tags);
  58. $definition->setProperties($classMetadata->properties);
  59. if (null === $classMetadata->id) {
  60. $classMetadata->id = '_jms_di_extra.unnamed.service_'.$count++;
  61. }
  62. if ($classMetadata->initMethod) {
  63. if (!method_exists($definition, 'setInitMethod')) {
  64. throw new \RuntimeException(sprintf('@AfterSetup is not available on your Symfony version.'));
  65. }
  66. $definition->setInitMethod($classMetadata->initMethod);
  67. }
  68. $definitions[$classMetadata->id] = $definition;
  69. $previous = $classMetadata;
  70. }
  71. return $definitions;
  72. }
  73. }