1: <?php
2: /**
3: * Base class for controllers that implements the Logged, Injected, and Viewed
4: * interfaces.
5: *
6: * This class is for convenience, if you decide you wish to use only logging or
7: * the injector or views, or neither, you do not have to use it. As long as
8: * your controllers implement Horde_Controller, they are runnable.
9: *
10: * @category Horde
11: * @package Controller
12: * @author James Pepin <james@bluestatedigital.com>
13: * @license http://www.horde.org/licenses/bsd BSD
14: */
15: abstract class Horde_Controller_Base implements Horde_Controller
16: {
17: /**
18: * This is marked private on purpose, so that you have to use the
19: * getInjector() method to access it in derived classes. This is done so
20: * that you don't assume its always set, since its set via setter-injection
21: * to save on having to define a constructor param for it
22: *
23: * @var Horde_Injector
24: */
25: private $_injector;
26:
27: /**
28: * Private on purpose so you have to use getLogger().
29: *
30: * @var Horde_Log_Logger
31: */
32: private $_logger;
33:
34: /**
35: * Private on purpose so you have to use getView().
36: *
37: * @var Horde_View
38: */
39: private $_view;
40:
41: /**
42: * Private on purpose so you have to use getUrlWriter().
43: *
44: * @var Horde_Controller_UrlWriter
45: */
46: private $_urlWriter;
47:
48: /**
49: * Set the injector for this controller
50: *
51: * @inject
52: *
53: * @param Horde_Injector The injector that this controller should use to create objects
54: */
55: public function setInjector(Horde_Injector $injector)
56: {
57: $this->_injector = $injector;
58: }
59:
60: /**
61: * Get the injector for this controller
62: *
63: * @return Horde_Injector The injector previously set for this controller,
64: * or a new Horde_Injector_TopLevel
65: */
66: public function getInjector()
67: {
68: if (!$this->_injector) {
69: $this->_injector = new Horde_Injector_TopLevel();
70: }
71: return $this->_injector;
72: }
73:
74: /**
75: * Set the Logger for this controller
76: *
77: * @inject
78: *
79: * @param Horde_Log_Logger The logger to use for this controller
80: */
81: public function setLogger(Horde_Log_Logger $logger)
82: {
83: $this->_logger = $logger;
84: }
85:
86: /**
87: * Get the logger assigned to this controller
88: *
89: * @return Horde_Log_Logger The logger for this controller
90: */
91: public function getLogger()
92: {
93: if (!$this->_logger) {
94: $this->_logger = new Horde_Log_Logger(new Horde_Log_Handler_Null());
95: }
96: return $this->_logger;
97: }
98:
99: /**
100: * Set the Horde_View object to be used for this controller
101: *
102: * @inject
103: *
104: * @param Horde_View_Base The view object
105: */
106: public function setView(Horde_View_Base $view)
107: {
108: $this->_view = $view;
109: $this->_view->controller = $this;
110: }
111:
112: /**
113: * Gets the current view for this controller
114: *
115: * @note This method will create an empty Horde_View if none has been set.
116: *
117: * @return Horde_View_Base The view for this controller, or a new empty
118: * Horde_View if none is set
119: */
120: public function getView()
121: {
122: if (!$this->_view) {
123: $this->setView($this->getInjector()->getInstance('Horde_View_Base'));
124: }
125: return $this->_view;
126: }
127:
128: /**
129: * Get the current request
130: */
131: public function getRequest()
132: {
133: return $this->getInjector()->getInstance('Horde_Controller_Request');
134: }
135:
136: /**
137: * Get the current response
138: */
139: public function getResponse()
140: {
141: return $this->getInjector()->getInstance('Horde_Controller_Response');
142: }
143:
144: /**
145: * Get an instance of UrlWriter for this controller.
146: *
147: * @return Horde_Controller_UrlWriter
148: */
149: public function getUrlWriter()
150: {
151: // instantiate UrlWriter that will generate URLs for this controller
152: if (!$this->_urlWriter) {
153: // Need a reasonable way to get the :controller match from the URL - reverse route?
154: // $defaults = array('controller' => $this->getControllerName());
155: $this->_urlWriter = $this->getInjector()->getInstance('Horde_Controller_UrlWriter');
156: }
157: return $this->_urlWriter;
158: }
159: }
160: