|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Lookyman\NetteOAuth2Server\Storage\Doctrine; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
|
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
|
8
|
|
|
use Kdyby\Doctrine\Events; |
|
9
|
|
|
use Kdyby\Doctrine\Mapping\ClassMetadata; |
|
10
|
|
|
use Kdyby\Events\Subscriber; |
|
11
|
|
|
use Lookyman\NetteOAuth2Server\Storage\Doctrine\AccessToken\AccessTokenEntity; |
|
12
|
|
|
use Lookyman\NetteOAuth2Server\Storage\Doctrine\AuthCode\AuthCodeEntity; |
|
13
|
|
|
use Lookyman\NetteOAuth2Server\Storage\Doctrine\Client\ClientEntity; |
|
14
|
|
|
use Lookyman\NetteOAuth2Server\Storage\Doctrine\RefreshToken\RefreshTokenEntity; |
|
15
|
|
|
use Lookyman\NetteOAuth2Server\Storage\Doctrine\Scope\ScopeEntity; |
|
16
|
|
|
|
|
17
|
|
|
class TablePrefixSubscriber implements Subscriber |
|
18
|
|
|
{ |
|
19
|
|
|
const DEFAULT_PREFIX = 'nette_oauth2_server_'; |
|
20
|
|
|
|
|
21
|
|
|
const ENTITIES = [ |
|
22
|
|
|
AccessTokenEntity::class, |
|
23
|
|
|
AuthCodeEntity::class, |
|
24
|
|
|
ClientEntity::class, |
|
25
|
|
|
RefreshTokenEntity::class, |
|
26
|
|
|
ScopeEntity::class, |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $prefix; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $prefix |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(string $prefix = self::DEFAULT_PREFIX) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->prefix = $prefix; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param LoadClassMetadataEventArgs $eventArgs |
|
44
|
|
|
*/ |
|
45
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) |
|
46
|
|
|
{ |
|
47
|
|
|
/** @var ClassMetadata $metadata */ |
|
48
|
|
|
$metadata = $eventArgs->getClassMetadata(); |
|
49
|
|
|
if (in_array($metadata->getName(), self::ENTITIES)) { |
|
50
|
|
|
$metadata->setPrimaryTable([ |
|
51
|
|
|
'name' => self::getPrefixedName($this->prefix, $metadata->getTableName()), |
|
52
|
|
|
]); |
|
53
|
|
|
} |
|
54
|
|
|
foreach ($metadata->getAssociationMappings() as $name => $mapping) { |
|
55
|
|
|
if ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY |
|
56
|
|
|
&& $mapping['isOwningSide'] |
|
57
|
|
|
&& in_array($mapping['targetEntity'], self::ENTITIES) |
|
58
|
|
|
) { |
|
59
|
|
|
$metadata->associationMappings[$name]['joinTable']['name'] = self::getPrefixedName($this->prefix, $mapping['joinTable']['name']); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getSubscribedEvents() |
|
68
|
|
|
{ |
|
69
|
|
|
return [Events::loadClassMetadata]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $prefix |
|
74
|
|
|
* @param string $name |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected static function getPrefixedName(string $prefix, string $name): string |
|
78
|
|
|
{ |
|
79
|
|
|
return $prefix . $name; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|