majkel89/tcpdf-warper

View on GitHub
src/org/majkel/tcpdfwarper/generator/Generator.php

Summary

Maintainability
A
2 hrs
Test Coverage
<?php
/**
 * Created by PhpStorm.
 * User: MichaƂ (majkel) Kowalik <maf.michal@gmail.com>
 * Date: 1/17/2015
 * Time: 20:44
 */

namespace org\majkel\tcpdfwarper\generator;

/**
 * Class Generator
 * @package org\majkel\tcpdfwarper\generator
 */
class Generator {

    /** @var string */
    protected $configFile;
    /** @var string */
    protected $classTemplateFile;
    /** @var string */
    protected $traitTemplateFile;

    /** @var array */
    protected $config;

    /** @var string */
    protected $date;
    /** @var string */
    protected $time;

    /**
     * @param array $config
     */
    public function __construct($config) {
        $this->configFile = $config['configFile'];
        $this->classTemplateFile = $config['classTemplateFile'];
        $this->traitTemplateFile = $config['traitTemplateFile'];
        $now = time();
        $this->date = date('Y-m-d', $now);
        $this->time = date('H:i:s', $now);
    }

    /**
     * @return ConfigItem[]
     * @throws GeneratorException
     */
    protected function getConfig() {
        if (is_null($this->config)) {
            $this->config = array();
            if (!file_exists($this->configFile)) {
                throw new GeneratorException("Config file `{$this->configFile}` does not exists.");
            }
            $config = require $this->configFile;
            if (!isset($config) || !is_array($config)) {
                throw new GeneratorException("Invalid configuration file `{$this->configFile}`");
            }
            foreach ($config as $method => $classDef) {
                if (is_array($classDef)) {
                    if (!is_integer($method) && !isset($classDef['method'])) {
                        $classDef['method'] = $method;
                    }
                }
                else {
                    $classDef = array(
                            'method' => $classDef,
                    );
                }
                $this->config[] = ConfigItem::fromArray($classDef);
            }
        }
        return $this->config;
    }

    /**
     * @param string $text
     * @return string
     */
    protected static function removeTrailingSpaces($text) {
        return rtrim(preg_replace('#[ \t]+[\r\n]#', "\n", $text), " \t\0\x0B");
    }

    /**
     * @param string $tpl
     * @param array $variables
     * @return string
     */
    protected function render($tpl, array $variables) {
        ob_start();
        extract($variables, EXTR_SKIP);
        require $tpl;
        $result = ob_get_contents();
        ob_end_clean();
        return self::removeTrailingSpaces($result);
    }

    /**
     * @param ClassDefinition $class
     * @return string
     */
    protected function generateClass($class) {
        return $this->render($this->classTemplateFile, [
            'class' => $class,
        ]);
    }

    /**
     * @param ClassDefinition[] $classes
     * @return string
     */
    protected function generateTrait($classes) {
        return $this->render($this->traitTemplateFile, [
            'classes' => $classes,
        ]);
    }

    /**
     * @param string $outputDirectory
     * @throws GeneratorException
     */
    public function generate($outputDirectory) {
        if (!is_dir($outputDirectory)) {
            throw new GeneratorException("`$outputDirectory` is not valid directory");
        }
        $classes = array();
        $config = $this->getConfig();
        foreach ($config as $classDef) {
            $class = ClassDefinition::fromConfigItem($classDef);
            $classes[] = $class;
            file_put_contents($outputDirectory.'/'.$classDef->className.'.php', $this->generateClass($class));
        }
        file_put_contents($outputDirectory.'/TCPDFWarperTrait.php', $this->generateTrait($classes));
    }

}