MediaSearch_File
[ class tree: MediaSearch_File ] [ index: MediaSearch_File ] [ all elements ]

Source for file TreeFactory.php

Documentation is available at TreeFactory.php

  1. <?php
  2. /**
  3.  * MediaSearch
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * $Author$
  14.  * $Revision$
  15.  *
  16.  * @category    MediaSearch
  17.  * @package     MediaSearch_File
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. /**
  22.  * @see MediaSearch_Interface_Factory
  23.  */
  24. require_once('MediaSearch/Interface/Factory.php');
  25.  
  26. /**
  27.  * Constructs a tree of a given root directory. The whole tree is generated
  28.  * recursivle of MediaSearch_File_Abstract objects.
  29.  *
  30.  * @category   MediaSearch
  31.  * @package    MediaSearch_File
  32.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  33.  */
  34. class MediaSearch_File_TreeFactory implements MediaSearch_Interface_Factory {
  35.     const TRACE_LEVEL 25;
  36.  
  37.     /**
  38.      * The root directory. The genarted tree's rrot node object
  39.      * has this directory as file path.
  40.      *
  41.      * @access private
  42.      * @var string 
  43.      */
  44.     private $rootDir;
  45.  
  46.     /**
  47.      * Sets the root directory.
  48.      *
  49.      * @access public
  50.      * @param string $rootDir 
  51.      */
  52.     public function __construct($rootDir{
  53.         $this->setRootDir($rootDir);
  54.     }
  55.  
  56.     /**
  57.      * Sets the root directory.
  58.      *
  59.      * @access public
  60.      * @param string $rootDir 
  61.      */
  62.     public function setRootDir($rootDir{
  63.         $this->rootDir $rootDir;
  64.     }
  65.  
  66.     /**
  67.      * MediaSearch_Interface_Factory implementation.
  68.      * Constructs the recursive directory tree and returns
  69.      * a object of type MediaSearch_File_Folder which represents
  70.      * the specified root directory.
  71.      *
  72.      * @access public
  73.      * @return MediaSearch_File_Folder 
  74.      */
  75.     public function construct({
  76.         MediaSearch_Log::trace('Constructing tree for: ' $this->rootDirself::TRACE_LEVEL);
  77.         $directoryIterator new RecursiveDirectoryIterator($this->rootDir);
  78.         $fileTree new MediaSearch_File_Folder(new SplFileInfo($this->rootDir));
  79.  
  80.         $fileTree $this->generateTree($fileTree$directoryIterator);
  81.  
  82.         return $fileTree;
  83.     }
  84.  
  85.     /**
  86.      * Helper method for recursive tree generation. On each subdirectory
  87.      * a recursiv call to this method is done.
  88.      *
  89.      * @access public
  90.      * @param MediaSearch_File_Folder $fileTree 
  91.      * @param RecursiveDirectoryIterator $directoryIterator 
  92.      * @return MediaSearch_File_Folder 
  93.      */
  94.     private function generateTree(MediaSearch_File_Folder $fileTreeRecursiveDirectoryIterator $directoryIterator{
  95.         MediaSearch_Log::trace('Generate folder: ' $directoryIterator->getPathname()self::TRACE_LEVEL);
  96.  
  97.         foreach ($directoryIterator as $entry{
  98.             if ($directoryIterator->isDot()) {
  99.                 continue;
  100.             }
  101.  
  102.             if (=== strpos($entry->getFilename()'.')) {
  103.                 continue;
  104.             }
  105.  
  106.             $fileInfo new SplFileInfo($entry->getPathname());
  107.  
  108.             if ($directoryIterator->hasChildren()) {
  109.                 $file new MediaSearch_File_Folder($fileInfo);
  110.                 $file $this->generateTree($file$directoryIterator->getChildren());
  111.             else {
  112.                 MediaSearch_Log::trace('Add file ' $entry->getPathname());
  113.                 $file new MediaSearch_File_File($fileInfo);
  114.             }
  115.  
  116.             $fileTree->addFile($file);
  117.         }
  118.  
  119.         return $fileTree;
  120.     }
  121. }

Documentation generated on Mon, 17 Aug 2009 14:54:20 +0200 by phpDocumentor 1.4.2