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