InterfaceGenerator.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Code\Generator;
  7. /**
  8. * Interface generator.
  9. */
  10. class InterfaceGenerator extends \Magento\Framework\Code\Generator\ClassGenerator
  11. {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function generate()
  16. {
  17. if (!$this->isSourceDirty()) {
  18. $output = $this->getSourceContent();
  19. if (!empty($output)) {
  20. return $output;
  21. }
  22. }
  23. $output = '';
  24. if (!$this->getName()) {
  25. return $output;
  26. }
  27. $output .= $this->generateDirectives();
  28. if (null !== ($docBlock = $this->getDocBlock())) {
  29. $docBlock->setIndentation('');
  30. $output .= $docBlock->generate();
  31. }
  32. $output .= 'interface ' . $this->getName();
  33. if (!empty($this->extendedClass)) {
  34. $output .= ' extends \\' . ltrim($this->extendedClass, '\\');
  35. }
  36. $output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED
  37. . $this->generateMethods() . self::LINE_FEED . '}' . self::LINE_FEED;
  38. return $output;
  39. }
  40. /**
  41. * Instantiate interface method generator object.
  42. *
  43. * @return \Magento\Framework\Code\Generator\InterfaceMethodGenerator
  44. */
  45. protected function createMethodGenerator()
  46. {
  47. return new \Magento\Framework\Code\Generator\InterfaceMethodGenerator();
  48. }
  49. /**
  50. * Generate methods.
  51. *
  52. * @return string
  53. */
  54. protected function generateMethods()
  55. {
  56. $output = '';
  57. $methods = $this->getMethods();
  58. if (!empty($methods)) {
  59. foreach ($methods as $method) {
  60. $output .= $method->generate() . self::LINE_FEED;
  61. }
  62. }
  63. return $output;
  64. }
  65. /**
  66. * Generate directives.
  67. *
  68. * @return string
  69. */
  70. protected function generateDirectives()
  71. {
  72. $output = '';
  73. $namespace = $this->getNamespaceName();
  74. if (null !== $namespace) {
  75. $output .= 'namespace ' . $namespace . ';' . self::LINE_FEED . self::LINE_FEED;
  76. }
  77. $uses = $this->getUses();
  78. if (!empty($uses)) {
  79. foreach ($uses as $use) {
  80. $output .= 'use ' . $use . ';' . self::LINE_FEED;
  81. }
  82. $output .= self::LINE_FEED;
  83. }
  84. return $output;
  85. }
  86. }