From 535bfcbe80de5d697b768c3a657214fdeff0eac3 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 30 Oct 2016 09:58:39 +0100 Subject: Move related event things in Event folder --- .../CoreBundle/Event/Listener/LocaleListener.php | 44 ++++++++++++++ .../Event/Listener/UserLocaleListener.php | 37 ++++++++++++ .../Subscriber/SQLiteCascadeDeleteSubscriber.php | 70 ++++++++++++++++++++++ .../Event/Subscriber/TablePrefixSubscriber.php | 51 ++++++++++++++++ .../CoreBundle/EventListener/LocaleListener.php | 44 -------------- .../EventListener/UserLocaleListener.php | 37 ------------ .../CoreBundle/Resources/config/services.yml | 4 +- .../Subscriber/SQLiteCascadeDeleteSubscriber.php | 70 ---------------------- .../Subscriber/TablePrefixSubscriber.php | 51 ---------------- 9 files changed, 204 insertions(+), 204 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php create mode 100644 src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php create mode 100644 src/Wallabag/CoreBundle/Event/Subscriber/SQLiteCascadeDeleteSubscriber.php create mode 100644 src/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriber.php delete mode 100644 src/Wallabag/CoreBundle/EventListener/LocaleListener.php delete mode 100644 src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php delete mode 100644 src/Wallabag/CoreBundle/Subscriber/SQLiteCascadeDeleteSubscriber.php delete mode 100644 src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php (limited to 'src') diff --git a/src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php b/src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php new file mode 100644 index 00000000..b435d99e --- /dev/null +++ b/src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php @@ -0,0 +1,44 @@ +defaultLocale = $defaultLocale; + } + + public function onKernelRequest(GetResponseEvent $event) + { + $request = $event->getRequest(); + if (!$request->hasPreviousSession()) { + return; + } + + // try to see if the locale has been set as a _locale routing parameter + if ($locale = $request->attributes->get('_locale')) { + $request->getSession()->set('_locale', $locale); + } else { + // if no explicit locale has been set on this request, use one from the session + $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale)); + } + } + + public static function getSubscribedEvents() + { + return [ + // must be registered before the default Locale listener + KernelEvents::REQUEST => [['onKernelRequest', 17]], + ]; + } +} diff --git a/src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php b/src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php new file mode 100644 index 00000000..367cdfb0 --- /dev/null +++ b/src/Wallabag/CoreBundle/Event/Listener/UserLocaleListener.php @@ -0,0 +1,37 @@ +session = $session; + } + + /** + * @param InteractiveLoginEvent $event + */ + public function onInteractiveLogin(InteractiveLoginEvent $event) + { + $user = $event->getAuthenticationToken()->getUser(); + + if (null !== $user->getConfig()->getLanguage()) { + $this->session->set('_locale', $user->getConfig()->getLanguage()); + } + } +} diff --git a/src/Wallabag/CoreBundle/Event/Subscriber/SQLiteCascadeDeleteSubscriber.php b/src/Wallabag/CoreBundle/Event/Subscriber/SQLiteCascadeDeleteSubscriber.php new file mode 100644 index 00000000..3b4c4cf9 --- /dev/null +++ b/src/Wallabag/CoreBundle/Event/Subscriber/SQLiteCascadeDeleteSubscriber.php @@ -0,0 +1,70 @@ +doctrine = $doctrine; + } + + /** + * @return array + */ + public function getSubscribedEvents() + { + return [ + 'preRemove', + ]; + } + + /** + * We removed everything related to the upcoming removed entry because SQLite can't handle it on it own. + * We do it in the preRemove, because we can't retrieve tags in the postRemove (because the entry id is gone). + * + * @param LifecycleEventArgs $args + */ + public function preRemove(LifecycleEventArgs $args) + { + $entity = $args->getEntity(); + + if (!$this->doctrine->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver || + !$entity instanceof Entry) { + return; + } + + $em = $this->doctrine->getManager(); + + if (null !== $entity->getTags()) { + foreach ($entity->getTags() as $tag) { + $entity->removeTag($tag); + } + } + + if (null !== $entity->getAnnotations()) { + foreach ($entity->getAnnotations() as $annotation) { + $em->remove($annotation); + } + } + + $em->flush(); + } +} diff --git a/src/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriber.php b/src/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriber.php new file mode 100644 index 00000000..9013328f --- /dev/null +++ b/src/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriber.php @@ -0,0 +1,51 @@ +prefix = (string) $prefix; + } + + public function getSubscribedEvents() + { + return ['loadClassMetadata']; + } + + public function loadClassMetadata(LoadClassMetadataEventArgs $args) + { + $classMetadata = $args->getClassMetadata(); + + // if we are in an inheritance hierarchy, only apply this once + if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) { + return; + } + + $classMetadata->setTableName($this->prefix.$classMetadata->getTableName()); + + foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) { + if ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY && isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])) { + $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name']; + $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix.$mappedTableName; + } + } + } +} diff --git a/src/Wallabag/CoreBundle/EventListener/LocaleListener.php b/src/Wallabag/CoreBundle/EventListener/LocaleListener.php deleted file mode 100644 index a1c7e5ab..00000000 --- a/src/Wallabag/CoreBundle/EventListener/LocaleListener.php +++ /dev/null @@ -1,44 +0,0 @@ -defaultLocale = $defaultLocale; - } - - public function onKernelRequest(GetResponseEvent $event) - { - $request = $event->getRequest(); - if (!$request->hasPreviousSession()) { - return; - } - - // try to see if the locale has been set as a _locale routing parameter - if ($locale = $request->attributes->get('_locale')) { - $request->getSession()->set('_locale', $locale); - } else { - // if no explicit locale has been set on this request, use one from the session - $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale)); - } - } - - public static function getSubscribedEvents() - { - return [ - // must be registered before the default Locale listener - KernelEvents::REQUEST => [['onKernelRequest', 17]], - ]; - } -} diff --git a/src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php b/src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php deleted file mode 100644 index 82d1a63a..00000000 --- a/src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php +++ /dev/null @@ -1,37 +0,0 @@ -session = $session; - } - - /** - * @param InteractiveLoginEvent $event - */ - public function onInteractiveLogin(InteractiveLoginEvent $event) - { - $user = $event->getAuthenticationToken()->getUser(); - - if (null !== $user->getConfig()->getLanguage()) { - $this->session->set('_locale', $user->getConfig()->getLanguage()); - } - } -} diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index cc5f9e9a..4b7751fe 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml @@ -30,7 +30,7 @@ services: - "@doctrine" wallabag_core.subscriber.table_prefix: - class: Wallabag\CoreBundle\Subscriber\TablePrefixSubscriber + class: Wallabag\CoreBundle\Event\Subscriber\TablePrefixSubscriber arguments: - "%database_table_prefix%" tags: @@ -131,7 +131,7 @@ services: - '%kernel.debug%' wallabag_core.subscriber.sqlite_cascade_delete: - class: Wallabag\CoreBundle\Subscriber\SQLiteCascadeDeleteSubscriber + class: Wallabag\CoreBundle\Event\Subscriber\SQLiteCascadeDeleteSubscriber arguments: - "@doctrine" tags: diff --git a/src/Wallabag/CoreBundle/Subscriber/SQLiteCascadeDeleteSubscriber.php b/src/Wallabag/CoreBundle/Subscriber/SQLiteCascadeDeleteSubscriber.php deleted file mode 100644 index f7210bd3..00000000 --- a/src/Wallabag/CoreBundle/Subscriber/SQLiteCascadeDeleteSubscriber.php +++ /dev/null @@ -1,70 +0,0 @@ -doctrine = $doctrine; - } - - /** - * @return array - */ - public function getSubscribedEvents() - { - return [ - 'preRemove', - ]; - } - - /** - * We removed everything related to the upcoming removed entry because SQLite can't handle it on it own. - * We do it in the preRemove, because we can't retrieve tags in the postRemove (because the entry id is gone). - * - * @param LifecycleEventArgs $args - */ - public function preRemove(LifecycleEventArgs $args) - { - $entity = $args->getEntity(); - - if (!$this->doctrine->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver || - !$entity instanceof Entry) { - return; - } - - $em = $this->doctrine->getManager(); - - if (null !== $entity->getTags()) { - foreach ($entity->getTags() as $tag) { - $entity->removeTag($tag); - } - } - - if (null !== $entity->getAnnotations()) { - foreach ($entity->getAnnotations() as $annotation) { - $em->remove($annotation); - } - } - - $em->flush(); - } -} diff --git a/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php b/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php deleted file mode 100644 index 0379ad6a..00000000 --- a/src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php +++ /dev/null @@ -1,51 +0,0 @@ -prefix = (string) $prefix; - } - - public function getSubscribedEvents() - { - return ['loadClassMetadata']; - } - - public function loadClassMetadata(LoadClassMetadataEventArgs $args) - { - $classMetadata = $args->getClassMetadata(); - - // if we are in an inheritance hierarchy, only apply this once - if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) { - return; - } - - $classMetadata->setTableName($this->prefix.$classMetadata->getTableName()); - - foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) { - if ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY && isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])) { - $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name']; - $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix.$mappedTableName; - } - } - } -} -- cgit v1.2.3