swaggest/php-code-builder

View on GitHub
src/PhpFile.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Swaggest\PhpCodeBuilder;

use PhpLang\ScopeExit;

class PhpFile extends PhpTemplate
{
    /** @var string */
    private $namespace;
    /** @var PhpNamespaces */
    private $namespaces;
    /** @var PhpCode */
    private $code;

    /** @var string */
    private $comment = <<<COMMENT
ATTENTION!!! The code below was carefully crafted by a mean machine.
Please consider to NOT put any emotional human-generated modifications as the splendid AI will throw them away with no mercy.
COMMENT;

    public function __construct($namespace = null)
    {
        $this->namespace = $namespace;
        $this->namespaces = new PhpNamespaces();
        $this->code = new PhpCode();
    }

    /**
     * @return PhpNamespaces
     */
    public function getNamespaces()
    {
        return $this->namespaces;
    }

    protected function toString()
    {
        $prev = self::setCurrentPhpFile($this);
        /** @noinspection PhpUnusedLocalVariableInspection */
        $_ = new ScopeExit(function () use ($prev) {
            self::setCurrentPhpFile($prev);
        });

        $this->namespaces->setFileNamespace($this->namespace);
        $code = $this->code->render();
        $phpDoc = new PhpDoc();
        $phpDoc->add(PhpDoc::TAG_FILE, $this->comment);
        $phpDoc = $phpDoc->render();

        $result = <<<PHP
<?php
{$phpDoc}
{$this->renderNamespace()}{$this->namespaces}

{$code}
PHP;
        return $result;

    }

    private function renderNamespace()
    {
        return $this->namespace
            ? "namespace " . trim($this->namespace, '\\') . ";\n\n"
            : '';

    }

    /** @var null|PhpFile */
    private static $currentPhpFile;

    /**
     * @return null|PhpFile
     */
    public static function getCurrentPhpFile()
    {
        return self::$currentPhpFile;
    }

    /**
     * @param null|PhpFile $currentPhpFile
     * @return null|PhpFile previous php file
     */
    public static function setCurrentPhpFile($currentPhpFile)
    {
        $previous = self::$currentPhpFile;
        self::$currentPhpFile = $currentPhpFile;
        return $previous;
    }

    /**
     * @return string
     */
    public function getNamespace()
    {
        return $this->namespace;
    }

    /**
     * @param string $namespace
     * @return PhpFile
     */
    public function setNamespace($namespace)
    {
        $this->namespace = $namespace;
        return $this;
    }

    /**
     * @return PhpCode
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * @param PhpCode $code
     * @return PhpFile
     */
    public function setCode($code)
    {
        $this->code = $code;
        return $this;
    }
}