aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Subscriber
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2016-10-30 09:58:39 +0100
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-10-30 09:58:39 +0100
commit535bfcbe80de5d697b768c3a657214fdeff0eac3 (patch)
treeab10a21bc5358344e29ee54c17a5a404d6cd7ada /src/Wallabag/CoreBundle/Subscriber
parent156bf62758080153668a65db611c4241d0fc8a00 (diff)
downloadwallabag-535bfcbe80de5d697b768c3a657214fdeff0eac3.tar.gz
wallabag-535bfcbe80de5d697b768c3a657214fdeff0eac3.tar.zst
wallabag-535bfcbe80de5d697b768c3a657214fdeff0eac3.zip
Move related event things in Event folder
Diffstat (limited to 'src/Wallabag/CoreBundle/Subscriber')
-rw-r--r--src/Wallabag/CoreBundle/Subscriber/SQLiteCascadeDeleteSubscriber.php70
-rw-r--r--src/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriber.php51
2 files changed, 0 insertions, 121 deletions
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 @@
1<?php
2
3namespace Wallabag\CoreBundle\Subscriber;
4
5use Doctrine\Common\EventSubscriber;
6use Doctrine\ORM\Event\LifecycleEventArgs;
7use Wallabag\CoreBundle\Entity\Entry;
8use Doctrine\Bundle\DoctrineBundle\Registry;
9
10/**
11 * SQLite doesn't care about cascading remove, so we need to manually remove associated stuf for an Entry.
12 * Foreign Key Support can be enabled by running `PRAGMA foreign_keys = ON;` at runtime (AT RUNTIME !).
13 * But it needs a compilation flag that not all SQLite instance has ...
14 *
15 * @see https://www.sqlite.org/foreignkeys.html#fk_enable
16 */
17class SQLiteCascadeDeleteSubscriber implements EventSubscriber
18{
19 private $doctrine;
20
21 /**
22 * @param \Doctrine\Bundle\DoctrineBundle\Registry $doctrine
23 */
24 public function __construct(Registry $doctrine)
25 {
26 $this->doctrine = $doctrine;
27 }
28
29 /**
30 * @return array
31 */
32 public function getSubscribedEvents()
33 {
34 return [
35 'preRemove',
36 ];
37 }
38
39 /**
40 * We removed everything related to the upcoming removed entry because SQLite can't handle it on it own.
41 * We do it in the preRemove, because we can't retrieve tags in the postRemove (because the entry id is gone).
42 *
43 * @param LifecycleEventArgs $args
44 */
45 public function preRemove(LifecycleEventArgs $args)
46 {
47 $entity = $args->getEntity();
48
49 if (!$this->doctrine->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver ||
50 !$entity instanceof Entry) {
51 return;
52 }
53
54 $em = $this->doctrine->getManager();
55
56 if (null !== $entity->getTags()) {
57 foreach ($entity->getTags() as $tag) {
58 $entity->removeTag($tag);
59 }
60 }
61
62 if (null !== $entity->getAnnotations()) {
63 foreach ($entity->getAnnotations() as $annotation) {
64 $em->remove($annotation);
65 }
66 }
67
68 $em->flush();
69 }
70}
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 @@
1<?php
2
3namespace Wallabag\CoreBundle\Subscriber;
4
5use Doctrine\Common\EventSubscriber;
6use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
7use Doctrine\ORM\Mapping\ClassMetadataInfo;
8
9/**
10 * Puts a prefix to each table.
11 * This way were used instead of using the built-in strategy from Doctrine, using `naming_strategy`
12 * Because it conflicts with the DefaultQuoteStrategy (that espace table name, like user for Postgres)
13 * see #1498 for more detail.
14 *
15 * Solution from :
16 * - http://stackoverflow.com/a/23860613/569101
17 * - http://doctrine-orm.readthedocs.org/en/latest/reference/namingstrategy.html
18 */
19class TablePrefixSubscriber implements EventSubscriber
20{
21 protected $prefix = '';
22
23 public function __construct($prefix)
24 {
25 $this->prefix = (string) $prefix;
26 }
27
28 public function getSubscribedEvents()
29 {
30 return ['loadClassMetadata'];
31 }
32
33 public function loadClassMetadata(LoadClassMetadataEventArgs $args)
34 {
35 $classMetadata = $args->getClassMetadata();
36
37 // if we are in an inheritance hierarchy, only apply this once
38 if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
39 return;
40 }
41
42 $classMetadata->setTableName($this->prefix.$classMetadata->getTableName());
43
44 foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
45 if ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY && isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])) {
46 $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
47 $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix.$mappedTableName;
48 }
49 }
50 }
51}