Skip to content

Commit a828d3d

Browse files
committed
Merge pull request #138 from symfony-cmf/cleanup-childinterface
make child interface neutral, support hierarchyinterface as well
2 parents 5bd9d9d + 2958ab3 commit a828d3d

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

Admin/Extension/ChildExtension.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Sonata\AdminBundle\Admin\AdminExtension;
1515
use Sonata\AdminBundle\Admin\AdminInterface;
1616
use Symfony\Cmf\Bundle\CoreBundle\Model\ChildInterface;
17+
use Doctrine\ODM\PHPCR\HierarchyInterface;
1718

1819
/**
1920
* Admin extension to handle child models.
@@ -29,14 +30,26 @@ class ChildExtension extends AdminExtension
2930
*/
3031
public function alterNewInstance(AdminInterface $admin, $object)
3132
{
32-
if (!$object instanceof ChildInterface) {
33-
throw new \InvalidArgumentException('Expected ChildInterface, got ' . get_class($object));
33+
if (!$admin->hasRequest()
34+
|| !$parentId = $admin->getRequest()->get('parent')
35+
) {
36+
return;
3437
}
3538

36-
if ($admin->hasRequest() && $parentId = $admin->getRequest()->get('parent')) {
37-
if ($parent = $admin->getModelManager()->find(null, $parentId)) {
39+
$parent = $admin->getModelManager()->find(null, $parentId);
40+
if (!$parent) {
41+
return;
42+
}
43+
44+
switch($object) {
45+
case $object instanceof HierarchyInterface:
3846
$object->setParentDocument($parent);
39-
}
47+
break;
48+
case $object instanceof ChildInterface:
49+
$object->setParentObject($parent);
50+
break;
51+
default:
52+
throw new \InvalidArgumentException(sprintf('Class %s is not supported', get_class($object)));
4053
}
4154
}
4255
}

Model/ChildInterface.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@
1212
namespace Symfony\Cmf\Bundle\CoreBundle\Model;
1313

1414
/**
15-
* An interface for models with a parent document.
15+
* An interface for models with a parent object.
16+
*
17+
* Note that PHPCR-ODM documents will most likely use the HierarchyInterface
18+
* of PHPCR-ODM instead.
1619
*/
1720
interface ChildInterface
1821
{
1922
/**
2023
* @param $parent object
2124
*/
22-
public function setParentDocument($parent);
25+
public function setParentObject($parent);
2326

2427
/**
2528
* @return object
2629
*/
27-
public function getParentDocument();
30+
public function getParentObject();
2831
}

Tests/Unit/Admin/Extension/ChildExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testAlterNewInstance()
5050

5151
$child = $this->getMock('Symfony\Cmf\Bundle\CoreBundle\Model\ChildInterface');
5252
$child->expects($this->once())
53-
->method('setParentDocument')
53+
->method('setParentObject')
5454
->with($this->equalTo($parent));
5555

5656
$extension = new ChildExtension();

0 commit comments

Comments
 (0)