Skip to content

Commit

Permalink
Merge pull request #213 from gregumo/master
Browse files Browse the repository at this point in the history
Use EntityManager in BusinessPageHelper to get correct entity name
  • Loading branch information
paulandrieux committed Sep 23, 2015
2 parents 7de6a52 + 2bf4726 commit 8e7a2b7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
15 changes: 10 additions & 5 deletions Bundle/BusinessPageBundle/Helper/BusinessPageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,25 @@ public function getIdentifierPositionInUrl(BusinessTemplate $bepPattern)

/**
* Guess the best pattern to represent given reflectionClass
*
* @param \ReflectionClass $refClass
* @param integer $entityId
* @param EntityManager $em
* @param string $originalRefClassName When digging into parentClass, we do not have to forget originalClass to be able to get reference after all
*
* @return View
* @throws \Exception
*/
public function guessBestPatternIdForEntity($refClass, $entityId, $originalRefClassName = null)
public function guessBestPatternIdForEntity($refClass, $entityId, $em, $originalRefClassName = null)
{
$refClassName = $em->getClassMetadata($refClass->name)->name;

$viewReference = null;
if (!$originalRefClassName) {
$originalRefClassName = $refClass->name;
$originalRefClassName = $refClassName;
}

$businessEntity = $this->businessEntityHelper->findByEntityClassname($refClass->name);
$businessEntity = $this->businessEntityHelper->findByEntityClassname($refClassName);

if ($businessEntity) {
$parameters = array(
Expand All @@ -203,9 +208,9 @@ public function guessBestPatternIdForEntity($refClass, $entityId, $originalRefCl
if (!$viewReference) {
$parentRefClass = $refClass->getParentClass();
if ($parentRefClass) {
$viewReference['patternId'] = $this->guessBestPatternIdForEntity($parentRefClass, $entityId, $originalRefClassName);
$viewReference['patternId'] = $this->guessBestPatternIdForEntity($parentRefClass, $entityId, $em, $originalRefClassName);
} else {
throw new \Exception(sprintf('Cannot find a BusinessTemplate that can display the requested BusinessEntity ("%s", "%s".)', $refClass->name, $entityId));
throw new \Exception(sprintf('Cannot find a BusinessTemplate that can display the requested BusinessEntity ("%s", "%s".)', $refClassName, $entityId));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Bundle/PageBundle/Controller/BasePageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function showBusinessPageByIdAction($entityId, $type)
$refClass = new \ReflectionClass($entity);

$patternId = $this->container->get('victoire_business_page.business_page_helper')
->guessBestPatternIdForEntity($refClass, $entityId);
->guessBestPatternIdForEntity($refClass, $entityId, $this->container->get('doctrine.orm.entity_manager'));

$page = $this->container->get('victoire_page.page_helper')->findPageByParameters(array(
'viewId' => $patternId,
Expand Down
1 change: 1 addition & 0 deletions Bundle/WidgetBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ services:
- @victoire_core.helper.business_entity_helper
- @victoire_business_page.business_page_helper
- @victoire_page.page_helper
- @doctrine.orm.entity_manager
tags:
- { name: twig.extension }

Expand Down
8 changes: 6 additions & 2 deletions Bundle/WidgetBundle/Twig/LinkExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Victoire\Bundle\WidgetBundle\Twig;

use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand All @@ -20,14 +21,16 @@ class LinkExtension extends \Twig_Extension
protected $businessEntityHelper; // @victoire_business_page.business_entity_helper
protected $BusinessPageHelper; // @victoire_business_page.business_page_helper
protected $pageHelper;
protected $em; // @doctrine.orm.entity_manager

public function __construct(
Router $router,
RequestStack $requestStack,
$analytics,
BusinessEntityHelper $businessEntityHelper,
BusinessPageHelper $BusinessPageHelper,
PageHelper $pageHelper
PageHelper $pageHelper,
EntityManager $em
)
{
$this->router = $router;
Expand All @@ -36,6 +39,7 @@ public function __construct(
$this->businessEntityHelper = $businessEntityHelper;
$this->BusinessPageHelper = $BusinessPageHelper;
$this->pageHelper = $pageHelper;
$this->em = $em;
}
/**
* Returns a list of functions to add to the existing list.
Expand Down Expand Up @@ -194,7 +198,7 @@ public function victoireBusinessLink($businessEntityInstance, $patternId = null,
{
if (!$patternId) {
$patternId = $this->BusinessPageHelper
->guessBestPatternIdForEntity(new \ReflectionClass($businessEntityInstance), $businessEntityInstance->getId());
->guessBestPatternIdForEntity(new \ReflectionClass($businessEntityInstance), $businessEntityInstance->getId(), $this->em);
}

$page = $this->pageHelper->findPageByParameters(array(
Expand Down

0 comments on commit 8e7a2b7

Please sign in to comment.