Loading code/GenerateRubrique.phpdeleted 100644 → 0 +0 −442 Changes for code/GenerateRubrique.php: 0 added lines, 442 removed lines. Original line number Diff line number Diff line <?php // Assume that all external field ended by _ID use UnicaenCode\Form\ElementMakerForm; use UnicaenCode\Util; /** * @var $this \Application\View\Renderer\PhpRenderer * @var $controller \Zend\Mvc\Controller\AbstractController * @var $container \Psr\Container\ContainerInterface * @var $viewName string * @var $viewFile string */ ?> <h1>Création d'une rubrique dans un menu</h1> <h3>Etape 1 : Paramétrage</h3> <?php $form = new \Zend\Form\Form(); $em = $controller->get('Doctrine\ORM\EntityManager'); $conn = $em->getConnection(); $sm = $conn->getSchemaManager(); $tables = $sm->listTableNames(); sort($tables); $val = []; $form->add([ 'type' => 'Zend\Form\Element\Select', 'name' => 'tables', 'options' => [ 'label' => 'Tables', ], ]); foreach ($tables as $tbl) { $val[$tbl] = $tbl; } $form->get('tables')->setValueOptions($val); $form->add([ 'type' => 'Zend\Form\Element\Select', 'name' => 'menus', 'options' => [ 'label' => 'Menu', 'value_options' => [ 'Administration' => 'Administration', 'Gestion' => 'Gestion', ], ], ]); $form->add([ 'type' => 'Zend\Form\Element\Text', 'name' => 'Origine', 'options' => [ 'label' => 'Rep Web', ], ]); $form->get('Origine')->setValue('/var/www/OSE/'); $form->add(ElementMakerForm::checkbox( 'classe-privilege', 'Privilège existant ?', true )); $form->add(ElementMakerForm::checkbox( 'non-remplacement', 'Ne pas générer fichier déjà existant ?', true )); $form->add(ElementMakerForm::submit('generate', 'Générer le code')); $form->setData($controller->getRequest()->getPost()); Util::displayForm($form); if (($controller->getRequest()->isPost()) && ($form->isValid())) { $baseOrig = $form->get('Origine')->getValue() . 'module/Application/'; $nom = $form->get('tables')->getValue(); $basefs = '/var/www/Web/module/Application/'; $nomsous = strtoupper($nom); $nomvisu = $nomsous . "_VISUALISATION"; $nomedit = $nomsous . "_EDITION"; if (!$form->get('classe-privilege')->getValue()) { $nomvisu = 'DROIT_PRIVILEGE_EDITION'; $nomedit = 'DROIT_PRIVILEGE_VISUALISATION'; } $nonRemplace = $form->get('non-remplacement')->getValue(); $nomtiret = $nomm = strtolower($nom); $nomtiret = str_replace('_', '-', $nomtiret); $nomm = preg_replace_callback('/_\w/', function ($m) { return strtoupper($m[0]); }, $nomm); $nommaj = preg_replace_callback('/^\w/', function ($m) { return strtoupper($m[0]); }, $nomm); $nomphrase = str_replace('_', ' ', $nommaj); $nomm = str_replace('_', '', $nomm); $nommaj = str_replace('_', '', $nommaj); /** * $col Doctrine\DBAL\Schema\Column */ $nomChamps = []; $typeChamps = []; $lgChamps = []; $listeChamps = []; $notnullChamps = []; $defaultChamps = []; $result = []; // résultat d'exec $nbe = 0; if (empty($nom)) die(''); $columns = $sm->listTableColumns($nom); foreach ($columns as $col) { $nchp = $col->getName(); $notnullChamps[$nbe] = $col->getNotnull(); $defaultChamps[$nbe] = $col->getDefault(); if (!preg_match('/^HISTO_/', $nchp)) { $nomChamps[$nbe] = $nchp; if (preg_match('/_ID$/', $nchp)) { $listeChamps[$nbe] = $nomChamps[$nbe] = preg_replace('/_ID$/', '', $nchp); if (preg_match('/^ANNEE_/', $nchp)) $listeChamps[$nbe] = 'ANNEE'; } else $listeChamps[$nbe] = ''; $typeChamps[$nbe] = $col->getType()->getName(); $lgChamps[$nbe] = $col->getLength(); if (($typeChamps[$nbe] == 'integer') || ($typeChamps[$nbe] == 'float')) { $lgChamps[$nbe] = $col->getPrecision(); if ($lgChamps[$nbe] == 1) $typeChamps[$nbe] = 'boolean'; } $nbe++; } } if (file_exists('/var/www/Web')) { exec("rm -rf /var/www/Web/*", $stdout, $err); } else { mkdir('/var/www/Web', 0770) or die('Vous devez créer un sous répertoire /var/www/Web avec les droits d\'écriture! pour l\'utilisateur www-data<BR>'); } // $res = shell_exec("Web/module/Application"); //echo "res:$res<BR>"; // mise en place des champs $txt0 = $txt01 = $txt4 = $txt41 = $txt42 = $txt43 = $txt44 = $txt45 = $txt5 = $txt53 = $txt6 = $txt7 = ''; // chargement des pseudos fichiers $tblInclude = [ 'champInput', 'champListe', 'config_php', 'controller_php', 'idx1', 'idx3', 'idx20', 'saisie_phtml', 'saisieFormAwareTrait_php', 'service_php', 'serviceTrait_php', ]; $i = 0; foreach ($tblInclude as $ci) { ${$ci} = file_get_contents(__DIR__ . '/template/GenerateRubrique/' . $ci); } $extractInput = '\'champtiret\' => $object->getchampmaj() ,'; $extractListe = '\'champtiret\' => ($s = $object->getchampmaj()) ? $s->getId() : null, '; $hydrateInput = '$object->setchampmaj($data[\'champtiret\']); '; $hydrateListe = 'if (array_key_exists(\'champtiret\', $data)) { $object->setchampmaj($this->getServicechampmaj()->get($data[\'champtiret\'])); } '; $idx21 = " <th style=\"word-wrap: break-word ; \">champphrase</th>\n"; $idx2 = ' <td style="word-wrap: break-word ; "><?= $fr->getchampmaj() ?></td>' . "\n"; $service10 = " use Application\Service\Traits\champmajServiceAwareTrait;\n"; $service11 = " use champmajServiceAwareTrait;\n"; $fic10 = '/** * Description of nommajSaisieForm * * @author ZVENIGOROSKY Alexandre <alexandre.zvenigorosky@unicaen.fr> */ class nommajSaisieForm extends AbstractForm { '; $fic11 = ' public function init() { $hydrator = new nommajHydrator(); $this->setHydrator($hydrator); $this->setAttribute(\'action\', $this->getCurrentUrl()); '; $fic1 = '<?php namespace Application\Form\nommaj; use Application\Form\AbstractForm; use Zend\Form\Element\Csrf; use Zend\Hydrator\HydratorInterface; '; $fic2 = ' $this->add(new Csrf(\'security\')); $this->add([ \'name\' => \'submit\', \'type\' => \'Submit\', \'attributes\' => [ \'value\' => "Enregistrer", \'class\' => \'btn btn-primary\', ], ]); return $this; } /** * Should return an array specification compatible with * {@link Zend\InputFilter\Factory::createInputFilter()}. * * @return array */ public function getInputFilterSpecification() { return ['; $fic21 = ' ]; } } class nommajHydrator implements HydratorInterface { '; $listeEnteteHydrator = 'use champmajServiceAwareTrait; '; $fic22 = ' /** * Hydrate $object with the provided $data. * * @param array $data * @param \Application\Entity\Db\nommaj $object * * @return object */ public function hydrate(array $data, $object) { '; $inputFilter = ' \'champtiret\' => [ \'required\' => true, ], '; $fic3 = ' return $object; } /** * Extract values from an object * * @param \Application\Entity\Db\nommaj $object * * @return array */ public function extract($object) { $data = [ '; $fic4 = ' ]; return $data; } } '; for ($i = 0; $i < $nbe; $i++) { $champ = $nomChamps[$i]; $champsous = strtoupper($champ); $champtiret = $champm = strtolower($champ); $champtiret = str_replace('_', '-', $champtiret); $champm = preg_replace_callback('/_\w/', function ($m) { return strtoupper($m[0]); }, $champm); $champmaj = preg_replace_callback('/^\w/', function ($m) { return strtoupper($m[0]); }, $champm); $champphrase = str_replace('_', ' ', $champmaj); $champm = str_replace('_', '', $champm); $champmaj = str_replace('_', '', $champmaj); # input:champInput.txt listeInput.txt ... if (($i) && ($notnullChamps[$i])) { $txt = str_replace('champtiret', $champtiret, $inputFilter); $txt6 .= $txt; } if ($champtiret != 'id') { $txt52 = str_replace('champphrase', $champphrase, $idx21); $txt53 .= $txt52; } if (!$listeChamps[$i]) { $champType = 'Text'; if ($typeChamps[$i] == 'String') $champType = 'Text'; if ($typeChamps[$i] == 'boolean') $champType = 'Checkbox'; if ($champtiret != 'id') { $txt51 = str_replace('champmaj', $champmaj, $idx2); $txt5 .= $txt51; } if ($champtiret != 'id') { $txt = str_replace(['champtiret', 'champphrase', $champType], [$champtiret, $champphrase, $champType], $champInput); $txt4 .= $txt; } if ($champtiret != 'id') { $txt = str_replace('champtiret', $champtiret, $hydrateInput); $txt = str_replace('champmaj', $champmaj, $txt); $txt41 .= $txt; } $txt = str_replace('champtiret', $champtiret, $extractInput); $txt = str_replace('champmaj', $champmaj, $txt); $txt42 .= $txt; } else { $txt51 = str_replace('champmaj', $champmaj, $idx2); $txt52 = str_replace('champphrase', $champphrase, $idx21); $txt5 .= $txt51; $txt52 .= $txt53; $txt = str_replace('champmaj', $champmaj, $service10); $txt0 .= $txt; $txt = str_replace('champmaj', $champmaj, $service11); $txt01 .= $txt; $txt = str_replace(['champtiret', 'champphrase', 'champmaj'], [$champtiret, $champphrase, $champmaj], $champListe); if ($notnullChamps[$i]) { $txt = str_replace(' ->setEmptyOption("(Aucun)")', '', $txt); } $txt43 .= $txt; $txt = str_replace(['champtiret', 'champmaj'], [$champtiret, $champmaj], $hydrateListe); $txt44 .= $txt; $txt = str_replace(['champtiret', 'champmaj'], [$champtiret, $champmaj], $extractListe); $txt45 .= $txt; $txt = str_replace('champmaj', $champmaj, $listeEnteteHydrator); $txt7 .= $txt; } } $saisieForm_php = $fic1 . $txt0 . $fic10 . $txt01 . $fic11 . $txt4 . $txt43 . $fic2 . $txt6 . $fic21 . $txt7 . $fic22 . $txt41 . $txt44 . $fic3 . $txt42 . $txt45 . $fic4; $index_phtml = $idx1 . $txt53 . $idx20 . $txt5 . $idx3; $reps = ['config/', 'src/Application/Controller/', 'src/Application/Form/', 'src/Application/Form/', 'view/application/', 'view/application/', 'src/Application/Service/', 'src/Application/Service/Traits/']; $rep2regs = ['', '', $nommaj . '/', $nommaj . '/Traits/', $nomtiret . '/', $nomtiret . '/', '', '']; $fichmotifs = [$config_php, $controller_php, $saisieForm_php, $saisieFormAwareTrait_php, $index_phtml, $saisie_phtml, $service_php, $serviceTrait_php]; $fichiers = [$nomtiret . '.config.php', $nommaj . 'Controller.php', $nommaj . 'SaisieForm.php', $nommaj . 'SaisieFormAwareTrait.php', 'index.phtml', 'saisie.phtml', $nommaj . 'Service.php', $nommaj . 'ServiceAwareTrait.php']; $motif = ['nomsous', 'nommaj', 'nomm', 'nomtiret', 'droitedition', 'droitvisualisation', 'nomphrase']; $remplace = [$nomsous, $nommaj, $nomm, $nomtiret, $nomedit, $nomvisu, $nomphrase]; $maxfichiers = sizeof($fichiers); for ($i = 0; $i < $maxfichiers; $i++) { $rep = $reps[$i]; $fich = $fichiers[$i]; $fmotif = $fichmotifs[$i]; $rep2reg = $rep2regs[$i]; $ficin = $fichmotifs[$i]; $ficout = $basefs . $rep . $rep2reg . $fich; $ficenr = $baseOrig . $rep . $rep2reg . $fich; // echo 'Ficout=' . $ficout.', ficenr:'.$ficenr.'<br>'; $txt = str_replace($motif, $remplace, $ficin); if (!file_exists($basefs . $rep . $rep2reg)) { $chemin = $basefs . $rep . $rep2reg; $res = exec("mkdir -p $chemin", $err); // echo "res:$res<BR>"; } $hdl = fopen($ficout, 'wb'); $lgWrite = fwrite($hdl, $txt); fclose($hdl); if (file_exists($ficenr)) { if ($nonRemplace) { unset($result); exec("diff $ficenr $ficout", $result); echo "<BR><span style=\"color:#00bbdd; \">Différences avec $ficenr<BR><HR></span>"; echo '<div style="position:relative; left:10%; font-family:Lucida Console; background-color:lightgrey; width:85% ">'; $nblg = sizeof($result); for ($j = 0; $j < $nblg; $j++) { echo '<span style="color:'; if (preg_match('/^\d/', $result[$j])) echo '#008bb9'; if (preg_match('/^>/', $result[$j])) echo '#000000'; if (preg_match('/^</', $result[$j])) echo '#00bb00'; if (preg_match('/^---/', $result[$j])) { echo '; width:500px; "><hr width="85%" size="2"></span>'; } else echo "; \"><BR>$result[$j]</span>"; } echo '</div>'; unlink($ficout); } else echo '<div style="color:#ff0000; ">Attention --> Le fichier module/Application/' . $ficout . ' existe dans /var/www/OSE !</div>'; } } echo "<BR><span style=\"color:#006600; \">Résultat dans l'arborescence $basefs</span>"; } code/NewEntityService.php +68 −52 Changes for code/NewEntityService.php: 68 added lines, 52 removed lines. Original line number Diff line number Diff line <?php use UnicaenCode\Form\AbstractForm; use UnicaenCode\Form\ElementMakerForm; use UnicaenCode\Util; Loading @@ -10,60 +11,75 @@ use UnicaenCode\Util; * @var $viewName string * @var $viewFile string */ class EntityServiceForm extends AbstractForm { public function init() { $this->addSelectEntity( 'entityClass', 'Entité du service' ); ?> <h1>Création d'un nouveau service d'entité OSE</h1> <h3>Etape 1 : Paramétrage</h3> $this->addText( 'alias', 'Alias d\'entité' ); <?php $this->addCheckbox( 'awareTrait', 'Générer un trait', true ); $form = new \Zend\Form\Form(); $form->add(ElementMakerForm::selectEntity( 'entity', 'Entité correspondante', 'Application\Entity\Db' )); $form->add(ElementMakerForm::text('alias', 'Alias d\'entité')); $form->add(ElementMakerForm::submit('generate', 'Générer le service')); $form->setData($controller->getRequest()->getPost()); Util::displayForm($form); if ($controller->getRequest()->isPost() && $form->isValid()) { $entity = $form->get('entity')->getValue(); $sCodeGenerator = Util::codeGenerator(); /* @var $sCodeGenerator \UnicaenCode\Service\CodeGenerator */ $params = $sCodeGenerator->generateServiceParams([ 'classname' => 'Application\\Service\\' . $entity . 'Service', 'generateTrait' => true, 'generateInterface' => false, ], [ 'Class' => [ 'template' => 'EntityService', 'entity' => $entity, 'alias' => $form->get('alias')->getValue(), ], 'Factory' => [ 'template' => 'EntityServiceFactory', ], ]); ?> <h3>Etape 2 : Création des fichiers sources du service</h3> <?php $sCodeGenerator->generateFiles($params); ?> <div class="alert alert-info">Les fichiers sont récupérables dans le dossier <?= $sCodeGenerator->getOutputDir() ?></div> <h3>Etape 3 : Déclaration dans le fichier de configuration</h3> <?php $sCodeGenerator->generateFile($params['Config'], false); ?> <div class="alert alert-warning"> Vous devez vous-même placer ces informations dans le fichier de configuration de votre module. </div> $this->addCheckbox( 'awareInterface', 'Générer une interface', false ); <?php $this->addCheckbox( 'subDir', 'Les traits, interfaces et Factory seront placés dans des sous-dossiers dédiés', false ); } public function getParams(): array { $params = [ 'generator' => 'Service', 'type' => 'Service', 'entityClass' => $this->get('entityClass')->getValue(), 'useGetter' => true, 'subDir' => $this->get('subDir')->getValue(), 'alias' => $this->get('alias')->getValue(), ]; $params['entityClassname'] = Util::classClassname($params['entityClass']); $params['class'] = Util::classModule($params['entityClass']) . '\Service\\' . $params['entityClassname'] . 'Service'; if ($this->get('awareTrait')->getValue()) { $params['awareTrait'] = []; } if ($this->get('awareInterface')->getValue()) { $params['awareInterface'] = []; } $params['factory'] = []; return $params; } } $cg = \UnicaenCode\Util::codeGenerator(); $cg->start('Création d\'un nouveau service d\'entité OSE'); $form = new EntityServiceForm(); $form->init(); $form->addText('alias', 'Alias d\'entité'); if (empty($params = $cg->formPublish($form))) return; $params['template'] = 'EntityService'; $params['factory']['template'] = 'EntityServiceFactory'; $cg->end($params); No newline at end of file code/NewOseForm.phpdeleted 100755 → 0 +0 −93 Changes for code/NewOseForm.php: 0 added lines, 93 removed lines. Original line number Diff line number Diff line <?php use UnicaenCode\Form\ElementMakerForm; use UnicaenCode\Util; /** * @var $this \Application\View\Renderer\PhpRenderer * @var $controller \Zend\Mvc\Controller\AbstractController * @var $container \Psr\Container\ContainerInterface * @var $viewName string * @var $viewFile string */ ?> <h1>Création d'un nouveau formulaire</h1> <h3>Etape 1 : Paramétrage</h3> <?php $form = new \Zend\Form\Form(); $form->add(ElementMakerForm::selectModule( 'module', 'Module dans lequel sera placé votre formulaire' )); $form->add(ElementMakerForm::select( 'type', 'Type de formulaire (Form ou Fieldset)', ['Form' => 'Form', 'Fieldset' => 'Fieldset'], 'Form' )); $form->add(ElementMakerForm::text( 'classname', 'Nom de classe du formulaire (en CamelCase, avec éventuellement un namespace avant : MonNamespace\Exemple)', 'Exemple' )); $form->add(ElementMakerForm::checkbox( 'useHydrator', 'Implémenter un hydrateur spécifique' )); $form->add(ElementMakerForm::checkbox( 'generateTrait', 'Générer un trait', true )); $form->add(ElementMakerForm::checkbox( 'generateInterface', 'Générer une interface', false )); $form->add(ElementMakerForm::checkbox( 'useGetter', 'Générer des getters dans les traits et les interfaces', true )); $form->add(ElementMakerForm::checkbox( 'generateFactory', 'Générer une factory', true )); $form->add(ElementMakerForm::submit('generate', 'Générer le formulaire')); $form->setData($controller->getRequest()->getPost()); Util::displayForm($form); if ($controller->getRequest()->isPost() && $form->isValid()) { $type = $form->get('type')->getValue(); $classname = $form->get('classname')->getValue(); $sCodeGenerator = Util::codeGenerator() /* @var $sCodeGenerator \UnicaenCode\Service\CodeGenerator */ $params = $sCodeGenerator->generateFormParams([ 'type' => $type, 'classname' => $form->get('module')->getValue() . '\\Form\\' . $classname . $type, 'name' => ($type == 'Fieldset' ? 'fieldset' : '') . str_replace('\\', '', $classname), 'useHydrator' => $form->get('useHydrator')->getValue(), 'generateTrait' => $form->get('generateTrait')->getValue(), 'generateInterface' => $form->get('generateInterface')->getValue(), 'generateFactory' => $form->get('generateFactory')->getValue(), 'useGetter' => $form->get('useGetter')->getValue(), ], [ 'Class' => [ 'template' => 'OseForm', 'useSubForm' => false !== strpos($classname, '\\'), ], ]); ?> <h3>Etape 2 : Création des fichiers sources du formulaire</h3> <?php $sCodeGenerator->generateFiles($params); ?> <div class="alert alert-info">Les fichiers sont récupérables dans le dossier <?= $sCodeGenerator->getOutputDir() ?></div> <h3>Etape 3 : Déclaration dans le fichier de configuration</h3> <?php $sCodeGenerator->generateFile($params['Config'], false); ?> <div class="alert alert-warning"> Vous devez vous-même placer ces informations dans le fichier de configuration de votre module. </div> <?php } No newline at end of file code/template/EntityService.php +7 −7 Changes for code/template/EntityService.php: 7 added lines, 7 removed lines. Original line number Diff line number Diff line Loading @@ -2,16 +2,16 @@ namespace <namespace>; use Application\Entity\Db\<entity>; use <entityClass>; /** * Description of <classname> * * @author <author> * * @method <entity> get($id) * @method <entity>[] getList(\Doctrine\ORM\QueryBuilder $qb = null, $alias = null) * @method <entity> newEntity() * @method <entityClassname> get($id) * @method <entityClassname>[] getList(\Doctrine\ORM\QueryBuilder $qb = null, $alias = null) * @method <entityClassname> newEntity() * */ class <classname> extends AbstractEntityService Loading @@ -23,9 +23,9 @@ class <classname> extends AbstractEntityService * @return string * @throws RuntimeException */ public function getEntityClass() public function getEntityClass(): string { return <entity>::class; return <entityClassname>::class; } Loading @@ -35,7 +35,7 @@ class <classname> extends AbstractEntityService * * @return string */ public function getAlias(){ public function getAlias(): string{ return '<alias>'; } Loading code/template/Form.php 0 → 100644 +93 −0 Changes for code/template/Form.php: 93 added lines, 0 removed lines. Original line number Diff line number Diff line <?php namespace <namespace>; use Application\Form\AbstractForm; <if useHydrator> use Zend\Stdlib\Hydrator\HydratorInterface; <endif useHydrator> /** * Description of <classname> * * @author <author> */ class <classname> extends AbstractForm { public function init() { <if useHydrator> $hydrator = new <classname>Hydrator; $this->setHydrator($hydrator); <endif useHydrator> $this->setAttribute('action',$this->getCurrentUrl()); /* Ajoutez vos éléments de formulaire ici */ $this->add([ 'name' => 'submit', 'type' => 'Submit', 'attributes' => [ 'value' => 'Enregistrer', 'class' => 'btn btn-primary', ], ]); } /** * Should return an array specification compatible with * {@link Zend\InputFilter\Factory::createInputFilter()}. * * @return array */ public function getInputFilterSpecification() { return [ /* Filtres et validateurs */ ]; } } <if useHydrator> class <classname>Hydrator implements HydratorInterface { /** * @param array $data * @param $object * * @return object */ public function hydrate(array $data, $object) { /* on peuple l'objet à partir du tableau de données */ return $object; } /** * @param $object * * @return array */ public function extract($object) { $data = [ /* On peuple le tableau avec les données de l'objet */ ]; return $data; } } <endif useHydrator> No newline at end of file Loading
code/GenerateRubrique.phpdeleted 100644 → 0 +0 −442 Changes for code/GenerateRubrique.php: 0 added lines, 442 removed lines. Original line number Diff line number Diff line <?php // Assume that all external field ended by _ID use UnicaenCode\Form\ElementMakerForm; use UnicaenCode\Util; /** * @var $this \Application\View\Renderer\PhpRenderer * @var $controller \Zend\Mvc\Controller\AbstractController * @var $container \Psr\Container\ContainerInterface * @var $viewName string * @var $viewFile string */ ?> <h1>Création d'une rubrique dans un menu</h1> <h3>Etape 1 : Paramétrage</h3> <?php $form = new \Zend\Form\Form(); $em = $controller->get('Doctrine\ORM\EntityManager'); $conn = $em->getConnection(); $sm = $conn->getSchemaManager(); $tables = $sm->listTableNames(); sort($tables); $val = []; $form->add([ 'type' => 'Zend\Form\Element\Select', 'name' => 'tables', 'options' => [ 'label' => 'Tables', ], ]); foreach ($tables as $tbl) { $val[$tbl] = $tbl; } $form->get('tables')->setValueOptions($val); $form->add([ 'type' => 'Zend\Form\Element\Select', 'name' => 'menus', 'options' => [ 'label' => 'Menu', 'value_options' => [ 'Administration' => 'Administration', 'Gestion' => 'Gestion', ], ], ]); $form->add([ 'type' => 'Zend\Form\Element\Text', 'name' => 'Origine', 'options' => [ 'label' => 'Rep Web', ], ]); $form->get('Origine')->setValue('/var/www/OSE/'); $form->add(ElementMakerForm::checkbox( 'classe-privilege', 'Privilège existant ?', true )); $form->add(ElementMakerForm::checkbox( 'non-remplacement', 'Ne pas générer fichier déjà existant ?', true )); $form->add(ElementMakerForm::submit('generate', 'Générer le code')); $form->setData($controller->getRequest()->getPost()); Util::displayForm($form); if (($controller->getRequest()->isPost()) && ($form->isValid())) { $baseOrig = $form->get('Origine')->getValue() . 'module/Application/'; $nom = $form->get('tables')->getValue(); $basefs = '/var/www/Web/module/Application/'; $nomsous = strtoupper($nom); $nomvisu = $nomsous . "_VISUALISATION"; $nomedit = $nomsous . "_EDITION"; if (!$form->get('classe-privilege')->getValue()) { $nomvisu = 'DROIT_PRIVILEGE_EDITION'; $nomedit = 'DROIT_PRIVILEGE_VISUALISATION'; } $nonRemplace = $form->get('non-remplacement')->getValue(); $nomtiret = $nomm = strtolower($nom); $nomtiret = str_replace('_', '-', $nomtiret); $nomm = preg_replace_callback('/_\w/', function ($m) { return strtoupper($m[0]); }, $nomm); $nommaj = preg_replace_callback('/^\w/', function ($m) { return strtoupper($m[0]); }, $nomm); $nomphrase = str_replace('_', ' ', $nommaj); $nomm = str_replace('_', '', $nomm); $nommaj = str_replace('_', '', $nommaj); /** * $col Doctrine\DBAL\Schema\Column */ $nomChamps = []; $typeChamps = []; $lgChamps = []; $listeChamps = []; $notnullChamps = []; $defaultChamps = []; $result = []; // résultat d'exec $nbe = 0; if (empty($nom)) die(''); $columns = $sm->listTableColumns($nom); foreach ($columns as $col) { $nchp = $col->getName(); $notnullChamps[$nbe] = $col->getNotnull(); $defaultChamps[$nbe] = $col->getDefault(); if (!preg_match('/^HISTO_/', $nchp)) { $nomChamps[$nbe] = $nchp; if (preg_match('/_ID$/', $nchp)) { $listeChamps[$nbe] = $nomChamps[$nbe] = preg_replace('/_ID$/', '', $nchp); if (preg_match('/^ANNEE_/', $nchp)) $listeChamps[$nbe] = 'ANNEE'; } else $listeChamps[$nbe] = ''; $typeChamps[$nbe] = $col->getType()->getName(); $lgChamps[$nbe] = $col->getLength(); if (($typeChamps[$nbe] == 'integer') || ($typeChamps[$nbe] == 'float')) { $lgChamps[$nbe] = $col->getPrecision(); if ($lgChamps[$nbe] == 1) $typeChamps[$nbe] = 'boolean'; } $nbe++; } } if (file_exists('/var/www/Web')) { exec("rm -rf /var/www/Web/*", $stdout, $err); } else { mkdir('/var/www/Web', 0770) or die('Vous devez créer un sous répertoire /var/www/Web avec les droits d\'écriture! pour l\'utilisateur www-data<BR>'); } // $res = shell_exec("Web/module/Application"); //echo "res:$res<BR>"; // mise en place des champs $txt0 = $txt01 = $txt4 = $txt41 = $txt42 = $txt43 = $txt44 = $txt45 = $txt5 = $txt53 = $txt6 = $txt7 = ''; // chargement des pseudos fichiers $tblInclude = [ 'champInput', 'champListe', 'config_php', 'controller_php', 'idx1', 'idx3', 'idx20', 'saisie_phtml', 'saisieFormAwareTrait_php', 'service_php', 'serviceTrait_php', ]; $i = 0; foreach ($tblInclude as $ci) { ${$ci} = file_get_contents(__DIR__ . '/template/GenerateRubrique/' . $ci); } $extractInput = '\'champtiret\' => $object->getchampmaj() ,'; $extractListe = '\'champtiret\' => ($s = $object->getchampmaj()) ? $s->getId() : null, '; $hydrateInput = '$object->setchampmaj($data[\'champtiret\']); '; $hydrateListe = 'if (array_key_exists(\'champtiret\', $data)) { $object->setchampmaj($this->getServicechampmaj()->get($data[\'champtiret\'])); } '; $idx21 = " <th style=\"word-wrap: break-word ; \">champphrase</th>\n"; $idx2 = ' <td style="word-wrap: break-word ; "><?= $fr->getchampmaj() ?></td>' . "\n"; $service10 = " use Application\Service\Traits\champmajServiceAwareTrait;\n"; $service11 = " use champmajServiceAwareTrait;\n"; $fic10 = '/** * Description of nommajSaisieForm * * @author ZVENIGOROSKY Alexandre <alexandre.zvenigorosky@unicaen.fr> */ class nommajSaisieForm extends AbstractForm { '; $fic11 = ' public function init() { $hydrator = new nommajHydrator(); $this->setHydrator($hydrator); $this->setAttribute(\'action\', $this->getCurrentUrl()); '; $fic1 = '<?php namespace Application\Form\nommaj; use Application\Form\AbstractForm; use Zend\Form\Element\Csrf; use Zend\Hydrator\HydratorInterface; '; $fic2 = ' $this->add(new Csrf(\'security\')); $this->add([ \'name\' => \'submit\', \'type\' => \'Submit\', \'attributes\' => [ \'value\' => "Enregistrer", \'class\' => \'btn btn-primary\', ], ]); return $this; } /** * Should return an array specification compatible with * {@link Zend\InputFilter\Factory::createInputFilter()}. * * @return array */ public function getInputFilterSpecification() { return ['; $fic21 = ' ]; } } class nommajHydrator implements HydratorInterface { '; $listeEnteteHydrator = 'use champmajServiceAwareTrait; '; $fic22 = ' /** * Hydrate $object with the provided $data. * * @param array $data * @param \Application\Entity\Db\nommaj $object * * @return object */ public function hydrate(array $data, $object) { '; $inputFilter = ' \'champtiret\' => [ \'required\' => true, ], '; $fic3 = ' return $object; } /** * Extract values from an object * * @param \Application\Entity\Db\nommaj $object * * @return array */ public function extract($object) { $data = [ '; $fic4 = ' ]; return $data; } } '; for ($i = 0; $i < $nbe; $i++) { $champ = $nomChamps[$i]; $champsous = strtoupper($champ); $champtiret = $champm = strtolower($champ); $champtiret = str_replace('_', '-', $champtiret); $champm = preg_replace_callback('/_\w/', function ($m) { return strtoupper($m[0]); }, $champm); $champmaj = preg_replace_callback('/^\w/', function ($m) { return strtoupper($m[0]); }, $champm); $champphrase = str_replace('_', ' ', $champmaj); $champm = str_replace('_', '', $champm); $champmaj = str_replace('_', '', $champmaj); # input:champInput.txt listeInput.txt ... if (($i) && ($notnullChamps[$i])) { $txt = str_replace('champtiret', $champtiret, $inputFilter); $txt6 .= $txt; } if ($champtiret != 'id') { $txt52 = str_replace('champphrase', $champphrase, $idx21); $txt53 .= $txt52; } if (!$listeChamps[$i]) { $champType = 'Text'; if ($typeChamps[$i] == 'String') $champType = 'Text'; if ($typeChamps[$i] == 'boolean') $champType = 'Checkbox'; if ($champtiret != 'id') { $txt51 = str_replace('champmaj', $champmaj, $idx2); $txt5 .= $txt51; } if ($champtiret != 'id') { $txt = str_replace(['champtiret', 'champphrase', $champType], [$champtiret, $champphrase, $champType], $champInput); $txt4 .= $txt; } if ($champtiret != 'id') { $txt = str_replace('champtiret', $champtiret, $hydrateInput); $txt = str_replace('champmaj', $champmaj, $txt); $txt41 .= $txt; } $txt = str_replace('champtiret', $champtiret, $extractInput); $txt = str_replace('champmaj', $champmaj, $txt); $txt42 .= $txt; } else { $txt51 = str_replace('champmaj', $champmaj, $idx2); $txt52 = str_replace('champphrase', $champphrase, $idx21); $txt5 .= $txt51; $txt52 .= $txt53; $txt = str_replace('champmaj', $champmaj, $service10); $txt0 .= $txt; $txt = str_replace('champmaj', $champmaj, $service11); $txt01 .= $txt; $txt = str_replace(['champtiret', 'champphrase', 'champmaj'], [$champtiret, $champphrase, $champmaj], $champListe); if ($notnullChamps[$i]) { $txt = str_replace(' ->setEmptyOption("(Aucun)")', '', $txt); } $txt43 .= $txt; $txt = str_replace(['champtiret', 'champmaj'], [$champtiret, $champmaj], $hydrateListe); $txt44 .= $txt; $txt = str_replace(['champtiret', 'champmaj'], [$champtiret, $champmaj], $extractListe); $txt45 .= $txt; $txt = str_replace('champmaj', $champmaj, $listeEnteteHydrator); $txt7 .= $txt; } } $saisieForm_php = $fic1 . $txt0 . $fic10 . $txt01 . $fic11 . $txt4 . $txt43 . $fic2 . $txt6 . $fic21 . $txt7 . $fic22 . $txt41 . $txt44 . $fic3 . $txt42 . $txt45 . $fic4; $index_phtml = $idx1 . $txt53 . $idx20 . $txt5 . $idx3; $reps = ['config/', 'src/Application/Controller/', 'src/Application/Form/', 'src/Application/Form/', 'view/application/', 'view/application/', 'src/Application/Service/', 'src/Application/Service/Traits/']; $rep2regs = ['', '', $nommaj . '/', $nommaj . '/Traits/', $nomtiret . '/', $nomtiret . '/', '', '']; $fichmotifs = [$config_php, $controller_php, $saisieForm_php, $saisieFormAwareTrait_php, $index_phtml, $saisie_phtml, $service_php, $serviceTrait_php]; $fichiers = [$nomtiret . '.config.php', $nommaj . 'Controller.php', $nommaj . 'SaisieForm.php', $nommaj . 'SaisieFormAwareTrait.php', 'index.phtml', 'saisie.phtml', $nommaj . 'Service.php', $nommaj . 'ServiceAwareTrait.php']; $motif = ['nomsous', 'nommaj', 'nomm', 'nomtiret', 'droitedition', 'droitvisualisation', 'nomphrase']; $remplace = [$nomsous, $nommaj, $nomm, $nomtiret, $nomedit, $nomvisu, $nomphrase]; $maxfichiers = sizeof($fichiers); for ($i = 0; $i < $maxfichiers; $i++) { $rep = $reps[$i]; $fich = $fichiers[$i]; $fmotif = $fichmotifs[$i]; $rep2reg = $rep2regs[$i]; $ficin = $fichmotifs[$i]; $ficout = $basefs . $rep . $rep2reg . $fich; $ficenr = $baseOrig . $rep . $rep2reg . $fich; // echo 'Ficout=' . $ficout.', ficenr:'.$ficenr.'<br>'; $txt = str_replace($motif, $remplace, $ficin); if (!file_exists($basefs . $rep . $rep2reg)) { $chemin = $basefs . $rep . $rep2reg; $res = exec("mkdir -p $chemin", $err); // echo "res:$res<BR>"; } $hdl = fopen($ficout, 'wb'); $lgWrite = fwrite($hdl, $txt); fclose($hdl); if (file_exists($ficenr)) { if ($nonRemplace) { unset($result); exec("diff $ficenr $ficout", $result); echo "<BR><span style=\"color:#00bbdd; \">Différences avec $ficenr<BR><HR></span>"; echo '<div style="position:relative; left:10%; font-family:Lucida Console; background-color:lightgrey; width:85% ">'; $nblg = sizeof($result); for ($j = 0; $j < $nblg; $j++) { echo '<span style="color:'; if (preg_match('/^\d/', $result[$j])) echo '#008bb9'; if (preg_match('/^>/', $result[$j])) echo '#000000'; if (preg_match('/^</', $result[$j])) echo '#00bb00'; if (preg_match('/^---/', $result[$j])) { echo '; width:500px; "><hr width="85%" size="2"></span>'; } else echo "; \"><BR>$result[$j]</span>"; } echo '</div>'; unlink($ficout); } else echo '<div style="color:#ff0000; ">Attention --> Le fichier module/Application/' . $ficout . ' existe dans /var/www/OSE !</div>'; } } echo "<BR><span style=\"color:#006600; \">Résultat dans l'arborescence $basefs</span>"; }
code/NewEntityService.php +68 −52 Changes for code/NewEntityService.php: 68 added lines, 52 removed lines. Original line number Diff line number Diff line <?php use UnicaenCode\Form\AbstractForm; use UnicaenCode\Form\ElementMakerForm; use UnicaenCode\Util; Loading @@ -10,60 +11,75 @@ use UnicaenCode\Util; * @var $viewName string * @var $viewFile string */ class EntityServiceForm extends AbstractForm { public function init() { $this->addSelectEntity( 'entityClass', 'Entité du service' ); ?> <h1>Création d'un nouveau service d'entité OSE</h1> <h3>Etape 1 : Paramétrage</h3> $this->addText( 'alias', 'Alias d\'entité' ); <?php $this->addCheckbox( 'awareTrait', 'Générer un trait', true ); $form = new \Zend\Form\Form(); $form->add(ElementMakerForm::selectEntity( 'entity', 'Entité correspondante', 'Application\Entity\Db' )); $form->add(ElementMakerForm::text('alias', 'Alias d\'entité')); $form->add(ElementMakerForm::submit('generate', 'Générer le service')); $form->setData($controller->getRequest()->getPost()); Util::displayForm($form); if ($controller->getRequest()->isPost() && $form->isValid()) { $entity = $form->get('entity')->getValue(); $sCodeGenerator = Util::codeGenerator(); /* @var $sCodeGenerator \UnicaenCode\Service\CodeGenerator */ $params = $sCodeGenerator->generateServiceParams([ 'classname' => 'Application\\Service\\' . $entity . 'Service', 'generateTrait' => true, 'generateInterface' => false, ], [ 'Class' => [ 'template' => 'EntityService', 'entity' => $entity, 'alias' => $form->get('alias')->getValue(), ], 'Factory' => [ 'template' => 'EntityServiceFactory', ], ]); ?> <h3>Etape 2 : Création des fichiers sources du service</h3> <?php $sCodeGenerator->generateFiles($params); ?> <div class="alert alert-info">Les fichiers sont récupérables dans le dossier <?= $sCodeGenerator->getOutputDir() ?></div> <h3>Etape 3 : Déclaration dans le fichier de configuration</h3> <?php $sCodeGenerator->generateFile($params['Config'], false); ?> <div class="alert alert-warning"> Vous devez vous-même placer ces informations dans le fichier de configuration de votre module. </div> $this->addCheckbox( 'awareInterface', 'Générer une interface', false ); <?php $this->addCheckbox( 'subDir', 'Les traits, interfaces et Factory seront placés dans des sous-dossiers dédiés', false ); } public function getParams(): array { $params = [ 'generator' => 'Service', 'type' => 'Service', 'entityClass' => $this->get('entityClass')->getValue(), 'useGetter' => true, 'subDir' => $this->get('subDir')->getValue(), 'alias' => $this->get('alias')->getValue(), ]; $params['entityClassname'] = Util::classClassname($params['entityClass']); $params['class'] = Util::classModule($params['entityClass']) . '\Service\\' . $params['entityClassname'] . 'Service'; if ($this->get('awareTrait')->getValue()) { $params['awareTrait'] = []; } if ($this->get('awareInterface')->getValue()) { $params['awareInterface'] = []; } $params['factory'] = []; return $params; } } $cg = \UnicaenCode\Util::codeGenerator(); $cg->start('Création d\'un nouveau service d\'entité OSE'); $form = new EntityServiceForm(); $form->init(); $form->addText('alias', 'Alias d\'entité'); if (empty($params = $cg->formPublish($form))) return; $params['template'] = 'EntityService'; $params['factory']['template'] = 'EntityServiceFactory'; $cg->end($params); No newline at end of file
code/NewOseForm.phpdeleted 100755 → 0 +0 −93 Changes for code/NewOseForm.php: 0 added lines, 93 removed lines. Original line number Diff line number Diff line <?php use UnicaenCode\Form\ElementMakerForm; use UnicaenCode\Util; /** * @var $this \Application\View\Renderer\PhpRenderer * @var $controller \Zend\Mvc\Controller\AbstractController * @var $container \Psr\Container\ContainerInterface * @var $viewName string * @var $viewFile string */ ?> <h1>Création d'un nouveau formulaire</h1> <h3>Etape 1 : Paramétrage</h3> <?php $form = new \Zend\Form\Form(); $form->add(ElementMakerForm::selectModule( 'module', 'Module dans lequel sera placé votre formulaire' )); $form->add(ElementMakerForm::select( 'type', 'Type de formulaire (Form ou Fieldset)', ['Form' => 'Form', 'Fieldset' => 'Fieldset'], 'Form' )); $form->add(ElementMakerForm::text( 'classname', 'Nom de classe du formulaire (en CamelCase, avec éventuellement un namespace avant : MonNamespace\Exemple)', 'Exemple' )); $form->add(ElementMakerForm::checkbox( 'useHydrator', 'Implémenter un hydrateur spécifique' )); $form->add(ElementMakerForm::checkbox( 'generateTrait', 'Générer un trait', true )); $form->add(ElementMakerForm::checkbox( 'generateInterface', 'Générer une interface', false )); $form->add(ElementMakerForm::checkbox( 'useGetter', 'Générer des getters dans les traits et les interfaces', true )); $form->add(ElementMakerForm::checkbox( 'generateFactory', 'Générer une factory', true )); $form->add(ElementMakerForm::submit('generate', 'Générer le formulaire')); $form->setData($controller->getRequest()->getPost()); Util::displayForm($form); if ($controller->getRequest()->isPost() && $form->isValid()) { $type = $form->get('type')->getValue(); $classname = $form->get('classname')->getValue(); $sCodeGenerator = Util::codeGenerator() /* @var $sCodeGenerator \UnicaenCode\Service\CodeGenerator */ $params = $sCodeGenerator->generateFormParams([ 'type' => $type, 'classname' => $form->get('module')->getValue() . '\\Form\\' . $classname . $type, 'name' => ($type == 'Fieldset' ? 'fieldset' : '') . str_replace('\\', '', $classname), 'useHydrator' => $form->get('useHydrator')->getValue(), 'generateTrait' => $form->get('generateTrait')->getValue(), 'generateInterface' => $form->get('generateInterface')->getValue(), 'generateFactory' => $form->get('generateFactory')->getValue(), 'useGetter' => $form->get('useGetter')->getValue(), ], [ 'Class' => [ 'template' => 'OseForm', 'useSubForm' => false !== strpos($classname, '\\'), ], ]); ?> <h3>Etape 2 : Création des fichiers sources du formulaire</h3> <?php $sCodeGenerator->generateFiles($params); ?> <div class="alert alert-info">Les fichiers sont récupérables dans le dossier <?= $sCodeGenerator->getOutputDir() ?></div> <h3>Etape 3 : Déclaration dans le fichier de configuration</h3> <?php $sCodeGenerator->generateFile($params['Config'], false); ?> <div class="alert alert-warning"> Vous devez vous-même placer ces informations dans le fichier de configuration de votre module. </div> <?php } No newline at end of file
code/template/EntityService.php +7 −7 Changes for code/template/EntityService.php: 7 added lines, 7 removed lines. Original line number Diff line number Diff line Loading @@ -2,16 +2,16 @@ namespace <namespace>; use Application\Entity\Db\<entity>; use <entityClass>; /** * Description of <classname> * * @author <author> * * @method <entity> get($id) * @method <entity>[] getList(\Doctrine\ORM\QueryBuilder $qb = null, $alias = null) * @method <entity> newEntity() * @method <entityClassname> get($id) * @method <entityClassname>[] getList(\Doctrine\ORM\QueryBuilder $qb = null, $alias = null) * @method <entityClassname> newEntity() * */ class <classname> extends AbstractEntityService Loading @@ -23,9 +23,9 @@ class <classname> extends AbstractEntityService * @return string * @throws RuntimeException */ public function getEntityClass() public function getEntityClass(): string { return <entity>::class; return <entityClassname>::class; } Loading @@ -35,7 +35,7 @@ class <classname> extends AbstractEntityService * * @return string */ public function getAlias(){ public function getAlias(): string{ return '<alias>'; } Loading
code/template/Form.php 0 → 100644 +93 −0 Changes for code/template/Form.php: 93 added lines, 0 removed lines. Original line number Diff line number Diff line <?php namespace <namespace>; use Application\Form\AbstractForm; <if useHydrator> use Zend\Stdlib\Hydrator\HydratorInterface; <endif useHydrator> /** * Description of <classname> * * @author <author> */ class <classname> extends AbstractForm { public function init() { <if useHydrator> $hydrator = new <classname>Hydrator; $this->setHydrator($hydrator); <endif useHydrator> $this->setAttribute('action',$this->getCurrentUrl()); /* Ajoutez vos éléments de formulaire ici */ $this->add([ 'name' => 'submit', 'type' => 'Submit', 'attributes' => [ 'value' => 'Enregistrer', 'class' => 'btn btn-primary', ], ]); } /** * Should return an array specification compatible with * {@link Zend\InputFilter\Factory::createInputFilter()}. * * @return array */ public function getInputFilterSpecification() { return [ /* Filtres et validateurs */ ]; } } <if useHydrator> class <classname>Hydrator implements HydratorInterface { /** * @param array $data * @param $object * * @return object */ public function hydrate(array $data, $object) { /* on peuple l'objet à partir du tableau de données */ return $object; } /** * @param $object * * @return array */ public function extract($object) { $data = [ /* On peuple le tableau avec les données de l'objet */ ]; return $data; } } <endif useHydrator> No newline at end of file