Overview

Namespaces

  • Faulancer
    • Console
      • Handler
    • Controller
    • Exception
    • Form
      • Validator
        • Type
    • Helper
      • Reflection
    • Http
    • ORM
    • Service
      • Factory
    • ServiceLocator
    • View
      • Helper

Classes

  • Generate
  • Overview
  • Namespace
  • Class
  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:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 
<?php
/**
 * Class Generate | Generate.php
 * @package ORM\Console
 * @author  Florian Knapp <office@florianknapp.de>
 */
namespace Faulancer\Console\Handler;

use Faulancer\Console\ArgumentParser;
use Faulancer\Console\ConsoleInterface;
use Faulancer\Console\Helper\DbTables;
use Faulancer\Console\Helper\TableRelationResolver;
use Faulancer\Console\Output;
use Faulancer\ServiceLocator\ServiceLocator;
use ORM\DbConfig;
use ORM\EntityManager;
use Faulancer\Service\Config;
use Symfony\Component\EventDispatcher\Tests\Service;

/**
 * Class Generate
 */
class Generate implements ConsoleInterface
{

    /** @var array */
    private $mysqlTypeMapping = [
        '/@property text/'         => '@property string',
        '/@property int\((\d+)\)/' => '@property integer',
        '/@property datetime/'     => '@property string',
        '/@property timestamp/'    => '@property string',
        '/varchar\((\d+)\)/'       => 'string'
    ];

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

    /** @var ArgumentParser */
    protected $args;

    /**
     * Generate constructor.
     *
     * @param Config         $config
     * @param ArgumentParser $args
     * @codeCoverageIgnore
     */
    public function __construct(Config $config, ArgumentParser $args)
    {
        $this->config = $config;
        $this->args   = $args;
    }

    /**
     * Generate entities
     *
     * @throws \Exception
     * @throws \ORM\Exceptions\NoConnection
     * @codeCoverageIgnore
     */
    public function entitiesAction()
    {
        $creationCount = 0;
        $entitiesRoot  = $this->config->get('projectRoot') . '/src/Entity';

        if (!is_dir($entitiesRoot)) {
            mkdir($entitiesRoot);
        }

        $result = DbTables::getTableData($this->config);

        foreach ($result as $table => $fields) {

            $properties = [];
            $namespace  = $this->config->get('namespacePrefix') . '\\Entity';
            $className  = ucfirst($table) . 'Entity';

            Output::writeLine('Process table "' . $table . '"', 'warning');

            $targetFile = $entitiesRoot . '/' . $className . '.php';

            if (file_exists($targetFile)) {
                Output::writeLine('Entity "' . $className . '" already exists... skipping.', 'error');
                continue;
            }

            $properties[] = '/**';

            foreach ($fields as $field) {

                $properties[] = ' * @property ' . $field['Type'] . ' $' . $field['Field'];
                Output::writeLine('Create property ' . $field['Field']);
                
            }

            $properties[] = ' */';

            $props     = implode(PHP_EOL, $properties);
            $props     = preg_replace(
                array_keys($this->mysqlTypeMapping),
                array_values($this->mysqlTypeMapping),
                $props
            );

            $fixture = str_replace(
                ['{$namespace}', '{$props}', '{$table}', '{$className}'],
                [$namespace, $props, $table, $className],
                file_get_contents(__DIR__ . '/../Fixture/Entity.fixture')
            );


            file_put_contents($entitiesRoot . '/' . $className . '.php', $fixture);

            Output::writeLine('Entity for table "' . $table . '" successfully generated', 'success');

            $creationCount++;

            Output::writeEmptyLine();

        }

        Output::writeLine($creationCount . ' entities generated', 'success');
    }

}
API documentation generated by ApiGen