Command/GenerateDocumentClassCommand.php
author ymh <ymh.work@gmail.com>
Thu, 27 Oct 2011 05:51:04 +0200
changeset 17 81962874e172
child 18 6f16b9fd6a17
permissions -rw-r--r--
First implementation of dynamic fields
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/*
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * This file is part of the WikiTagBundle package.
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * (c) IRI <http://www.iri.centrepompidou.fr/>
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * For the full copyright and license information, please view the LICENSE
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * file that was distributed with this source code.
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 */
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
namespace IRI\Bundle\WikiTagBundle\Command;
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
use Symfony\Component\Console\Input\InputArgument;
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
use Symfony\Component\Console\Input\InputInterface;
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
use Symfony\Component\Console\Input\InputOption;
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
use Symfony\Component\Console\Output\OutputInterface;
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
use Mandango\Mondator\Definition\Definition;
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
use Mandango\Mondator\Definition\Property;
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
use Mandango\Mondator\Definition\Method;
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
use Mandango\Mondator\Dumper;
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
class GenerateDocumentClassCommand extends ContainerAwareCommand
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
{
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    protected function configure()
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    {
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        parent::configure();
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        $this
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
            ->setName('wikitag:generate-document-class')
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
            ->setDescription('Generate the document class document class')
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
            ->addArgument('path', InputArgument::OPTIONAL, 'The generation path')
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
            ->addOption("simulate","S",InputOption::VALUE_NONE, "Simulate generation");
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    }
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    protected function execute(InputInterface $input, OutputInterface $output)
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    {
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        $path = $input->getArgument('path');
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        if(is_null($path) || strlen($path) == 0)
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        {
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
            $path = realpath($this->getContainer()->get('kernel')->getRootDir()."/../src");
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        }
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        $definition = new Definition('IRI\Bundle\WikiTagBundle\Entity\Document');
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        $definition->setParentClass('IRI\Bundle\WikiTagBundle\Model\Document');
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        $fields = $this->getContainer()->getParameter('wiki_tag.fields');
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        foreach ( $fields as $name => $field_def)
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        {
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
            $property = new Property("private", $name, NULL);
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            $definition->addProperty($property);
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
            
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            $get_method = new Method("public", "get".ucfirst($name), NULL, <<<EOF
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
                return \$this->$name;
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
EOF
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            );
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
            $definition->addMethod($get_method);
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
            
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
            $set_method = new Method("public", "set".ucfirst($name), "\$$name", <<<EOF
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
                \$this->$name = \$$name;
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
EOF
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
            );
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
            $definition->addMethod($set_method);
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
            
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        }
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
                
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        $dumper = new Dumper($definition);
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
        $classCode = $dumper->dump();
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        if($input->getOption('simulate'))
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        {
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
            $output->writeln($classCode);
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        }
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        else
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        {
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
            $file = "$path/IRI/Bundle/WikiTagBundle/Entity/Document.php";
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
            $output->writeln("Creating IRI\\Bundle\\WikiTagBundle\\Entity\\Document in $file");
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
            
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            if(!file_exists(dirname($file)) && !mkdir(dirname($file),0777,true))
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
            {
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
                $output->writeln("Impossible to create folder exitiing.");
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
                die;
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
            }
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
            file_put_contents($file, $classCode);
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        }
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
    }
81962874e172 First implementation of dynamic fields
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
}