Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 5c117f1

Browse files
committed
Use ChildInterface
1 parent f261302 commit 5c117f1

File tree

10 files changed

+60
-52
lines changed

10 files changed

+60
-52
lines changed

Admin/AbstractMenuNodeAdmin.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,6 @@ protected function configureShowField(ShowMapper $showMapper)
9595
;
9696
}
9797

98-
/**
99-
* @return MenuNode
100-
*/
101-
public function getNewInstance()
102-
{
103-
/** @var $new MenuNode */
104-
$new = parent::getNewInstance();
105-
106-
if ($this->hasRequest()) {
107-
108-
// Set the parent
109-
$parentId = $this->getRequest()->query->get('parent');
110-
111-
if (null !== $parentId) {
112-
$new->setParent($this->getModelManager()->find(null, $parentId));
113-
}
114-
}
115-
116-
return $new;
117-
}
118-
11998
public function getExportFormats()
12099
{
121100
return array();

Admin/MenuAdmin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function getNewInstance()
4848
{
4949
/** @var $new Menu */
5050
$new = parent::getNewInstance();
51-
$new->setParent($this->getModelManager()->find(null, $this->menuRoot));
51+
$new->setParentDocument($this->getModelManager()->find(null, $this->menuRoot));
5252

5353
return $new;
5454
}

Admin/MenuNodeAdmin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function buildBreadcrumbs($action, MenuItemInterface $menu = null)
5151
return $menuNodeNode;
5252
}
5353

