From dcd64186c73aff6d5bb1dc5308365c241162abae Mon Sep 17 00:00:00 2001 From: JMSBot Date: Tue, 2 Oct 2012 00:36:30 +0200 Subject: [PATCH] changes from automatic review --- Admin/TemplateAdmin.php | 80 ++++---- DependencyInjection/Configuration.php | 1 - .../DifaneTwigDatabaseExtension.php | 3 +- Entity/Template.php | 32 +-- Repository/TemplateRepository.php | 93 ++++----- Subscriber/TableNameSubscriber.php | 70 +++---- Tests/Twig/DatabaseLoaderTest.php | 22 +-- Twig/DatabaseLoader.php | 182 ++++++++---------- 8 files changed, 229 insertions(+), 254 deletions(-) diff --git a/Admin/TemplateAdmin.php b/Admin/TemplateAdmin.php index 55cc5a4..693a09c 100644 --- a/Admin/TemplateAdmin.php +++ b/Admin/TemplateAdmin.php @@ -1,41 +1,39 @@ -addIdentifier('name') - ->add('created_at') - ->add('updated_at') - ; - } - - protected function configureDatagridFilters(DatagridMapper $filterMapper) - { - $filterMapper - ->add('name') - ->add('created_at') - ->add('updated_at') - ; - } - - protected function configureFormFields(FormMapper $formMapper) - { - $formMapper - ->with('General') - ->add('name') - ->add('content') - ->end() - ; - } -} \ No newline at end of file +addIdentifier('name') + ->add('created_at') + ->add('updated_at') + ; + } + + protected function configureDatagridFilters(DatagridMapper $filterMapper) + { + $filterMapper + ->add('name') + ->add('created_at') + ->add('updated_at') + ; + } + + protected function configureFormFields(FormMapper $formMapper) + { + $formMapper + ->with('General') + ->add('name') + ->add('content') + ->end() + ; + } +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 0b850e6..02c2f06 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -38,7 +38,6 @@ public function getConfigTreeBuilder() // Here you should define the parameters that are allowed to // configure your bundle. See the documentation linked above for // more information on that topic. - return $treeBuilder; } } diff --git a/DependencyInjection/DifaneTwigDatabaseExtension.php b/DependencyInjection/DifaneTwigDatabaseExtension.php index 29d107d..a2d1e2d 100644 --- a/DependencyInjection/DifaneTwigDatabaseExtension.php +++ b/DependencyInjection/DifaneTwigDatabaseExtension.php @@ -28,8 +28,7 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('difane.bundle.twigdatabase.twig.loader.database.autocreatetemplates', $config['auto_create_templates']); $container->setParameter('difane.bundle.twigdatabase.database.tablename', $config['table_name']); - if(true == $config['sonata_admin']['enabled']) - { + if (true == $config['sonata_admin']['enabled']) { $loader->load('sonata-admin.xml'); $container->setParameter('difane.bundle.twigdatabase.admin.template.group', $config['sonata_admin']['group']); $container->setParameter('difane.bundle.twigdatabase.admin.template.label', $config['sonata_admin']['label']); diff --git a/Entity/Template.php b/Entity/Template.php index 9fc2d8a..fb22701 100644 --- a/Entity/Template.php +++ b/Entity/Template.php @@ -12,7 +12,7 @@ * @UniqueEntity("name") */ class Template -{ +{ /** * @ORM\Id * @ORM\Column(type="integer") @@ -44,7 +44,7 @@ class Template * @ORM\PrePersist */ public function handlePrePersist() - { + { $this->created_at = new \DateTime; $this->updated_at = new \DateTime; } @@ -60,7 +60,7 @@ public function handlePreUpdate() /** * Get id * - * @return string + * @return string */ public function getId() { @@ -70,20 +70,20 @@ public function getId() /** * Set name * - * @param string $name + * @param string $name * @return Template */ public function setName($name) { $this->name = $name; - + return $this; } /** * Get name * - * @return string + * @return string */ public function getName() { @@ -93,20 +93,20 @@ public function getName() /** * Set content * - * @param string $content + * @param string $content * @return Template */ public function setContent($content) { $this->content = $content; - + return $this; } /** * Get content * - * @return string + * @return string */ public function getContent() { @@ -116,20 +116,20 @@ public function getContent() /** * Set created_at * - * @param \DateTime $createdAt + * @param \DateTime $createdAt * @return Template */ public function setCreatedAt($createdAt) { $this->created_at = $createdAt; - + return $this; } /** * Get created_at * - * @return \DateTime + * @return \DateTime */ public function getCreatedAt() { @@ -139,20 +139,20 @@ public function getCreatedAt() /** * Set updated_at * - * @param \DateTime $updatedAt + * @param \DateTime $updatedAt * @return Template */ public function setUpdatedAt($updatedAt) { $this->updated_at = $updatedAt; - + return $this; } /** * Get updated_at * - * @return \DateTime + * @return \DateTime */ public function getUpdatedAt() { @@ -167,4 +167,4 @@ public function __toString() return $this->getName(); } -} \ No newline at end of file +} diff --git a/Repository/TemplateRepository.php b/Repository/TemplateRepository.php index e229aaf..a3f6e00 100644 --- a/Repository/TemplateRepository.php +++ b/Repository/TemplateRepository.php @@ -1,51 +1,42 @@ -getEntityManager()->createQuery(' - SELECT t FROM DifaneTwigDatabaseBundle:Template t - WHERE t.name = :name - ') - ->setParameter('name', $name) - ->getSingleResult(); - } - catch (\Doctrine\ORM\NoResultException $e) - { - return null; - } - } - - public function getTemplateTimestamp($name) - { - try - { - $result = $this->getEntityManager()->createQuery(' - SELECT t.updated_at FROM DifaneTwigDatabaseBundle:Template t - WHERE t.name = :name - ') - ->setParameter('name', $name) - ->getSingleResult(); - - if(false == is_null($result) && is_array($result) && array_key_exists("updated_at", $result)) - { - return $result["updated_at"]->getTimestamp(); - } - else - { - return null; - } - } - catch (\Doctrine\ORM\NoResultException $e) - { - return null; - } - } -} \ No newline at end of file +getEntityManager()->createQuery(' + SELECT t FROM DifaneTwigDatabaseBundle:Template t + WHERE t.name = :name + ') + ->setParameter('name', $name) + ->getSingleResult(); + } catch (\Doctrine\ORM\NoResultException $e) { + return null; + } + } + + public function getTemplateTimestamp($name) + { + try { + $result = $this->getEntityManager()->createQuery(' + SELECT t.updated_at FROM DifaneTwigDatabaseBundle:Template t + WHERE t.name = :name + ') + ->setParameter('name', $name) + ->getSingleResult(); + + if (false == is_null($result) && is_array($result) && array_key_exists("updated_at", $result)) { + return $result["updated_at"]->getTimestamp(); + } else { + return null; + } + } catch (\Doctrine\ORM\NoResultException $e) { + return null; + } + } +} diff --git a/Subscriber/TableNameSubscriber.php b/Subscriber/TableNameSubscriber.php index 451a936..11bcad4 100644 --- a/Subscriber/TableNameSubscriber.php +++ b/Subscriber/TableNameSubscriber.php @@ -1,35 +1,35 @@ -name = (string) $name; - } - - public function getSubscribedEvents() - { - return array('loadClassMetadata'); - } - - public function loadClassMetadata(LoadClassMetadataEventArgs $args) - { - $classMetadata = $args->getClassMetadata(); - - if('Difane\Bundle\TwigDatabaseBundle\Entity\Template' != $classMetadata->getName()) return; - - $classMetadata->setTableName($this->name); - foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) { - if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY) { - //$mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name']; - $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->name; - } - } - } - -} \ No newline at end of file +name = (string) $name; + } + + public function getSubscribedEvents() + { + return array('loadClassMetadata'); + } + + public function loadClassMetadata(LoadClassMetadataEventArgs $args) + { + $classMetadata = $args->getClassMetadata(); + + if('Difane\Bundle\TwigDatabaseBundle\Entity\Template' != $classMetadata->getName()) return; + + $classMetadata->setTableName($this->name); + foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) { + if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY) { + //$mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name']; + $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->name; + } + } + } + +} diff --git a/Tests/Twig/DatabaseLoaderTest.php b/Tests/Twig/DatabaseLoaderTest.php index 5bc4eb9..9fb4e48 100644 --- a/Tests/Twig/DatabaseLoaderTest.php +++ b/Tests/Twig/DatabaseLoaderTest.php @@ -1,11 +1,11 @@ -assertTrue(true); - } -} \ No newline at end of file +assertTrue(true); + } +} diff --git a/Twig/DatabaseLoader.php b/Twig/DatabaseLoader.php index ca5640f..a37a8cd 100644 --- a/Twig/DatabaseLoader.php +++ b/Twig/DatabaseLoader.php @@ -1,97 +1,85 @@ -entityManager = $entityManager; - $this->logger = $logger; - $this->autoCreateTemplates = $autoCreateTemplates; - } - - public function getSource($name) - { - $this->logger->debug("DatabaseLoader::getSource() called with parameters[name: ".$name."]"); - - $template = $this->entityManager->getRepository('DifaneTwigDatabaseBundle:Template')->getTemplate($name); - - if($template instanceof \Difane\Bundle\TwigDatabaseBundle\Entity\Template) - { - $this->logger->debug("DatabaseLoader::getSource() Template was found. Returning its content."); - return $template->getContent(); - } - else - { - $this->logger->debug("DatabaseLoader::getSource() Template was not found. Trying to create it."); - $newTemplate = $this->tryCreateTemplate($name); - if(true == is_null($newTemplate)) - { - throw new Twig_Error_Loader(sprintf('TwigDatabase: Unable to find template "%s".', $name)); - } - else - { - return $newTemplate->getContent(); - } - } - } - - public function isFresh($name, $time) - { - $this->logger->debug("DatabaseLoader::isFresh() called with parameters[name: ".$name.", time:".$time."]"); - - $templateTimestamp = $this->entityManager->getRepository('DifaneTwigDatabaseBundle:Template')->getTemplateTimestamp($name); - - if(false == is_null($templateTimestamp)) - { - $this->logger->debug("DatabaseLoader::isFresh() Template was found. Returning its fresh status"); - return ($templateTimestamp <= $time); - } - else - { - $this->logger->debug("DatabaseLoader::isFresh() Template was not found. Trying to create it."); - if(true == is_null($this->tryCreateTemplate($name))) - { - throw new Twig_Error_Loader(sprintf('TwigDatabase: Unable to find template "%s".', $name)); - } - else - { - return false; - } - } - } - - public function getCacheKey($name) - { - $this->logger->debug("DatabaseLoader::getCacheKey() called with parameters[name: ".$name."]"); - - return "twig:db:".$name; - } - - private function tryCreateTemplate($name) - { - if( false == $this->autoCreateTemplates ) return null; - - try - { - $template = new \Difane\Bundle\TwigDatabaseBundle\Entity\Template(); - $template->setName($name); - $template->setContent("{% for i in range(0, 3) %} Lorem ipsum dolor sit amet. {% endfor %}"); - $this->entityManager->persist($template); - $this->entityManager->flush(); - return $template; - } - catch(\Exception $ex) - { - return null; - } - } -} \ No newline at end of file +entityManager = $entityManager; + $this->logger = $logger; + $this->autoCreateTemplates = $autoCreateTemplates; + } + + public function getSource($name) + { + $this->logger->debug("DatabaseLoader::getSource() called with parameters[name: ".$name."]"); + + $template = $this->entityManager->getRepository('DifaneTwigDatabaseBundle:Template')->getTemplate($name); + + if ($template instanceof \Difane\Bundle\TwigDatabaseBundle\Entity\Template) { + $this->logger->debug("DatabaseLoader::getSource() Template was found. Returning its content."); + + return $template->getContent(); + } else { + $this->logger->debug("DatabaseLoader::getSource() Template was not found. Trying to create it."); + $newTemplate = $this->tryCreateTemplate($name); + if (true == is_null($newTemplate)) { + throw new Twig_Error_Loader(sprintf('TwigDatabase: Unable to find template "%s".', $name)); + } else { + return $newTemplate->getContent(); + } + } + } + + public function isFresh($name, $time) + { + $this->logger->debug("DatabaseLoader::isFresh() called with parameters[name: ".$name.", time:".$time."]"); + + $templateTimestamp = $this->entityManager->getRepository('DifaneTwigDatabaseBundle:Template')->getTemplateTimestamp($name); + + if (false == is_null($templateTimestamp)) { + $this->logger->debug("DatabaseLoader::isFresh() Template was found. Returning its fresh status"); + + return ($templateTimestamp <= $time); + } else { + $this->logger->debug("DatabaseLoader::isFresh() Template was not found. Trying to create it."); + if (true == is_null($this->tryCreateTemplate($name))) { + throw new Twig_Error_Loader(sprintf('TwigDatabase: Unable to find template "%s".', $name)); + } else { + return false; + } + } + } + + public function getCacheKey($name) + { + $this->logger->debug("DatabaseLoader::getCacheKey() called with parameters[name: ".$name."]"); + + return "twig:db:".$name; + } + + private function tryCreateTemplate($name) + { + if( false == $this->autoCreateTemplates ) return null; + + try { + $template = new \Difane\Bundle\TwigDatabaseBundle\Entity\Template(); + $template->setName($name); + $template->setContent("{% for i in range(0, 3) %} Lorem ipsum dolor sit amet. {% endfor %}"); + $this->entityManager->persist($template); + $this->entityManager->flush(); + + return $template; + } catch (\Exception $ex) { + return null; + } + } +}