Commits

Ludovic Chabant committed 308e7dc

Moved utility functions from `FileSystem` to `PathHelper`.
Cleaned using statements.

Comments (0)

Files changed (22)

_piecrust/src/PieCrust/Baker/PageBaker.php

 use PieCrust\IPage;
 use PieCrust\PieCrustException;
 use PieCrust\Page\PageRenderer;
-use PieCrust\IO\FileSystem;
 use PieCrust\Util\PageHelper;
+use PieCrust\Util\PathHelper;
 
 
 /**
         }
         
         // Copy the page.
-        FileSystem::ensureDirectory(dirname($bakePath));
+        PathHelper::ensureDirectory(dirname($bakePath));
         file_put_contents($bakePath, $bakedContents);
         $this->bakedFiles[] = $bakePath;
         
             $assetPaths = $assetor->getAssetPathnames();
             if ($assetPaths != null)
             {
-                FileSystem::ensureDirectory($bakeAssetDir);
+                PathHelper::ensureDirectory($bakeAssetDir);
                 foreach ($assetPaths as $assetPath)
                 {
                     $destinationAssetPath = $bakeAssetDir . basename($assetPath);

_piecrust/src/PieCrust/Baker/PieCrustBaker.php

 use PieCrust\PieCrustDefaults;
 use PieCrust\PieCrustCacheInfo;
 use PieCrust\PieCrustException;
-use PieCrust\Environment\PageRepository;
-use PieCrust\Environment\LinkCollector;
-use PieCrust\IO\FileSystem;
 use PieCrust\Util\UriBuilder;
 use PieCrust\Util\PageHelper;
 use PieCrust\Util\PathHelper;
         if ($cleanCache)
         {
             $start = microtime(true);
-            FileSystem::deleteDirectoryContents($this->pieCrust->getCacheDir());
+            PathHelper::deleteDirectoryContents($this->pieCrust->getCacheDir());
             file_put_contents($cacheValidity['path'], $cacheValidity['hash']);
             $this->logger->info(self::formatTimed($start, 'cleaned cache (reason: ' . $cleanCacheReason . ')'));
             

_piecrust/src/PieCrust/Chef/Commands/BakeCommand.php

 use PieCrust\PieCrustException;
 use PieCrust\Baker\PieCrustBaker;
 use PieCrust\Chef\ChefContext;
-use PieCrust\IO\FileSystem;
-use PieCrust\Util\PathHelper;
 
 
 class BakeCommand extends ChefCommand

_piecrust/src/PieCrust/Chef/Commands/ImportCommand.php

 use PieCrust\IPieCrust;
 use PieCrust\Chef\ChefContext;
 use PieCrust\Interop\PieCrustImporter;
-use PieCrust\IO\FileSystem;
-use PieCrust\Util\PathHelper;
 
 
 class ImportCommand extends ChefCommand

_piecrust/src/PieCrust/Chef/Commands/InitCommand.php

 use PieCrust\PieCrustException;
 use PieCrust\PieCrustDefaults;
 use PieCrust\Chef\ChefContext;
-use PieCrust\IO\FileSystem;
+use PieCrust\Util\PathHelper;
 
 require_once 'sfYaml/lib/sfYamlDumper.php';
 
     
     protected function createDirectory($rootDir, $dir, $makeWritable = false)
     {
-        FileSystem::ensureDirectory($rootDir . $dir, $makeWritable);
+        PathHelper::ensureDirectory($rootDir . $dir, $makeWritable);
     }
     
     protected function createSystemFile($fileName, $rootDir, $destination)

_piecrust/src/PieCrust/Chef/Commands/PurgeCommand.php

 use PieCrust\IPieCrust;
 use PieCrust\PieCrustException;
 use PieCrust\Chef\ChefContext;
-use PieCrust\IO\FileSystem;
+use PieCrust\Util\PathHelper;
 
 
 class PurgeCommand extends ChefCommand
             throw new PieCrustException("The cache directory doesn't exist: {$cacheDir}");
 
         $context->getLog()->info("Purging cache: {$cacheDir}");
-        FileSystem::deleteDirectoryContents($cacheDir);
+        PathHelper::deleteDirectoryContents($cacheDir);
     }
 }
 
-

_piecrust/src/PieCrust/Chef/Commands/ServeCommand.php

 use PieCrust\IPieCrust;
 use PieCrust\PieCrustException;
 use PieCrust\Chef\ChefContext;
-use PieCrust\IO\FileSystem;
 use PieCrust\Server\PieCrustServer;
-use PieCrust\Util\PathHelper;
 
 
 class ServeCommand extends ChefCommand

_piecrust/src/PieCrust/Chef/Commands/StatsCommand.php

         $blogKeys = $pieCrust->getConfig()->getValueUnchecked('site/blogs');
         foreach ($blogKeys as $blogKey)
         {
-            $fs = FileSystem::create($pieCrust, $blogKey);
-            $postInfos = $fs->getPostFiles();
-            $postCounts[$blogKey] = count($postInfos);
+            $postCounts[$blogKey] = count($pieCrust->getEnvironment()->getPostInfos($blogKey));
         }
 
         $logger->info("Stats for '{$title}':");

_piecrust/src/PieCrust/Data/PagePropertyData.php

 use PieCrust\IPage;
 use PieCrust\IPieCrust;
 use PieCrust\PieCrustException;
-use PieCrust\IO\FileSystem;
 use PieCrust\Util\PageHelper;
-use PieCrust\Util\UriBuilder;
 
 
 class PagePropertyData implements \ArrayAccess, \Iterator

_piecrust/src/PieCrust/Environment/PageRepository.php

 use PieCrust\IPieCrust;
 use PieCrust\PieCrustDefaults;
 use PieCrust\PieCrustException;
-use PieCrust\IO\FileSystem;
 use PieCrust\Page\Page;
-use PieCrust\Util\PageHelper;
-use PieCrust\Util\UriBuilder;
 
 
 /**

_piecrust/src/PieCrust/IO/FileSystem.php

 
 /**
  * Base class for a  PieCrust file-system that provides
- * the list of blog posts in descending date order.
+ * the list of blog posts in descending date order, and
+ * the list of pages.
  *
  * It also has a couple helper functions.
  */
 abstract class FileSystem
 {
     protected $pieCrust;
-    protected $subDir;
+    protected $postsSubDir;
     
-    protected function __construct(IPieCrust $pieCrust, $subDir)
+    /**
+     * Builds a new instance of FileSystem.
+     */
+    protected function __construct(IPieCrust $pieCrust, $postsSubDir)
     {
         $this->pieCrust = $pieCrust;
         
-        if ($subDir == null) $this->subDir = '';
-        else $this->subDir = trim($subDir, '\\/') . '/';
+        if ($postsSubDir == null)
+            $this->postsSubDir = '';
+        else 
+            $this->postsSubDir = trim($postsSubDir, '\\/') . '/';
     }
 
+    /**
+     * Gets the info about all the page files in the website.
+     */
     public function getPageFiles()
     {
         $pagesDir = $this->pieCrust->getPagesDir();
         return $pages;
     }
     
+    /**
+     * Gets the info about all the post files in the website.
+     *
+     * File infos are expected to be sorted in reverse chronological
+     * order based on the day of the post.
+     */
     public abstract function getPostFiles();
     
+    /**
+     * Gets the complete info for a post file based on an incomplete
+     * one (e.g. when the URL to a post doesn't contain all the
+     * information to locate it on disk).
+     */
     public function getPostPathInfo($captureGroups)
     {
         $postsDir = $this->pieCrust->getPostsDir();
             array($year, $month, $day, $slug),
             $path
         );
-        $path = $postsDir . $this->subDir . $path;
+        $path = $postsDir . $this->postsSubDir . $path;
         
         $pathInfo = array(
             'year' => $year,
         return $pathInfo;
     }
     
+    /**
+     * Gets the posts path format.
+     */
     public abstract function getPostPathFormat();
     
-    public static function create(IPieCrust $pieCrust, $subDir = null)
+    /**
+     * Creates the appropriate implementation of `FileSystem` based
+     * on the configuration of the website.
+     */
+    public static function create(IPieCrust $pieCrust, $postsSubDir = null)
     {
-        if ($subDir == PieCrustDefaults::DEFAULT_BLOG_KEY) $subDir = null;
+        if ($postsSubDir == PieCrustDefaults::DEFAULT_BLOG_KEY)
+            $postsSubDir = null;
         $postsFs = $pieCrust->getConfig()->getValueUnchecked('site/posts_fs');
         switch ($postsFs)
         {
         case 'hierarchy':
-            return new HierarchicalFileSystem($pieCrust, $subDir);
+            return new HierarchicalFileSystem($pieCrust, $postsSubDir);
         case 'shallow':
-            return new ShallowFileSystem($pieCrust, $subDir);
+            return new ShallowFileSystem($pieCrust, $postsSubDir);
         case 'flat':
-            return new FlatFileSystem($pieCrust, $subDir);
+            return new FlatFileSystem($pieCrust, $postsSubDir);
         default:
             throw new PieCrustException("Unknown posts_fs: " . $postsFs);
         }
     }
-    
-    public static function ensureDirectory($dir, $writable = false)
-    {
-        if (!is_dir($dir))
-        {
-            if (!mkdir($dir, 0777, true))
-                throw new PieCrustException("Can't create directory: " . $dir);
-
-            if ($writable && !is_writable($dir))
-                if (!chmod($dir, 0777))
-                    throw new PieCrustException("Can't make directory '" . $dir . "' writable.");
-
-            return true;
-        }
-        return false;
-    }
-
-    public static function deleteDirectoryContents($dir, $skipPattern = null)
-    {
-        self::deleteDirectoryContentsRecursive($dir, $skipPattern, 0, '');
-    }
-    
-    private static function deleteDirectoryContentsRecursive($dir, $skipPattern, $level, $relativeParent)
-    {
-        $skippedFiles = false;
-        $files = new \FilesystemIterator($dir);
-        foreach ($files as $file)
-        {
-            $relativePathname = $file->getPathname();
-            if ($relativeParent != '')
-            {
-                $relativePathname = $relativeParent . '/' . $file->getPathname();
-            }
-
-            if ($skipPattern != null and preg_match($skipPattern, $relativePathname))
-            {
-                $skippedFiles = true;
-                continue;
-            }
-            
-            if ($file->isDir())
-            {
-                $skippedFiles |= self::deleteDirectoryContentsRecursive($file->getPathname(), $skipPattern, $level + 1, $relativePathname);
-            }
-            else
-            {
-                if (!unlink($file))
-                    throw new PieCrustException("Can't unlink file: ".$file);
-            }
-        }
-        
-        if ($level > 0 and !$skippedFiles and is_dir($dir))
-        {
-            if (!rmdir($dir))
-                throw new PieCrustException("Can't rmdir directory: ".$dir);
-        }
-        return $skippedFiles;
-    }
 }

