Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored to inject helper rather than extending it. #68

Merged
merged 3 commits into from
Jul 12, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions Templating/Helper/CmfHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public function __construct(SecurityContextInterface $publishWorkflowChecker = n
}
}

protected function getDm()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lsmith77 I think this is actually the right way to do it anyway - I believe we should also add setManagerName and that this method should return the document manager from the registry depending on the value of $managerName ??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case it might however make sense to assign the manager to a local variable for those methods that use the manager multiple times.

{
if (!$this->dm) {
throw new \RuntimeException('Document Manager has not been initialized yet.');
}

return $this->dm;
}

/**
* Gets the helper name.
*
Expand Down Expand Up @@ -81,7 +90,7 @@ public function getParentPath($document)
public function getPath($document)
{
try {
return $this->dm->getUnitOfWork()->getDocumentId($document);
return $this->getDm()->getUnitOfWork()->getDocumentId($document);
} catch (\Exception $e) {
return false;
}
Expand All @@ -95,7 +104,7 @@ public function getPath($document)
*/
public function find($path)
{
return $this->dm->find(null, $path);
return $this->getDm()->find(null, $path);
}

/**
Expand All @@ -113,7 +122,7 @@ public function find($path)
private function getDocument($document, $ignoreRole = false, $class = null)
{
if (is_string($document)) {
$document = $this->dm->find(null, $document);
$document = $this->getDm()->find(null, $document);
}
if (null !== $ignoreRole && null === $this->publishWorkflowChecker) {
throw new InvalidConfigurationException('You can not fetch only published documents when the publishWorkflowChecker is not set. Either enable the publish workflow or pass "ignoreRole = null" to skip publication checks.');
Expand Down Expand Up @@ -194,15 +203,15 @@ public function isPublished($document)
public function getLocalesFor($document, $includeFallbacks = false)
{
if (is_string($document)) {
$document = $this->dm->find(null, $document);
$document = $this->getDm()->find(null, $document);
}

if (empty($document)) {
return array();
}

try {
$locales = $this->dm->getLocalesFor($document, $includeFallbacks);
$locales = $this->getDm()->getLocalesFor($document, $includeFallbacks);
} catch (MissingTranslationException $e) {
$locales = array();
}
Expand All @@ -220,13 +229,13 @@ public function getChild($parent, $name)
{
if (is_object($parent)) {
try {
$parent = $this->dm->getUnitOfWork()->getDocumentId($parent);
$parent = $this->getDm()->getUnitOfWork()->getDocumentId($parent);
} catch (\Exception $e) {
return false;
}
}

return $this->dm->find(null, "$parent/$name");
return $this->getDm()->find(null, "$parent/$name");
}

/**
Expand All @@ -249,9 +258,9 @@ public function getChildren($parent, $limit = false, $offset = false, $filter =

if ($limit || $offset) {
if (is_object($parent)) {
$parent = $this->dm->getUnitOfWork()->getDocumentId($parent);
$parent = $this->getDm()->getUnitOfWork()->getDocumentId($parent);
}
$node = $this->dm->getPhpcrSession()->getNode($parent);
$node = $this->getDm()->getPhpcrSession()->getNode($parent);
$children = (array) $node->getNodeNames();
foreach ($children as $key => $child) {
// filter before fetching data already to save some traffic
Expand All @@ -268,7 +277,7 @@ public function getChildren($parent, $limit = false, $offset = false, $filter =
$children = array_slice($children, $key);
}
} else {
$children = $this->dm->getChildren($parent, $filter);
$children = $this->getDm()->getChildren($parent, $filter);
}

$result = array();
Expand Down Expand Up @@ -327,7 +336,7 @@ private function getChildrenPaths($path, array &$children, $depth)

--$depth;

$node = $this->dm->getPhpcrSession()->getNode($path);
$node = $this->getDm()->getPhpcrSession()->getNode($path);
$names = (array) $node->getNodeNames();
foreach ($names as $name) {
if (strpos($name, 'phpcr_locale:') === 0) {
Expand All @@ -353,7 +362,7 @@ public function getDescendants($parent, $depth = null)

$children = array();
if (is_object($parent)) {
$parent = $this->dm->getUnitOfWork()->getDocumentId($parent);
$parent = $this->getDm()->getUnitOfWork()->getDocumentId($parent);
}
$this->getChildrenPaths($parent, $children, $depth);

Expand Down Expand Up @@ -407,7 +416,7 @@ private function traversePrevDepth($depth, $anchorDepth, array $childNames, $pat
{
foreach ($childNames as $childName) {
$childPath = "$path/$childName";
$node = $this->dm->getPhpcrSession()->getNode($childPath);
$node = $this->getDm()->getPhpcrSession()->getNode($childPath);
if (null === $depth || PathHelper::getPathDepth($childPath) - $anchorDepth < $depth) {
$childNames = $node->getNodeNames()->getArrayCopy();
if (!empty($childNames)) {
Expand Down Expand Up @@ -440,17 +449,17 @@ private function traversePrevDepth($depth, $anchorDepth, array $childNames, $pat
private function searchDepthPrev($path, $anchor, $depth = null, $ignoreRole = false, $class = null)
{
if (is_object($path)) {
$path = $this->dm->getUnitOfWork()->getDocumentId($path);
$path = $this->getDm()->getUnitOfWork()->getDocumentId($path);
}

if (null === $path || '/' === $path) {
return null;
}

$node = $this->dm->getPhpcrSession()->getNode($path);
$node = $this->getDm()->getPhpcrSession()->getNode($path);

if (is_object($anchor)) {
$anchor = $this->dm->getUnitOfWork()->getDocumentId($anchor);
$anchor = $this->getDm()->getUnitOfWork()->getDocumentId($anchor);
}

if (0 !== strpos($path, $anchor)) {
Expand Down Expand Up @@ -514,17 +523,17 @@ private function searchDepthPrev($path, $anchor, $depth = null, $ignoreRole = fa
private function searchDepthNext($path, $anchor, $depth = null, $ignoreRole = false, $class = null)
{
if (is_object($path)) {
$path = $this->dm->getUnitOfWork()->getDocumentId($path);
$path = $this->getDm()->getUnitOfWork()->getDocumentId($path);
}

if (null === $path || '/' === $path) {
return null;
}

$node = $this->dm->getPhpcrSession()->getNode($path);
$node = $this->getDm()->getPhpcrSession()->getNode($path);

if (is_object($anchor)) {
$anchor = $this->dm->getUnitOfWork()->getDocumentId($anchor);
$anchor = $this->getDm()->getUnitOfWork()->getDocumentId($anchor);
}

if (0 !== strpos($path, $anchor)) {
Expand Down Expand Up @@ -588,14 +597,14 @@ private function searchDepthNext($path, $anchor, $depth = null, $ignoreRole = fa
private function search($path, $reverse = false, $ignoreRole = false, $class = null)
{
if (is_object($path)) {
$path = $this->dm->getUnitOfWork()->getDocumentId($path);
$path = $this->getDm()->getUnitOfWork()->getDocumentId($path);
}

if (null === $path || '/' === $path) {
return null;
}

$node = $this->dm->getPhpcrSession()->getNode($path);
$node = $this->getDm()->getPhpcrSession()->getNode($path);
$parentNode = $node->getParent();
$childNames = $parentNode->getNodeNames()->getArrayCopy();
if ($reverse) {
Expand Down
26 changes: 26 additions & 0 deletions Tests/Unit/Twig/Extension/CmfExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Symfony\Cmf\Bundle\CoreBundle\Tests\Unit\Twig\Extension;

use Symfony\Cmf\Bundle\CoreBundle\Twig\Extension\CmfExtension;

class CmfExtensionTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->cmfHelper = $this->getMockBuilder(
'Symfony\Cmf\Bundle\CoreBundle\Templating\Helper\CmfHelper'
)->disableOriginalConstructor()->getMock();

$this->cmfExtension = new CmfExtension($this->cmfHelper);
$this->env = new \Twig_Environment();
$this->env->addExtension($this->cmfExtension);
}


public function testFunctions()
{
$functions = $this->cmfExtension->getFunctions();
$this->assertCount(15, $functions);
}
}
55 changes: 33 additions & 22 deletions Twig/Extension/CmfExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,48 @@

use Symfony\Cmf\Bundle\CoreBundle\Templating\Helper\CmfHelper;

class CmfExtension extends CmfHelper implements \Twig_ExtensionInterface
class CmfExtension implements \Twig_ExtensionInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the implement and extend Twig_Extension and remove all method (except getName) after line // from \Twig_Extension (line 41)

{
protected $cmfHelper;

public function __construct(CmfHelper $cmfHelper)
{
$this->cmfHelper = $cmfHelper;
}

/**
* Get list of available functions
*
* @return array
*/
public function getFunctions()
{
$functions = array('cmf_is_published' => new \Twig_Function_Method($this, 'isPublished'));

if ($this->dm) {
$functions['cmf_child'] = new \Twig_Function_Method($this, 'getChild');
$functions['cmf_children'] = new \Twig_Function_Method($this, 'getChildren');
$functions['cmf_prev'] = new \Twig_Function_Method($this, 'getPrev');
$functions['cmf_next'] = new \Twig_Function_Method($this, 'getNext');
$functions['cmf_find'] = new \Twig_Function_Method($this, 'find');
$functions['cmf_find_many'] = new \Twig_Function_Method($this, 'findMany');
$functions['cmf_descendants'] = new \Twig_Function_Method($this, 'getDescendants');
$functions['cmf_nodename'] = new \Twig_Function_Method($this, 'getNodeName');
$functions['cmf_parent_path'] = new \Twig_Function_Method($this, 'getParentPath');
$functions['cmf_path'] = new \Twig_Function_Method($this, 'getPath');
$functions['cmf_document_locales'] = new \Twig_Function_Method($this, 'getLocalesFor');
$functions = array(
new \Twig_SimpleFunction('cmf_is_published', array($this->cmfHelper, 'isPublished')),
new \Twig_SimpleFunction('cmf_child', array($this->cmfHelper, 'getChild')),
new \Twig_SimpleFunction('cmf_children', array($this->cmfHelper, 'getChildren')),
new \Twig_SimpleFunction('cmf_prev', array($this->cmfHelper, 'getPrev')),
new \Twig_SimpleFunction('cmf_next', array($this->cmfHelper, 'getNext')),
new \Twig_SimpleFunction('cmf_find', array($this->cmfHelper, 'find')),
new \Twig_SimpleFunction('cmf_find_many', array($this->cmfHelper, 'findMany')),
new \Twig_SimpleFunction('cmf_descendants', array($this->cmfHelper, 'getDescendants')),
new \Twig_SimpleFunction('cmf_nodename', array($this->cmfHelper, 'getNodeName')),
new \Twig_SimpleFunction('cmf_parent_path', array($this->cmfHelper, 'getParentPath')),
new \Twig_SimpleFunction('cmf_path', array($this->cmfHelper, 'getPath')),
new \Twig_SimpleFunction('cmf_document_locales', array($this->cmfHelper, 'getLocalesFor')),
);

if (interface_exists('Symfony\Cmf\Component\Routing\RouteAwareInterface')) {
$functions['cmf_prev_linkable'] = new \Twig_Function_Method($this, 'getPrevLinkable');
$functions['cmf_next_linkable'] = new \Twig_Function_Method($this, 'getNextLinkable');
$functions['cmf_linkable_children'] = new \Twig_Function_Method($this, 'getLinkableChildren');
}
if (interface_exists('Symfony\Cmf\Component\Routing\RouteAwareInterface')) {
$functions = array_merge($functions, array(
new \Twig_SimpleFunction('cmf_prev_linkable', array($this->cmfHelper, 'getPrevLinkable')),
new \Twig_SimpleFunction('cmf_next_linkable', array($this->cmfHelper, 'getNextLinkable')),
new \Twig_SimpleFunction('cmf_linkable_children', array($this->cmfHelper, 'getLinkableChildren')),
));
}

return $functions;
}

// from \Twig_Extension

/**
* Initializes the runtime environment.
*
Expand Down Expand Up @@ -110,4 +116,9 @@ public function getGlobals()
{
return array();
}

public function getName()
{
return 'cmf';
}
}