1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Sulu. |
5
|
|
|
* |
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sulu\Component\DocumentManager\Subscriber\Behavior\Path; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Inflector\Inflector; |
15
|
|
|
use Sulu\Component\DocumentManager\Behavior\Path\AliasFilingBehavior; |
16
|
|
|
use Sulu\Component\DocumentManager\Event\PersistEvent; |
17
|
|
|
use Sulu\Component\DocumentManager\Events; |
18
|
|
|
use Sulu\Component\DocumentManager\MetadataFactoryInterface; |
19
|
|
|
use Sulu\Component\DocumentManager\NodeManager; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Automatically set the parent at a pre-determined location. |
23
|
|
|
*/ |
24
|
|
|
class AliasFilingSubscriber extends AbstractFilingSubscriber |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var MetadataFactoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $metadataFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param NodeManager $nodeManager |
33
|
|
|
* @param MetadataFactoryInterface $metadataFactory |
34
|
|
|
*/ |
35
|
|
|
public function __construct( |
36
|
|
|
NodeManager $nodeManager, |
37
|
|
|
MetadataFactoryInterface $metadataFactory |
38
|
|
|
) { |
39
|
|
|
parent::__construct($nodeManager); |
40
|
|
|
$this->metadataFactory = $metadataFactory; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public static function getSubscribedEvents() |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
Events::PERSIST => ['handlePersist', 490], |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected function generatePath(PersistEvent $event) |
57
|
|
|
{ |
58
|
|
|
$document = $event->getDocument(); |
59
|
|
|
|
60
|
|
|
$currentPath = ''; |
61
|
|
|
if ($event->hasParentNode()) { |
62
|
|
|
$currentPath = $event->getParentNode()->getPath(); |
63
|
|
|
} |
64
|
|
|
$parentName = $this->getParentName($document); |
65
|
|
|
|
66
|
|
|
return sprintf('%s/%s', $currentPath, Inflector::pluralize($parentName)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param object $document |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
protected function supports($document) |
75
|
|
|
{ |
76
|
|
|
return $document instanceof AliasFilingBehavior; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $document |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
protected function getParentName($document) |
85
|
|
|
{ |
86
|
|
|
return $this->metadataFactory->getMetadataForClass(get_class($document))->getAlias(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|