Skip to content

Commit

Permalink
Merge pull request #138 from symfony-cmf/cleanup-childinterface
Browse files Browse the repository at this point in the history
make child interface neutral, support hierarchyinterface as well
  • Loading branch information
dbu committed Apr 30, 2014
2 parents 5bd9d9d + 2958ab3 commit a828d3d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
23 changes: 18 additions & 5 deletions Admin/Extension/ChildExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sonata\AdminBundle\Admin\AdminExtension;
use Sonata\AdminBundle\Admin\AdminInterface;
use Symfony\Cmf\Bundle\CoreBundle\Model\ChildInterface;
use Doctrine\ODM\PHPCR\HierarchyInterface;

/**
* Admin extension to handle child models.
Expand All @@ -29,14 +30,26 @@ class ChildExtension extends AdminExtension
*/
public function alterNewInstance(AdminInterface $admin, $object)
{
if (!$object instanceof ChildInterface) {
throw new \InvalidArgumentException('Expected ChildInterface, got ' . get_class($object));
if (!$admin->hasRequest()
|| !$parentId = $admin->getRequest()->get('parent')
) {
return;
}

if ($admin->hasRequest() && $parentId = $admin->getRequest()->get('parent')) {
if ($parent = $admin->getModelManager()->find(null, $parentId)) {
$parent = $admin->getModelManager()->find(null, $parentId);
if (!$parent) {
return;
}

switch($object) {
case $object instanceof HierarchyInterface:
$object->setParentDocument($parent);
}
break;
case $object instanceof ChildInterface:
$object->setParentObject($parent);
break;
default:
throw new \InvalidArgumentException(sprintf('Class %s is not supported', get_class($object)));
}
}
}
9 changes: 6 additions & 3 deletions Model/ChildInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
namespace Symfony\Cmf\Bundle\CoreBundle\Model;

/**
* An interface for models with a parent document.
* An interface for models with a parent object.
*
* Note that PHPCR-ODM documents will most likely use the HierarchyInterface
* of PHPCR-ODM instead.
*/
interface ChildInterface
{
/**
* @param $parent object
*/
public function setParentDocument($parent);
public function setParentObject($parent);

/**
* @return object
*/
public function getParentDocument();
public function getParentObject();
}
2 changes: 1 addition & 1 deletion Tests/Unit/Admin/Extension/ChildExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testAlterNewInstance()

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

$extension = new ChildExtension();
Expand Down

0 comments on commit a828d3d

Please sign in to comment.