Skip to content

Commit

Permalink
Extending the actions class to add a getDoctrineMenu() method to acti…
Browse files Browse the repository at this point in the history
…ons.
  • Loading branch information
weaverryan committed Jun 4, 2010
1 parent e703179 commit 35d4b31
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 8 deletions.
3 changes: 3 additions & 0 deletions config/ioDoctrineMenuPluginConfiguration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public function initialize()
{
$this->dispatcher->connect('routing.load_configuration', array($this, 'loadRoutes'));
}

$actions = new ioDoctrineMenuActions($this);
$this->dispatcher->connect('component.method_not_found', array($actions, 'extend'));
}

/**
Expand Down
66 changes: 66 additions & 0 deletions lib/actions/ioDoctrineMenuActions.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Adds methods that can be called from the actions class
*
* @package ioDoctrineMenuPlugin
* @subpackage actions
* @author Ryan Weaver <ryan@thatsquality.com>
*/
class ioDoctrineMenuActions
{
/**
* @var ioDoctrineMenuPluginConfiguration
*/
protected $_pluginConfiguration;

/**
* Class constructor
*
* @param ioDoctrineMenuPluginConfiguration $configuration
*/
public function __construct(ioDoctrineMenuPluginConfiguration $configuration)
{
$this->_pluginConfiguration = $configuration;
}

/**
* Returns an ioMenuItem tree loaded from the given name that corresponds
* to a root node name in the ioDoctrineMenuItem model.
*
* This method cann be called directly from the actions class.
*
* @param string $name The name of the root menu item to return
* @return ioMenuItem
*/
public function getDoctrineMenu($name)
{
return $this->_pluginConfiguration->getMenuManager()
->getMenu($name);
}

/**
* Listener method for method_not_found events
*
* This allows any public other methods in this class to be called as
* if they were in the actions class.
*/
public function extend(sfEvent $event)
{
$this->_subject = $event->getSubject();
$method = $event['method'];
$arguments = $event['arguments'];

if (method_exists($this, $method))
{
$result = call_user_func_array(array($this, $method), $arguments);

$event->setReturnValue($result);

return true;
}
else
{
return false;
}
}
}
11 changes: 3 additions & 8 deletions test/fixtures/project/apps/frontend/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ homepage:
url: /
param: { module: default, action: index }

# generic rules
# please, remove them by adding more specific rules
default_index:
url: /:module
param: { action: index }

default:
url: /:module/:action/*
actions_get_menu:
url: /actions/get-menu
param: { module: test, action: getMenu }
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
// test actions class
class testActions extends sfActions
{
// tests calling ->getMenu() on the actions class
public function executeGetMenu(sfWebRequest $request)
{
$this->menu = $this->getDoctrineMenu('Root li');
$this->setTemplate('render');
$this->renderText($this->menu->render());

$this->setLayout(false);
return sfView::NONE;
}
}
23 changes: 23 additions & 0 deletions test/functional/menuActionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

require_once dirname(__FILE__).'/../bootstrap/functional.php';
require_once sfConfig::get('sf_lib_dir').'/test/unitHelper.php';

$browser = new sfTestFunctional(new sfBrowser());

create_doctrine_test_tree($browser->test());
$browser->info('1 - Goto a url that uses ioDoctrineMenuActions->getMenu()')
->get('/actions/get-menu')

->with('request')->begin()
->isParameter('module', 'test')
->isParameter('action', 'getMenu')
->end()

->with('response')->begin()
->isStatusCode(200)
->info(' 1.1 - Check for the root ul and its 7 descendants.')
->checkElement('ul.root', true)
->checkElement('ul.root li', 7)
->end()
;

0 comments on commit 35d4b31

Please sign in to comment.