Components used in Shells
While working with Shells, most of the time I have some Tasks taking care of certain specific business logic each. Today, I had the need for a special Task. Special because it was actually a kinda duplicate of an existing Component, my quite useful but still incomplete to share, SerialComponent.
Like I just said, this Component is incomplete and I know that I will be making several changes in the coming month - so quickly, before thinking of duplicate code, good/bad cake or anything like that, I realized that I didn’t want to have 2 different classes to update every time. That been said, what’s coming next is my implementation of a Component inside a Task, which, I still don’t know if it makes a good or bad cake but it at least avoids most of the duplicate code. Maybe there is a better one I don’t know of that would entirely avoid that duplication but until then, the following works.
include COREPATH . ‘cake’ . DS . ‘libs’ . DS . ‘controller’ . DS . ‘component.php’; include APPPATH . ‘controllers’ . DS . ‘components’ . DS . ’serial.php’;That’s what was in my /vendors/shells/tasks/serializer.phpclass SerializerTask extends Shell { /** * Holds instance of SerialComponent * * @var SerialComponent * @access private */ var $_serial = null; /** * Overrides the shell constructor and instantiate the SerialComponent * * @access protected */ function __construct() { $this->_serial =& new SerialComponent(); } /** * Pass-thru method for SerialComponent::example() * * @param string * @param array * @return array * @access public */ function example($type, $params) { return $this->_serial->example($type, $params); } }
From my shell I could now call it like any other task. Do you know of a better method? Keep in mind, I want to use it exactly like a Task, meaning by multiple shells in different methods, so instantiating it when needed becomes more of code bloating IMO. What do you think?
October 27th, 2007 at 4:50 pm
Developing a lot in Shells classes lately but not all is very clear to me. Like using helpers and Components in the shell. Your approach seems ok, but I can’t imagine that the cakePHP developers didn’t think of using components in the Shell. So including it yourself and looad the class is maybe a bit too much for the RAD
October 27th, 2007 at 5:44 pm
@Charlie: Well, surprisingly, I believe they did ignore components in shells. I am sure they separated helpers from shells, heck they can’t even be used in controllers