From 16adb4256d9668819a893b7909b6916c3dfc4d18 Mon Sep 17 00:00:00 2001 From: lecluse <lecluse@d57fa8bc-6af1-4de9-8b7d-78e900e231e7> Date: Wed, 22 Jul 2015 15:24:51 +0000 Subject: [PATCH] Utilisation de UnicaenCode --- code/entityInterfaceGenerator.php | 28 ++++ code/entityTraitGenerator.php | 27 ++++ code/serviceTraitGenerator.php | 23 ++++ code/template/EntityInterface.php | 24 ++++ code/template/EntityTrait.php | 37 ++++++ code/template/ServiceTrait.php | 51 +++++++ composer.json | 3 +- composer.lock | 125 +++++++++++------- config/application.config.php | 8 +- config/autoload/unicaen-code.global.php | 10 ++ .../Controller/IndexController.php | 1 + .../src/Application/Service/Intervenant.php | 4 +- 12 files changed, 284 insertions(+), 57 deletions(-) create mode 100644 code/entityInterfaceGenerator.php create mode 100644 code/entityTraitGenerator.php create mode 100644 code/serviceTraitGenerator.php create mode 100644 code/template/EntityInterface.php create mode 100644 code/template/EntityTrait.php create mode 100644 code/template/ServiceTrait.php create mode 100644 config/autoload/unicaen-code.global.php diff --git a/code/entityInterfaceGenerator.php b/code/entityInterfaceGenerator.php new file mode 100644 index 0000000000..c2f6524ffa --- /dev/null +++ b/code/entityInterfaceGenerator.php @@ -0,0 +1,28 @@ +<?php + +use UnicaenCode\Util; + + +$outputdir = '/tmp/entityInterfaces/'; + +$sIntrospection = $controller->getServiceLocator()->get('UnicaenCode\Introspection'); +/* @var $sIntrospection \UnicaenCode\Service\Introspection */ + +$sCodeGenerator = $controller->getServiceLocator()->get('UnicaenCode\CodeGenerator'); +/* @var $sCodeGenerator \UnicaenCode\Service\CodeGenerator */ + +$entities = $sIntrospection->getEntities(); + +$sCodeGenerator->setTemplate('EntityInterface'); +foreach( $entities as $entity ){ + + $entityPath = Util::namespaceClass($entity); + $entityClass = Util::baseClassName($entity); + $entityParam = lcfirst($entityClass); + + $sCodeGenerator->setParams( compact('entityPath', 'entityClass', 'entityParam') ); + $sCodeGenerator->generateToFile($outputdir, $entityClass.'AwareInterface.php'); +} +?> +<h1>Génération des aware interfaces de getters/setters d\'entités</h1> +Résultats dans <b><?php echo $outputdir ?></b> \ No newline at end of file diff --git a/code/entityTraitGenerator.php b/code/entityTraitGenerator.php new file mode 100644 index 0000000000..079282e799 --- /dev/null +++ b/code/entityTraitGenerator.php @@ -0,0 +1,27 @@ +<h1>Génération des aware traits de getters/setters d\'entités</h1> +<?php + +use UnicaenCode\Util; + +$outputdir = '/tmp/entityTraits/'; + +$sIntrospection = $controller->getServiceLocator()->get('UnicaenCode\Introspection'); +/* @var $sIntrospection \UnicaenCode\Service\Introspection */ + +$sCodeGenerator = $controller->getServiceLocator()->get('UnicaenCode\CodeGenerator'); +/* @var $sCodeGenerator \UnicaenCode\Service\CodeGenerator */ + +$entities = $sIntrospection->getEntities(); + +$sCodeGenerator->setTemplate('EntityTrait'); +foreach( $entities as $entity ){ + + $entityPath = Util::namespaceClass($entity); + $entityClass = Util::baseClassName($entity); + $entityParam = lcfirst($entityClass); + + $sCodeGenerator->setParams( compact('entityPath', 'entityClass', 'entityParam') ); + $sCodeGenerator->generateToFile($outputdir, $entityClass.'AwareTrait.php'); +} +?> +Résultats dans <b><?php echo $outputdir ?></b> \ No newline at end of file diff --git a/code/serviceTraitGenerator.php b/code/serviceTraitGenerator.php new file mode 100644 index 0000000000..416cd08368 --- /dev/null +++ b/code/serviceTraitGenerator.php @@ -0,0 +1,23 @@ +<h1>Génération des aware traits d\'accès aux services</h1> +<?php + +$outputdir = '/tmp/serviceTraits/'; + +$sIntrospection = $controller->getServiceLocator()->get('UnicaenCode\Introspection'); +/* @var $sIntrospection \UnicaenCode\Service\Introspection */ + +$sCodeGenerator = $controller->getServiceLocator()->get('UnicaenCode\CodeGenerator'); +/* @var $sCodeGenerator \UnicaenCode\Service\CodeGenerator */ + + + +$services = $sIntrospection->getServices('Application\\Service\\', 'AbstractService'); + +$sCodeGenerator->setTemplate('ServiceTrait'); +foreach( $services as $entityClass ){ + $sCodeGenerator->setParams( compact('entityClass') ); + $sCodeGenerator->generateToFile($outputdir, $entityClass.'AwareTrait.php'); +} + +?> +Résultats dans <b><?php echo $outputdir ?></b> \ No newline at end of file diff --git a/code/template/EntityInterface.php b/code/template/EntityInterface.php new file mode 100644 index 0000000000..015155a66e --- /dev/null +++ b/code/template/EntityInterface.php @@ -0,0 +1,24 @@ +<?php + +namespace Application\Interfaces; + +use <entityPath>\<entityClass>; + +/** + * + * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr> + */ +interface <entityClass>AwareInterface +{ + + /** + * @param <entityClass> $<entityParam> + * @return self + */ + public function set<entityClass>(<entityClass> $<entityParam> = null); + + /** + * @return <entityClass> + */ + public function get<entityClass>(); +} \ No newline at end of file diff --git a/code/template/EntityTrait.php b/code/template/EntityTrait.php new file mode 100644 index 0000000000..18941eb9f9 --- /dev/null +++ b/code/template/EntityTrait.php @@ -0,0 +1,37 @@ +<?php + +namespace Application\Traits; + +use <entityPath>\<entityClass>; + +/** + * Description of <entityClass>AwareTrait + * + * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr> + */ +trait <entityClass>AwareTrait +{ + /** + * @var <entityClass> + */ + protected $<entityParam>; + + /** + * @param <entityClass> $<entityParam> + * @return self + */ + public function set<entityClass>(<entityClass> $<entityParam> = null) + { + $this-><entityParam> = $<entityParam>; + + return $this; + } + + /** + * @return <entityClass> + */ + public function get<entityClass>() + { + return $this-><entityParam>; + } +} \ No newline at end of file diff --git a/code/template/ServiceTrait.php b/code/template/ServiceTrait.php new file mode 100644 index 0000000000..1c7ff0346c --- /dev/null +++ b/code/template/ServiceTrait.php @@ -0,0 +1,51 @@ +<?php + +namespace Application\Service\Traits; + +use Application\Service\<entityClass>; +use Common\Exception\RuntimeException; + +trait <entityClass>AwareTrait +{ + /** + * description + * + * @var <entityClass> + */ + private $service<entityClass>; + + /** + * + * @param <entityClass> $service<entityClass> + * @return self + */ + public function setService<entityClass>( <entityClass> $service<entityClass> ) + { + $this->service<entityClass> = $service<entityClass>; + return $this; + } + + /** + * + * @return <entityClass> + * @throws \Common\Exception\RuntimeException + */ + public function getService<entityClass>() + { + if (empty($this->service<entityClass>)){ + if (! method_exists($this, 'getServiceLocator')) { + throw new RuntimeException( 'La classe '.get_class($this).' n\'a pas accès au ServiceLocator.'); + } + + $serviceLocator = $this->getServiceLocator(); + if (method_exists($serviceLocator, 'getServiceLocator')) { + $serviceLocator = $serviceLocator->getServiceLocator(); + } + + return $serviceLocator->get('application<entityClass>'); + }else{ + return $this->service<entityClass>; + } + } + +} \ No newline at end of file diff --git a/composer.json b/composer.json index 1d428d3271..b5681acf5c 100755 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "require-dev": { "zendframework/zend-test": ">=2.3", "phpunit/PHPUnit": ">=3.7", - "zendframework/zend-developer-tools": "dev-master" + "zendframework/zend-developer-tools": "dev-master", + "unicaen/unicaen-code": "dev-trunk" } } diff --git a/composer.lock b/composer.lock index 5ff764274c..4043fff2af 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "84e7c075059944efa7196a1b7f16a7a7", + "hash": "2e5b200c550fe0380663902466ee4d6e", "packages": [ { "name": "bjyoungblood/bjy-authorize", @@ -1175,7 +1175,7 @@ "source": { "type": "svn", "url": "https://svn.unicaen.fr/svn/UnicaenApp", - "reference": "/trunk/@616" + "reference": "/trunk/@617" }, "require": { "doctrine/doctrine-orm-module": ">=0.7", @@ -1273,7 +1273,7 @@ }, { "name": "zendframework/zend-authentication", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-authentication.git", @@ -1336,7 +1336,7 @@ }, { "name": "zendframework/zend-cache", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-cache.git", @@ -1397,7 +1397,7 @@ }, { "name": "zendframework/zend-code", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-code.git", @@ -1450,7 +1450,7 @@ }, { "name": "zendframework/zend-config", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-config.git", @@ -1507,7 +1507,7 @@ }, { "name": "zendframework/zend-console", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-console.git", @@ -1557,7 +1557,7 @@ }, { "name": "zendframework/zend-crypt", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-crypt.git", @@ -1608,7 +1608,7 @@ }, { "name": "zendframework/zend-db", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-db.git", @@ -1661,7 +1661,7 @@ }, { "name": "zendframework/zend-escaper", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-escaper.git", @@ -1706,7 +1706,7 @@ }, { "name": "zendframework/zend-eventmanager", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-eventmanager.git", @@ -1752,7 +1752,7 @@ }, { "name": "zendframework/zend-filter", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-filter.git", @@ -1808,7 +1808,7 @@ }, { "name": "zendframework/zend-form", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-form.git", @@ -1875,7 +1875,7 @@ }, { "name": "zendframework/zend-http", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-http.git", @@ -1925,7 +1925,7 @@ }, { "name": "zendframework/zend-i18n", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-i18n.git", @@ -1989,7 +1989,7 @@ }, { "name": "zendframework/zend-inputfilter", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-inputfilter.git", @@ -2041,7 +2041,7 @@ }, { "name": "zendframework/zend-json", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-json.git", @@ -2095,7 +2095,7 @@ }, { "name": "zendframework/zend-ldap", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-ldap.git", @@ -2147,7 +2147,7 @@ }, { "name": "zendframework/zend-loader", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-loader.git", @@ -2192,7 +2192,7 @@ }, { "name": "zendframework/zend-log", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-log.git", @@ -2254,7 +2254,7 @@ }, { "name": "zendframework/zend-mail", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-mail.git", @@ -2309,7 +2309,7 @@ }, { "name": "zendframework/zend-math", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-math.git", @@ -2360,7 +2360,7 @@ }, { "name": "zendframework/zend-mime", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-mime.git", @@ -2410,7 +2410,7 @@ }, { "name": "zendframework/zend-modulemanager", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-modulemanager.git", @@ -2468,7 +2468,7 @@ }, { "name": "zendframework/zend-mvc", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-mvc.git", @@ -2555,7 +2555,7 @@ }, { "name": "zendframework/zend-navigation", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-navigation.git", @@ -2614,7 +2614,7 @@ }, { "name": "zendframework/zend-paginator", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-paginator.git", @@ -2724,7 +2724,7 @@ }, { "name": "zendframework/zend-serializer", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-serializer.git", @@ -2777,7 +2777,7 @@ }, { "name": "zendframework/zend-servicemanager", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-servicemanager.git", @@ -2827,7 +2827,7 @@ }, { "name": "zendframework/zend-session", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-session.git", @@ -2888,16 +2888,16 @@ }, { "name": "zendframework/zend-stdlib", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-stdlib.git", - "reference": "a5dd7fd2ba6e8f6c6ea5a12db0605d3aa48af1e7" + "reference": "d8ecb629a72da9f91bd95c5af006384823560b42" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zendframework/zend-stdlib/zipball/a5dd7fd2ba6e8f6c6ea5a12db0605d3aa48af1e7", - "reference": "a5dd7fd2ba6e8f6c6ea5a12db0605d3aa48af1e7", + "url": "https://api.github.com/repos/zendframework/zend-stdlib/zipball/d8ecb629a72da9f91bd95c5af006384823560b42", + "reference": "d8ecb629a72da9f91bd95c5af006384823560b42", "shasum": "" }, "require": { @@ -2939,11 +2939,11 @@ "stdlib", "zf2" ], - "time": "2015-05-07 14:55:31" + "time": "2015-07-21 13:55:46" }, { "name": "zendframework/zend-uri", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-uri.git", @@ -2991,7 +2991,7 @@ }, { "name": "zendframework/zend-validator", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-validator.git", @@ -3055,16 +3055,16 @@ }, { "name": "zendframework/zend-version", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-version.git", - "reference": "c02ce68d2c2617cd222c92545d2bf820bb9e0837" + "reference": "f5d1b305dee6629b796a1f5307b3243a695abab7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zendframework/zend-version/zipball/c02ce68d2c2617cd222c92545d2bf820bb9e0837", - "reference": "c02ce68d2c2617cd222c92545d2bf820bb9e0837", + "url": "https://api.github.com/repos/zendframework/zend-version/zipball/f5d1b305dee6629b796a1f5307b3243a695abab7", + "reference": "f5d1b305dee6629b796a1f5307b3243a695abab7", "shasum": "" }, "require": { @@ -3101,11 +3101,11 @@ "version", "zf2" ], - "time": "2015-06-16 21:50:03" + "time": "2015-07-21 14:00:02" }, { "name": "zendframework/zend-view", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-view.git", @@ -4264,6 +4264,30 @@ "homepage": "https://symfony.com", "time": "2015-07-01 11:25:50" }, + { + "name": "unicaen/unicaen-code", + "version": "dev-trunk", + "source": { + "type": "svn", + "url": "https://svn.unicaen.fr/svn/UnicaenCode", + "reference": "/trunk/@3" + }, + "require-dev": { + "phpunit/phpunit": ">=3.7", + "zendframework/zend-test": ">=2.2" + }, + "type": "library", + "autoload": { + "psr-0": { + "UnicaenCode": "src/", + "UnicaenCodeTest": "tests/" + }, + "classmap": [ + "./Module.php" + ] + }, + "description": "Boite à outils pour la programmation avec la bibliothèque Unicaen" + }, { "name": "zendframework/zend-debug", "version": "2.5.1", @@ -4319,12 +4343,12 @@ "source": { "type": "git", "url": "https://github.com/zendframework/ZendDeveloperTools.git", - "reference": "d3432681aa32177a741ad23604a40af9ad454acb" + "reference": "a3399877d69491acdac86e3aead53937c16b0303" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zendframework/ZendDeveloperTools/zipball/d3432681aa32177a741ad23604a40af9ad454acb", - "reference": "d3432681aa32177a741ad23604a40af9ad454acb", + "url": "https://api.github.com/repos/zendframework/ZendDeveloperTools/zipball/a3399877d69491acdac86e3aead53937c16b0303", + "reference": "a3399877d69491acdac86e3aead53937c16b0303", "shasum": "" }, "require": { @@ -4379,11 +4403,11 @@ "module", "zf2" ], - "time": "2015-05-06 10:10:04" + "time": "2015-07-21 09:14:24" }, { "name": "zendframework/zend-dom", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-dom.git", @@ -4429,7 +4453,7 @@ }, { "name": "zendframework/zend-test", - "version": "2.4.3", + "version": "2.4.4", "source": { "type": "git", "url": "https://github.com/zendframework/zend-test.git", @@ -4494,7 +4518,8 @@ "unicaen/unicaen-auth": 20, "unicaen/unicaen-ldap": 20, "bjyoungblood/bjy-authorize": 20, - "zendframework/zend-developer-tools": 20 + "zendframework/zend-developer-tools": 20, + "unicaen/unicaen-code": 20 }, "prefer-stable": false, "prefer-lowest": false, diff --git a/config/application.config.php b/config/application.config.php index b1a066ff92..4867e344cd 100755 --- a/config/application.config.php +++ b/config/application.config.php @@ -9,12 +9,12 @@ $modules = [ 'UnicaenLdap', 'Common', 'Application', - 'Import', - //'ZendDeveloperTools' + 'Import' ]; -if ( file_exists(dirname(dirname(__FILE__)).'/module/Debug') ) { - $modules[] = 'Debug'; +if ( 'development' == $env ) { + $modules[] = 'ZendDeveloperTools'; + $modules[] = 'UnicaenCode'; } return [ diff --git a/config/autoload/unicaen-code.global.php b/config/autoload/unicaen-code.global.php new file mode 100644 index 0000000000..1a33e4477f --- /dev/null +++ b/config/autoload/unicaen-code.global.php @@ -0,0 +1,10 @@ +<?php + +$settings = [ + 'view-dir' => APPLICATION_PATH . '/code', + 'template-dir' => APPLICATION_PATH . '/code/template', +]; + +return [ + 'unicaen-code' => $settings, +]; \ No newline at end of file diff --git a/module/Application/src/Application/Controller/IndexController.php b/module/Application/src/Application/Controller/IndexController.php index a30398dfe3..bd84f24e2e 100755 --- a/module/Application/src/Application/Controller/IndexController.php +++ b/module/Application/src/Application/Controller/IndexController.php @@ -3,6 +3,7 @@ namespace Application\Controller; use Application\Service\Traits\IntervenantAwareTrait; +use UnicaenCode\Util; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; diff --git a/module/Application/src/Application/Service/Intervenant.php b/module/Application/src/Application/Service/Intervenant.php index dc566b04ad..c2b3d830e3 100644 --- a/module/Application/src/Application/Service/Intervenant.php +++ b/module/Application/src/Application/Service/Intervenant.php @@ -212,8 +212,6 @@ class Intervenant extends AbstractEntityService */ public function importer($sourceCode) { - $repo = $this->getEntityManager()->getRepository($this->getEntityClass()); - if ($intervenant = $this->getBySourceCode($sourceCode)) { return $intervenant; } @@ -222,6 +220,8 @@ class Intervenant extends AbstractEntityService /* @var $import Import */ $import->intervenant($sourceCode); + $repo = $this->getEntityManager()->getRepository($this->getEntityClass()); + if (!($intervenant = $repo->findOneBySourceCode($sourceCode))) { throw new RuntimeException("Vous n'êtes pas autorisé à vous connecter à OSE avec ce compte. Vous vous prions de vous rapprocher de votre composante pour en obtenir un valide."); } -- GitLab