54-
$parentDoc = $this->getSubject()->getParent();
54+
$parentDoc = $this->getSubject()->getParentDocument();
5555
$pool = $this->getConfigurationPool();
5656
$parentAdmin = $pool->getAdminByClass(
5757
ClassUtils::getClass($parentDoc)

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
=========
33

4+
* **2014-03-24**: setParent() and getParent() are now deprecated.
5+
Use setParentDocument() and getParentDocument() instead.
6+
Moreover, you should now enable the ChildExtension from the CoreBundle.
7+
48
* **2014-01-10**: The PhpcrMenuProvider now attempts to prefetch the whole menu
59
node tree to reduce the number of requests to the PHPCR storage. You can
610
tweak the behaviour with the configuration setting

Model/MenuNodeBase.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Symfony\Cmf\Bundle\MenuBundle\Model;
1414

1515
use Knp\Menu\NodeInterface;
16+
use Symfony\Cmf\Bundle\CoreBundle\Model\ChildInterface;
1617

1718
/**
1819
* This is a persistable implementation of the KnpMenu
@@ -21,7 +22,7 @@
2122
* @author Uwe Jäger <[email protected]>
2223
* @author Daniel Leech <[email protected]>
2324
*/
24-
class MenuNodeBase implements NodeInterface
25+
class MenuNodeBase implements NodeInterface, ChildInterface
2526
{
2627
/**
2728
* Id of this menu node.
@@ -176,14 +177,32 @@ public function setId($id)
176177
return $this;
177178
}
178179

180+
/**
181+
* @deprecated Use setParentDocument instead.
182+
*/
183+
public function setParent($parent)
184+
{
185+
$this->parent = $parent;
186+
187+
return $this;
188+
}
189+
190+
/**
191+
* @deprecated Use getParentDocument instead.
192+
*/
193+
public function getParent()
194+
{
195+
return $this->parent;
196+
}
197+
179198
/**
180199
* Set the parent of this menu node
181200
*
182201
* @param $parent MenuNode - Parent node
183202
*
184203
* @return MenuNode - this instance
185204
*/
186-
public function setParent($parent)
205+
public function setParentDocument($parent)
187206
{
188207
$this->parent = $parent;
189208

@@ -195,7 +214,7 @@ public function setParent($parent)
195214
*
196215
* @return object
197216
*/
198-
public function getParent()
217+
public function getParentDocument()
199218
{
200219
return $this->parent;
201220
}
@@ -418,7 +437,7 @@ public function getChildren()
418437
*/
419438
public function addChild(MenuNode $child)
420439
{
421-
$child->setParent($this);
440+
$child->setParentDocument($this);
422441
$this->children[] = $child;
423442

424443
return $child;

Tests/Functional/Doctrine/Phpcr/MenuNodeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function setUp()
2525
$this->baseNode = $this->dm->find(null, '/test');
2626

2727
$this->content = new Content;
28-
$this->content->setParent($this->baseNode);
28+
$this->content->setParentDocument($this->baseNode);
2929
$this->content->setTitle('fake_weak_content');
3030
$this->content->setName('fake_weak_content');
3131
$this->dm->persist($this->content);
@@ -81,7 +81,7 @@ public function testMenuNode()
8181
$menuNode = new MenuNode;
8282
$refl = new \ReflectionClass($menuNode);
8383

84-
$menuNode->setParent($this->baseNode);
84+
$menuNode->setParentDocument($this->baseNode);
8585

8686
foreach ($data as $key => $value) {
8787
$refl = new \ReflectionClass($menuNode);

Tests/Resources/DataFixtures/PHPCR/LoadMenuData.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ protected function loadMainMenu(DocumentManager $manager)
6161
$menu = new Menu;
6262
$menu->setName('test-menu');
6363
$menu->setLabel('Test Menu');
64-
$menu->setParent($this->menuRoot);
64+
$menu->setParentDocument($this->menuRoot);
6565
$manager->persist($menu);
6666

6767
$menuNode = new MenuNode;
68-
$menuNode->setParent($menu);
68+
$menuNode->setParentDocument($menu);
6969
$menuNode->setLabel('item-1');
7070
$menuNode->setName('item-1');
7171
$manager->persist($menuNode);
7272

7373
$content->addMenuNode($menuNode);
7474

7575
$menuNode = new MenuNode;
76-
$menuNode->setParent($menu);
76+
$menuNode->setParentDocument($menu);
7777
$menuNode->setLabel('This node has a URI');
7878
$menuNode->setUri('http://www.example.com');
7979
$menuNode->setName('item-2');
@@ -82,40 +82,40 @@ protected function loadMainMenu(DocumentManager $manager)
8282
$content->addMenuNode($menuNode);
8383

8484
$subNode = new MenuNode;
85-
$subNode->setParent($menuNode);
85+
$subNode->setParentDocument($menuNode);
8686
$subNode->setLabel('@todo this node should have content');
8787
$subNode->setName('sub-item-1');
8888
$manager->persist($subNode);
8989

9090
$subNode = new MenuNode;
91-
$subNode->setParent($menuNode);
91+
$subNode->setParentDocument($menuNode);
9292
$subNode->setLabel('This node has an assigned route');
9393
$subNode->setName('sub-item-2');
9494
$subNode->setRoute('link_test_route');
9595
$manager->persist($subNode);
9696

9797
$subNode = new MenuNode;
98-
$subNode->setParent($menuNode);
98+
$subNode->setParentDocument($menuNode);
9999
$subNode->setLabel('This node has an assigned route with parameters');
100100
$subNode->setName('sub-item-3');
101101
$subNode->setRoute('link_test_route_with_params');
102102
$subNode->setRouteParameters(array('foo' => 'bar', 'bar' => 'foo'));
103103
$manager->persist($subNode);
104104

105105
$menuNode = new MenuNode;
106-
$menuNode->setParent($menu);
106+
$menuNode->setParentDocument($menu);
107107
$menuNode->setLabel('item-3');
108108
$menuNode->setName('item-3');
109109
$manager->persist($menuNode);
110110

111111
$menu = new Menu;
112112
$menu->setName('another-menu');
113113
$menu->setLabel('Another Menu');
114-
$menu->setParent($this->menuRoot);
114+
$menu->setParentDocument($this->menuRoot);
115115
$manager->persist($menu);
116116

117117
$menuNode = new MenuNode;
118-
$menuNode->setParent($menu);
118+
$menuNode->setParentDocument($menu);
119119
$menuNode->setLabel('This node has uri, route and content set. but linkType is set to route');
120120
$menuNode->setLinkType('route');
121121
$menuNode->setUri('http://www.example.com');
@@ -124,7 +124,7 @@ protected function loadMainMenu(DocumentManager $manager)
124124
$manager->persist($menuNode);
125125

126126
$menuNode = new MenuNode;
127-
$menuNode->setParent($menu);
127+
$menuNode->setParentDocument($menu);
128128
$menuNode->setLabel('item-2');
129129
$menuNode->setName('item-2');
130130
$manager->persist($menuNode);
@@ -202,32 +202,32 @@ protected function loadVoterMenu(DocumentManager $manager)
202202
$menu = new Menu;
203203
$menu->setName('side-menu');
204204
$menu->setLabel('Side Menu');
205-
$menu->setParent($this->menuRoot);
205+
$menu->setParentDocument($this->menuRoot);
206206
$manager->persist($menu);
207207

208208
$menuNode = new MenuNode;
209-
$menuNode->setParent($menu);
209+
$menuNode->setParentDocument($menu);
210210
$menuNode->setLabel('Default Behavior');
211211
$menuNode->setName('default');
212212
$menuNode->setRoute('current_menu_item_default');
213213
$manager->persist($menuNode);
214214

215215
$menuNode = new MenuNode;
216-
$menuNode->setParent($menu);
216+
$menuNode->setParentDocument($menu);
217217
$menuNode->setLabel('Request Content Identity Voter');
218218
$menuNode->setName('request-content-identity-voter');
219219
$menuNode->setContent($content);
220220
$manager->persist($menuNode);
221221

222222
$menuNode = new MenuNode;
223-
$menuNode->setParent($menu);
223+
$menuNode->setParentDocument($menu);
224224
$menuNode->setLabel('URI Prefix Voter');
225225
$menuNode->setName('uri-prefix-voter');
226226
$menuNode->setContent($articlesRoute);
227227
$manager->persist($menuNode);
228228

229229
$menuNode = new MenuNode;
230-
$menuNode->setParent($menu);
230+
$menuNode->setParentDocument($menu);
231231
$menuNode->setLabel('Request Parent Content Identity Voter');
232232
$menuNode->setName('request-parent-content-identity-voter');
233233
$menuNode->setContent($blog);

Tests/Resources/Document/Content.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717

1818
use Knp\Menu\NodeInterface;
1919

20+
use Symfony\Cmf\Bundle\CoreBundle\Model\ChildInterface;
2021
use Symfony\Component\Routing\Route;
2122
use Symfony\Cmf\Component\Routing\RouteReferrersReadInterface;
2223
use Symfony\Cmf\Bundle\MenuBundle\Model\MenuNodeReferrersInterface;
2324

2425
/**
2526
* @PHPCRODM\Document(referenceable=true)
2627
*/
27-
class Content implements MenuNodeReferrersInterface, RouteReferrersReadInterface
28+
class Content implements MenuNodeReferrersInterface, RouteReferrersReadInterface, ChildInterface
2829
{
2930
/**
3031
* @PHPCRODM\Id(strategy="assigned")
@@ -112,7 +113,12 @@ public function getRoutes()
112113
return $this->routes;
113114
}
114115

115-
public function getParent()
116+
public function setParentDocument($parent)
117+
{
118+
$this->parent = $parent;
119+
}
120+
121+
public function getParentDocument()
116122
{
117123
return $this->parent;
118124
}

Tests/Unit/Model/MenuNodeTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function setUp()
2626
$this->parentNode = new MenuNode;
2727
$this->node = new MenuNode;
2828
$this->node->setId('/foo/bar')
29-
->setParent($this->parentNode)
29+
->setParentDocument($this->parentNode)
3030
->setName('test')
3131
->setLabel('Test')
3232
->setUri('http://www.example.com')
@@ -45,7 +45,7 @@ public function setUp()
4545

4646
public function testGetters()
4747
{
48-
$this->assertSame($this->parentNode, $this->node->getParent());
48+
$this->assertSame($this->parentNode, $this->node->getParentDocument());
4949
$this->assertEquals('test', $this->node->getName());
5050
$this->assertEquals('Test', $this->node->getLabel());
5151
$this->assertEquals('http://www.example.com', $this->node->getUri());
@@ -58,7 +58,7 @@ public function testGetters()
5858

5959
$this->parentNode = new MenuNode;
6060
$this->node->setPosition($this->parentNode, 'FOOO');
61-
$this->assertSame($this->parentNode, $this->node->getParent());
61+
$this->assertSame($this->parentNode, $this->node->getParentDocument());
6262
$this->assertEquals('FOOO', $this->node->getName());
6363
$this->assertEquals(array('link' => 'knil'), $this->node->getLinkAttributes());
6464
$this->assertEquals(array('label' => 'lebal'), $this->node->getLabelAttributes());
@@ -78,7 +78,7 @@ public function testAddChild()
7878

7979
$children = $m->getChildren();
8080
$this->assertCount(2, $children);
81-
$this->assertSame($m, $children[0]->getParent());
81+
$this->assertSame($m, $children[0]->getParentDocument());
8282
$this->assertSame($c2, $ret);
8383
}
8484

Voter/RequestParentContentIdentityVoter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* A variant of the RequestContentIdentityVoter that checks if the request
2020
* content is of a specific class and if so checks if the value returned by
21-
* *getParent()* is identical to the content item in the menu items extras.
21+
* *getParentDocument()* is identical to the content item in the menu items extras.
2222
*
2323
* Note that there is no check, you have to make sure the $childClass does
2424
* indeed have a getParent method.
@@ -74,7 +74,7 @@ public function matchItem(ItemInterface $item = null)
7474
if (null !== $content
7575
&& $this->request->attributes->has($this->requestKey)
7676
&& $this->request->attributes->get($this->requestKey) instanceof $this->childClass
77-
&& $this->request->attributes->get($this->requestKey)->getParent() === $content
77+
&& $this->request->attributes->get($this->requestKey)->getParentDocument() === $content
7878
) {
7979
return true;
8080
}

0 commit comments

Comments
 (0)