How to fake a request…
…or, why are there no setXXXXX() methods in ZenMagick?
These are the sort of questions that have started to pop up more frequently lately, so I’ll try to answer them a bit.
First of all, there is no final answer, as ZenMagick is a (fast) moving target.
The current state of ZenMagick is the result of starting with a couple classes to be able to pull some category and product data out of the database. From there focus shifted towards replacing the current cumbersome zen-cart templating mechanism. For this, it turned out, a lot more classes to read the database were needed and thus added over time.
So, right now, ZenMagick is a (mostly) read only front-end, replacing zen-cart’s templating code and a few other bits and pieces.
The main reason for missing setXXX() methods on model classes is that I was to lazy to code them :). Also, they were not needed externally. Internally, most services access class variables directly. This is not very nice and not compatible with PHP5’s protected and private qualifiers. I added set methods only where they are required by external code, for example templates, html.default code or plugins. Things are starting to change, though. Looking at the development between 0.7.1 and 0.8.0 you will see some new setXXX() methods, in particular around account objects. Also, the relevant service has now support for updating accounts and there are more to come.
Scripted access to ZenMagick/zen-cart is not easily archived, as ZenMagick does not have a separate layer with code that implements business logic. All logic is currently in the processGet/processPost methods of the controller implementations. That makes it a lot harder to reuse. Having said that, I am currently working on some sample code to illustrate how to use that to script controller…
So, to sum it all up, ZenMagick is moving towards being more useful for scripted/batch use. There will be more support for database updates, more getXXX() methods on classes and so forth.
Just keep in mind that this is all not my main objective. ZenMagick is the result of voluntary work and therefore I feel free to set my priorities based on motivation and fun factor rather than commercial pressure.
So, the next release will include some sample code to show how Zenmagick may be used to script stuff, but for the really impatient, here is a quick example of how a controller may be called by external code:
// include ZenMagick API
require("../zen-cart/zenmagick/external.php");
$email = “some@email.com”;
$pwd = “xxxxxx”;
// fake request
$_GET[’main_page’] = ‘login’;
// create login controller
$controller = $zm_loader->create(”LoginController”);
// fake login submit
global $_POST;
$_POST = array();
$_POST[’email_address’] = $email;
$_POST[’password’] = $pwd;
// execute
$view = $controller->processPost();
