Menu

How to use Feeds and implement Factories

Anonymous
2013-03-24
2013-03-25
  • Anonymous

    Anonymous - 2013-03-24

    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
    • Gabriel Zerbib

      Gabriel Zerbib - 2013-03-25

      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

      <html>
        <body>
          <fig:feed class="MyFeed" target="myfeed" />
          <div fig:text="/myfeed/mydata" />
        </body>
      </html>
      

      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

      <?php
      class MyFeed extends FIG_Feed {
      
        public function setDatabase($database) {
          $this->db = $database;
        }
      
        public function run() {
          //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

      <?php
      class MyDatabaseAwareFeedFactory implements FIG_FeedFactory {
        public function create ( /*string*/ $className, array $attributes ) {
      
          //Your Factory can be responsible for several
          // Feed classes, based on their names
          if ( 'MyFeed' == $className ) {
            $feed = new MyFeed();
            $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:
          return null;
        }
      
        //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.
        public function setDatabase( ... ) {
          ...
        }
      }
      

      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 = new MyDatabaseAwareFeedFactory();
      $feedFactory->setDatabase( /* your db link, or PDO object, etc. */ );
      
      $view = new FIG_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

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.