I read about your project in March 2013 Linux Magazine France. I found the concept brilliant and like to use it in a small project of mine. Since I am just a programming enthusiast, I do not understand some concepts or, at least, don't know how to implement them. I read some articles about the "Factory pattern" but still can't understand how FigDice need it to work when using Feeds. Could you please point me to some working code I could study?
Thanks in advance.
Martin
Last edit: Gabriel Zerbib 2013-03-24
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Let's consider that your Feed class MyFeed must read data from a database.
Feed class
First, your Feed class must have a public method which enables the injection of the Database object (PDO, direct credentials, mysqli resource: it can be anything you want, depending on the way you code your DB interactions). The Feed instance will just assume that this has been prepared beforehand, from outside.
File: MyFeed.php
<?phpclassMyFeedextendsFIG_Feed{publicfunctionsetDatabase($database){$this->db=$database;}publicfunctionrun() {//This is the method that the framework calls//when it needs to execute your Feed class in//order to inject its outcome into the View's universe.//At this time, you can safely assume that the// setDatabase( ) method//has been called already, and that your// $this->db attribute//is populated.//To fetch the data from your database, you will use,// depending on your project:/*either*/$this->db->query("some sql string");/*or*/mysqli_query("some sql string",$this->db);//and so on.}}
Feed Factory
Now, you will tell your FIG_View how to instantiate MyFeed on demand:
you do this through a Feed Factory.
File: MyDatabaseAwareFeedFactory.php
<?phpclassMyDatabaseAwareFeedFactoryimplementsFIG_FeedFactory{publicfunctioncreate(/*string*/$className,array$attributes){//Your Factory can be responsible for several// Feed classes, based on their namesif('MyFeed'==$className){$feed=newMyFeed();$feed->setDatabase(/* everything you need to make the Feed class aware of the DB */);return$feed;}//Your factory should serve in the same way//the instances of various Feed class//which belong to a same family (e.g. Feeds that//read data from one same database).//If your Factory class was requested a Feed// which it does not know to handle:returnnull;}//You call this method yourself, in your controller,//before passing this Factory to the View.//By this method you make your Factory keep track//of the Database, so as to make it possible to//propagate it to the future Feed instances that//this Factory will create.publicfunctionsetDatabase(...){...}}
Controller
Finally, your controller (which owns the main access to database) will invoke the view after registering the Feed Factory.
The Feed Factory is given access to DB through your special method, and in turn it will be able to pass it the the feeds later.
<?php$feedFactory=newMyDatabaseAwareFeedFactory();$feedFactory->setDatabase(/* your db link, or PDO object, etc. */);$view=newFIG_View();$view->registerFeedFactory($feedFactory);// and continue normally with the View// in particular:// - load the XML source,// - register more Feed Factories// - register custom function factories// - set the output Language of the view, and// indicate the path for internationalization files,// - and finally ->render() the view.
I hope this helps!
Regards,
Gabriel
Last edit: Gabriel Zerbib 2013-03-25
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Gabriel,
I read about your project in March 2013 Linux Magazine France. I found the concept brilliant and like to use it in a small project of mine. Since I am just a programming enthusiast, I do not understand some concepts or, at least, don't know how to implement them. I read some articles about the "Factory pattern" but still can't understand how FigDice need it to work when using Feeds. Could you please point me to some working code I could study?
Thanks in advance.
Martin
Last edit: Gabriel Zerbib 2013-03-24
Hi Martin,
Thank you for your comments.
Below is an example of how to use the Factory pattern with Feeds in FigDice.
Suppose that you want to render this View:
File: myView.xml
Let's consider that your Feed class MyFeed must read data from a database.
Feed class
First, your Feed class must have a public method which enables the injection of the Database object (PDO, direct credentials, mysqli resource: it can be anything you want, depending on the way you code your DB interactions). The Feed instance will just assume that this has been prepared beforehand, from outside.
File: MyFeed.php
Feed Factory
Now, you will tell your FIG_View how to instantiate MyFeed on demand:
you do this through a Feed Factory.
File: MyDatabaseAwareFeedFactory.php
Controller
Finally, your controller (which owns the main access to database) will invoke the view after registering the Feed Factory.
The Feed Factory is given access to DB through your special method, and in turn it will be able to pass it the the feeds later.
I hope this helps!
Regards,
Gabriel
Last edit: Gabriel Zerbib 2013-03-25