Skip to content

Commit 35d4b31

Browse files
committed
Extending the actions class to add a getDoctrineMenu() method to actions.
1 parent e703179 commit 35d4b31

File tree

5 files changed

+110
-8
lines changed

5 files changed

+110
-8
lines changed

config/ioDoctrineMenuPluginConfiguration.class.php

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public function initialize()
1717
{
1818
$this->dispatcher->connect('routing.load_configuration', array($this, 'loadRoutes'));
1919
}
20+
21+
$actions = new ioDoctrineMenuActions($this);
22+
$this->dispatcher->connect('component.method_not_found', array($actions, 'extend'));
2023
}
2124

2225
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Adds methods that can be called from the actions class
4+
*
5+
* @package ioDoctrineMenuPlugin
6+
* @subpackage actions
7+
* @author Ryan Weaver <[email protected]>
8+
*/
9+
class ioDoctrineMenuActions
10+
{
11+
/**
12+
* @var ioDoctrineMenuPluginConfiguration
13+
*/
14+
protected $_pluginConfiguration;
15+
16+
/**
17+
* Class constructor
18+
*
19+
* @param ioDoctrineMenuPluginConfiguration $configuration
20+
*/
21+
public function __construct(ioDoctrineMenuPluginConfiguration $configuration)
22+
{
23+
$this->_pluginConfiguration = $configuration;
24+
}
25+
26+
/**
27+
* Returns an ioMenuItem tree loaded from the given name that corresponds
28+
* to a root node name in the ioDoctrineMenuItem model.
29+
*
30+
* This method cann be called directly from the actions class.
31+
*
32+
* @param string $name The name of the root menu item to return
33+
* @return ioMenuItem
34+
*/
35+
public function getDoctrineMenu($name)
36+
{
37+
return $this->_pluginConfiguration->getMenuManager()
38+
->getMenu($name);
39+
}
40+
41+
/**
42+
* Listener method for method_not_found events
43+
*
44+
* This allows any public other methods in this class to be called as
45+
* if they were in the actions class.
46+
*/
47+
public function extend(sfEvent $event)
48+
{
49+
$this->_subject = $event->getSubject();
50+
$method = $event['method'];
51+
$arguments = $event['arguments'];
52+
53+
if (method_exists($this, $method))
54+
{
55+
$result = call_user_func_array(array($this, $method), $arguments);
56+
57+
$event->setReturnValue($result);
58+
59+
return true;
60+
}
61+
else
62+
{
63+
return false;
64+
}
65+
}
66+
}

test/fixtures/project/apps/frontend/config/routing.yml

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ homepage:
66
url: /
77
param: { module: default, action: index }
88

9-
# generic rules
10-
# please, remove them by adding more specific rules
11-
default_index:
12-
url: /:module
13-
param: { action: index }
14-
15-
default:
16-
url: /:module/:action/*
9+
actions_get_menu:
10+
url: /actions/get-menu
11+
param: { module: test, action: getMenu }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
// test actions class
3+
class testActions extends sfActions
4+
{
5+
// tests calling ->getMenu() on the actions class
6+
public function executeGetMenu(sfWebRequest $request)
7+
{
8+
$this->menu = $this->getDoctrineMenu('Root li');
9+
$this->setTemplate('render');
10+
$this->renderText($this->menu->render());
11+
12+
$this->setLayout(false);
13+
return sfView::NONE;
14+
}
15+
}

test/functional/menuActionsTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
require_once dirname(__FILE__).'/../bootstrap/functional.php';
4+
require_once sfConfig::get('sf_lib_dir').'/test/unitHelper.php';
5+
6+
$browser = new sfTestFunctional(new sfBrowser());
7+
8+
create_doctrine_test_tree($browser->test());
9+
$browser->info('1 - Goto a url that uses ioDoctrineMenuActions->getMenu()')
10+
->get('/actions/get-menu')
11+
12+
->with('request')->begin()
13+
->isParameter('module', 'test')
14+
->isParameter('action', 'getMenu')
15+
->end()
16+
17+
->with('response')->begin()
18+
->isStatusCode(200)
19+
->info(' 1.1 - Check for the root ul and its 7 descendants.')
20+
->checkElement('ul.root', true)
21+
->checkElement('ul.root li', 7)
22+
->end()
23+
;

0 commit comments

Comments
 (0)