InterfaceGenerator.php 2.4 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\Code\Generator;

/**
 * Interface generator.
 */
class InterfaceGenerator extends \Magento\Framework\Code\Generator\ClassGenerator
{
    /**
     * {@inheritdoc}
     */
    public function generate()
    {
        if (!$this->isSourceDirty()) {
            $output = $this->getSourceContent();
            if (!empty($output)) {
                return $output;
            }
        }
        $output = '';
        if (!$this->getName()) {
            return $output;
        }

        $output .= $this->generateDirectives();
        if (null !== ($docBlock = $this->getDocBlock())) {
            $docBlock->setIndentation('');
            $output .= $docBlock->generate();
        }
        $output .= 'interface ' . $this->getName();
        if (!empty($this->extendedClass)) {
            $output .= ' extends \\' . ltrim($this->extendedClass, '\\');
        }

        $output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED
            . $this->generateMethods() . self::LINE_FEED . '}' . self::LINE_FEED;

        return $output;
    }

    /**
     * Instantiate interface method generator object.
     *
     * @return \Magento\Framework\Code\Generator\InterfaceMethodGenerator
     */
    protected function createMethodGenerator()
    {
        return new \Magento\Framework\Code\Generator\InterfaceMethodGenerator();
    }

    /**
     * Generate methods.
     *
     * @return string
     */
    protected function generateMethods()
    {
        $output = '';
        $methods = $this->getMethods();
        if (!empty($methods)) {
            foreach ($methods as $method) {
                $output .= $method->generate() . self::LINE_FEED;
            }
        }
        return $output;
    }

    /**
     * Generate directives.
     *
     * @return string
     */
    protected function generateDirectives()
    {
        $output = '';
        $namespace = $this->getNamespaceName();
        if (null !== $namespace) {
            $output .= 'namespace ' . $namespace . ';' . self::LINE_FEED . self::LINE_FEED;
        }

        $uses = $this->getUses();
        if (!empty($uses)) {
            foreach ($uses as $use) {
                $output .= 'use ' . $use . ';' . self::LINE_FEED;
            }
            $output .= self::LINE_FEED;
        }
        return $output;
    }
}