_piecrust/src/PieCrust/IO/FlatFileSystem.php

  */
 class FlatFileSystem extends FileSystem
 {
-    public function __construct(IPieCrust $pieCrust, $subDir)
+    public function __construct(IPieCrust $pieCrust, $postsSubDir)
     {
-        FileSystem::__construct($pieCrust, $subDir);
+        FileSystem::__construct($pieCrust, $postsSubDir);
     }
     
     public function getPostFiles()
             return array();
 
         $paths = array();
-        $pathsIterator = new FilesystemIterator($this->pieCrust->getPostsDir() . $this->subDir);
+        $pathsIterator = new FilesystemIterator($this->pieCrust->getPostsDir() . $this->postsSubDir);
         foreach ($pathsIterator as $p)
         {
             if ($p->getExtension() != 'html')

_piecrust/src/PieCrust/IO/HierarchicalFileSystem.php

  */
 class HierarchicalFileSystem extends FileSystem
 {
-    public function __construct(IPieCrust $pieCrust, $subDir)
+    public function __construct(IPieCrust $pieCrust, $postsSubDir)
     {
-        FileSystem::__construct($pieCrust, $subDir);
+        FileSystem::__construct($pieCrust, $postsSubDir);
     }
     
     public function getPostFiles()
         $result = array();
         
         $years = array();
-        $yearsIterator = new DirectoryIterator($this->pieCrust->getPostsDir() . $this->subDir);
+        $yearsIterator = new DirectoryIterator($this->pieCrust->getPostsDir() . $this->postsSubDir);
         foreach ($yearsIterator as $year)
         {
             if (preg_match('/^\d{4}$/', $year->getFilename()) == false)
         foreach ($years as $year)
         {
             $months = array();
-            $monthsIterator = new DirectoryIterator($this->pieCrust->getPostsDir() . $this->subDir . $year);
+            $monthsIterator = new DirectoryIterator($this->pieCrust->getPostsDir() . $this->postsSubDir . $year);
             foreach ($monthsIterator as $month)
             {
                 if (preg_match('/^\d{2}$/', $month->getFilename()) == false)
             foreach ($months as $month)
             {
                 $days = array();
-                $postsIterator = new DirectoryIterator($this->pieCrust->getPostsDir() . $this->subDir . $year . '/' . $month);
+                $postsIterator = new DirectoryIterator($this->pieCrust->getPostsDir() . $this->postsSubDir . $year . '/' . $month);
                 foreach ($postsIterator as $post)
                 {
                     $matches = array();

_piecrust/src/PieCrust/IO/ShallowFileSystem.php

  */
 class ShallowFileSystem extends FileSystem
 {
-    public function __construct(IPieCrust $pieCrust, $subDir)
+    public function __construct(IPieCrust $pieCrust, $postsSubDir)
     {
-        FileSystem::__construct($pieCrust, $subDir);
+        FileSystem::__construct($pieCrust, $postsSubDir);
     }
     
     public function getPostFiles()
             return array();
 
         $years = array();
-        $yearsIterator = new \DirectoryIterator($this->pieCrust->getPostsDir() . $this->subDir);
+        $yearsIterator = new \DirectoryIterator($this->pieCrust->getPostsDir() . $this->postsSubDir);
         foreach ($yearsIterator as $year)
         {
             if (preg_match('/^\d{4}$/', $year->getFilename()) == false)
         foreach ($years as $year)
         {
             $posts = array();
-            $pathPattern = $this->pieCrust->getPostsDir() . $this->subDir . $year . '/' . '*.html';
+            $pathPattern = $this->pieCrust->getPostsDir() . $this->postsSubDir . $year . '/' . '*.html';
             $paths = glob($pathPattern, GLOB_ERR);
             if ($paths === false)
             {

_piecrust/src/PieCrust/Interop/PieCrustImporter.php

 use PieCrust\PieCrustDefaults;
 use PieCrust\PieCrustException;
 use PieCrust\Interop\Importers\IImporter;
-use PieCrust\IO\FileSystem;
-use PieCrust\Plugins\PluginLoader;
 
 
 /**

_piecrust/src/PieCrust/Page/PaginationIterator.php

 use \ArrayAccess;
 use PieCrust\IPage;
 use PieCrust\PieCrustException;
-use PieCrust\Environment\PageRepository;
 use PieCrust\Page\Filtering\PaginationFilter;
-use PieCrust\Util\UriBuilder;
 use PieCrust\Util\PageHelper;
 use PieCrust\Util\PieCrustHelper;
 

_piecrust/src/PieCrust/Page/Paginator.php

 use PieCrust\IPage;
 use PieCrust\PieCrustDefaults;
 use PieCrust\PieCrustException;
-use PieCrust\IO\FileSystem;
 use PieCrust\Page\Filtering\PaginationFilter;
-use PieCrust\Util\UriBuilder;
 use PieCrust\Util\PageHelper;
-use PieCrust\Util\PieCrustHelper;
 
 
 /**

_piecrust/src/PieCrust/PieCrustCacheInfo.php

 
 namespace PieCrust;
 
-use PieCrust\IO\FileSystem;
+use PieCrust\Util\PathHelper;
 
 
 /**
         if ($cleanCache && !$isCacheValid)
         {
             // Clean the cache!
-            FileSystem::deleteDirectoryContents($this->pieCrust->getCacheDir(), $this->cacheCleaningSkipPatterns);
+            PathHelper::deleteDirectoryContents($this->pieCrust->getCacheDir(), $this->cacheCleaningSkipPatterns);
             file_put_contents($cacheInfoFileName, $cacheInfo);
             $cacheValidity['is_valid'] = true;
             $cacheValidity['was_cleaned'] = true;

_piecrust/src/PieCrust/Server/PieCrustServer.php

 use PieCrust\PieCrustCacheInfo;
 use PieCrust\PieCrustException;
 use PieCrust\Baker\DirectoryBaker;
-use PieCrust\IO\FileSystem;
 use PieCrust\Runner\PieCrustErrorHandler;
 use PieCrust\Runner\PieCrustRunner;
+use PieCrust\Util\PathHelper;
 
 
 /**
             'root' => $this->rootDir,
             'cache' => true
         ));
-        FileSystem::ensureDirectory($this->bakeCacheDir);
+        PathHelper::ensureDirectory($this->bakeCacheDir);
 
         // Set-up the stupid web server.
         $this->server = new StupidHttp_WebServer($this->bakeCacheDir, $this->options['port']);

_piecrust/src/PieCrust/Util/PageHelper.php

 use PieCrust\IPage;
 use PieCrust\IPieCrust;
 use PieCrust\PieCrustDefaults;
-use PieCrust\IO\FileSystem;
-use PieCrust\Page\PageRepository;
 use PieCrust\Util\UriBuilder;
 
 

_piecrust/src/PieCrust/Util/PathHelper.php

         
         return $name;
     }
+
+    /**
+     * Makes sure the given directory exists, and optionally makes
+     * sure it's writable.
+     *
+     * Returns whether the directory was just created.
+     */
+    public static function ensureDirectory($dir, $writable = false)
+    {
+        if (!is_dir($dir))
+        {
+            if (!mkdir($dir, 0777, true))
+                throw new PieCrustException("Can't create directory: {$dir}");
+            return true;
+        }
+        else if ($writable && !is_writable($dir))
+        {
+            if (!chmod($dir, 0777))
+                throw new PieCrustException("Can't make directory '{$dir}' writable.");
+        }
+        return false;
+    }
+
+    /**
+     * Deletes the contents of a directory, but optionally skips files
+     * matching a given pattern.
+     */
+    public static function deleteDirectoryContents($dir, $skipPattern = null)
+    {
+        self::deleteDirectoryContentsRecursive($dir, $skipPattern, 0, '');
+    }
+    
+    private static function deleteDirectoryContentsRecursive($dir, $skipPattern, $level, $relativeParent)
+    {
+        $skippedFiles = false;
+        $files = new \FilesystemIterator($dir);
+        foreach ($files as $file)
+        {
+            $relativePathname = $file->getPathname();
+            if ($relativeParent != '')
+            {
+                $relativePathname = $relativeParent . '/' . $file->getPathname();
+            }
+
+            if ($skipPattern != null and preg_match($skipPattern, $relativePathname))
+            {
+                $skippedFiles = true;
+                continue;
+            }
+            
+            if ($file->isDir())
+            {
+                $skippedFiles |= self::deleteDirectoryContentsRecursive($file->getPathname(), $skipPattern, $level + 1, $relativePathname);
+            }
+            else
+            {
+                if (!unlink($file))
+                    throw new PieCrustException("Can't unlink file: ".$file);
+            }
+        }
+        
+        if ($level > 0 and !$skippedFiles and is_dir($dir))
+        {
+            if (!rmdir($dir))
+                throw new PieCrustException("Can't rmdir directory: ".$dir);
+        }
+        return $skippedFiles;
+    }
 }

_piecrust/src/PieCrust/Util/PieCrustHelper.php

 
 use PieCrust\IPieCrust;
 use PieCrust\PieCrustException;
-use PieCrust\Environment\PageRepository;
-use PieCrust\IO\FileSystem;
 
 
 /**