26 Aralık 2009 Cumartesi

ADVTemplateList Class ile Tema Listesinin Alınması

ADVTemplateList sınıfı reklam yöneticimiz tarafından kullanılacak ve şablon listemizi hazırlayacak olan sınıftır. Amacı belirtilen bir veya birden fazla klasör içerisinde bulunan şablon dosyalarını okuyarak ADVTemplate objeleri listesini hazırlamasıdır.
Bu sınıf oluşturulurken listesini hazırlayacağı klasör konumuna zorunlu olarak ihtiyaç duymaktadır. Hazırladığım sınıfın kaynak kodu şu şekilde geliştirildi.
<?php
/**
  * This class is used to represent the list of Templates that are in a directory.
  * 
  * @author Behçet MUTLU
  * @copyright 2009 Adres Gezgini
  */
  class ADVTemplateList {
    
      private $dir;
      private $list;
      private $ready;
      
      /**
      * This is the default constructor of the class that needs directory to be 
      * traversed of which contains the templates.
      * 
      * @param mixed $dir
      * @return void
      */
      public function __construct($dir)
      {
          $this->dir = $dir;
          $this->_initialize();
      }
      
      /**
      * This method is used to initialize the private variables of the class that
      * are used by this class.
      * 
      * @return void
      */
      private function _initialize()
      {
          $this->list = $this->_getTemplates($this->dir);
          
          $this->ready = $this->list != false;
      }
      
      /**
      * This method is used to traverse the list of the directories of given directory
      * and collect the list of templates in to an array and return, if the directory
      * exist template list, otherwise false.
      * 
      * @param mixed $directory to be traversed
      * @param mixed $exempt to exclude from listing
      * @param mixed $templates the referance to be used 
      *                         if we want collect from multiple directories
      * @return template list or false
      */
      private function _getTemplates($directory, 
                                      $exempt = array('.','..','.ds_store','.svn'), 
                                      &$templates = array()) 
      {
          if( ! ($handle = @opendir($directory))) // with @ character we are avoiding the wrong path error to be displayed as an exception
              return false;
          
          while(false !== ($resource = readdir($handle))) {
              if(!in_array(strtolower($resource),$exempt)) {
                  if(is_dir($directory.$resource.'/'))
                  {
                      if(file_exists($directory.$resource."/template.xml"))
                       $templates[] = new ADVTemplate($directory.$resource."/template.xml");
                      else
                       debug("Template at \"" . $directory.$resource . "\" does not contain  <i>template.xml</i> schema file.");
                  }
              }
          }
          closedir($handle);
          return $templates;
      } 
      
      /**
      * The setter methods of the class
      * @param value to set
      */
      public function setList($value) { $this->list = $value; }
      public function setDir($value) { $this->dir = $value; $this->_initialize();}
      
      /**
      * The getter methods of the class
      * 
      * @return values requested
      */
      public function getList() { return $this->list; }
      public function getDir() { return $this->dir; }
      public function isReady() { return $this->ready; }
  }
?>

0 yorum:

Yorum Gönder