1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\DoctrinePHPCRAdminBundle\Description; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
17
|
|
|
use PHPCR\PathNotFoundException; |
18
|
|
|
use PHPCR\Util\PathHelper; |
19
|
|
|
use Symfony\Cmf\Component\Resource\Description\Description; |
20
|
|
|
use Symfony\Cmf\Component\Resource\Description\DescriptionEnhancerInterface; |
21
|
|
|
use Symfony\Cmf\Component\Resource\Puli\Api\PuliResource; |
22
|
|
|
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* A description enhancer to add the position/sorting value of children on a parent. |
26
|
|
|
* |
27
|
|
|
* @author Maximilian Berghoff <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class PositionEnhancer implements DescriptionEnhancerInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var \PHPCR\SessionInterface |
33
|
|
|
*/ |
34
|
|
|
private $session; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $sessionName |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ManagerRegistry $manager, $sessionName) |
40
|
|
|
{ |
41
|
|
|
$this->session = $manager->getConnection($sessionName); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function enhance(Description $description) |
48
|
|
|
{ |
49
|
|
|
$nodePath = $description->getResource()->getPath(); |
50
|
|
|
$nodeName = PathHelper::getNodeName($nodePath); |
51
|
|
|
$parentPath = PathHelper::getParentPath($nodePath); |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
$parentNode = $this->session->getNode($parentPath); |
55
|
|
|
} catch (PathNotFoundException $exception) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$nodeIterator = $parentNode->getNodes(); |
60
|
|
|
$nodeIterator->rewind(); |
61
|
|
|
$counter = 0; |
62
|
|
|
while ($nodeIterator->valid()) { |
63
|
|
|
++$counter; |
64
|
|
|
if ($nodeIterator->key() === $nodeName) { |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
$nodeIterator->next(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$description->set('position', $counter); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function supports(PuliResource $resource) |
77
|
|
|
{ |
78
|
|
|
if (!$resource instanceof CmfResource) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
try { |
83
|
|
|
$parentNode = $this->session->getNode(PathHelper::getParentPath($resource->getPath())); |
|
|
|
|
84
|
|
|
} catch (PathNotFoundException $exception) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Todo: check for non orderable type |
89
|
|
|
|
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.