public function getPostFiles($blogKey) { $postsDir = $this->getPostsDir($blogKey); if (!$postsDir) { return array(); } $result = array(); $pathsIterator = new FilesystemIterator($postsDir); foreach ($pathsIterator as $path) { if ($path->isDir()) { continue; } $extension = pathinfo($path->getFilename(), PATHINFO_EXTENSION); if (!in_array($extension, $this->htmlExtensions)) { continue; } $matches = array(); $res = preg_match('/^(\\d{4})-(\\d{2})-(\\d{2})_(.*)\\.' . preg_quote($extension, '/') . '$/', $path->getFilename(), $matches); if (!$res) { $this->pieCrust->getEnvironment()->getLog()->warning("File '{$path->getPathname()}' is not formatted as 'YYYY-MM-DD_slug-title.{$extension}' and is ignored. Is that a typo?"); continue; } $result[] = PostInfo::fromStrings($matches[1], $matches[2], $matches[3], $matches[4], $extension, $path->getPathname()); } return $result; }
public function getPostFiles($blogKey) { $postsDir = $this->config['dir']; if (!$postsDir or !is_dir($postsDir)) { return array(); } $postsPattern = $this->getPostsPattern($blogKey); if (!$postsPattern) { return array(); } $year = date('Y'); $month = date('m'); $day = date('d'); $result = array(); $pathsIterator = new FilesystemIterator($postsDir); foreach ($pathsIterator as $path) { if ($path->isDir()) { continue; } $extension = pathinfo($path->getFilename(), PATHINFO_EXTENSION); if (!in_array($extension, $this->htmlExtensions)) { continue; } $pattern = str_replace(array('%slug%', '%ext%'), array('(.*)', preg_quote($extension, '/')), $postsPattern); $pattern = '/^' . $pattern . '$/'; $matches = array(); $res = preg_match($pattern, $path->getFilename(), $matches); if (!$res) { continue; } $result[] = PostInfo::fromStrings($year, $month, $day, $matches[1], $extension, $path->getPathname()); } return $result; }
public function getPostFiles($blogKey) { $postsDir = $this->getPostsDir($blogKey); if (!$postsDir) { return array(); } $result = array(); $years = array(); $yearsIterator = new FilesystemIterator($postsDir); foreach ($yearsIterator as $year) { if (!$year->isDir()) { continue; } if (preg_match('/^\\d{4}$/', $year->getFilename()) === false) { continue; } $thisYear = $year->getFilename(); $years[] = $thisYear; } foreach ($years as $year) { $months = array(); $monthsIterator = new FilesystemIterator($postsDir . $year); foreach ($monthsIterator as $month) { if (!$month->isDir()) { continue; } if (preg_match('/^\\d{2}$/', $month->getFilename()) === false) { continue; } $thisMonth = $month->getFilename(); $months[] = $thisMonth; } foreach ($months as $month) { $postsIterator = new FilesystemIterator($postsDir . $year . '/' . $month); foreach ($postsIterator as $path) { if ($path->isDir()) { continue; } $extension = pathinfo($path->getFilename(), PATHINFO_EXTENSION); if (!in_array($extension, $this->htmlExtensions)) { continue; } $matches = array(); if (!preg_match('/^(\\d{2})_(.*)\\.' . preg_quote($extension, '/') . '$/', $path->getFilename(), $matches)) { $this->pieCrust->getEnvironment()->getLog()->warning("File '{$path->getPathname()}' is not formatted as 'DD_slug-title.{$extension}' and is ignored. Is that a typo?"); continue; } $result[] = PostInfo::fromStrings($year, $month, $matches[1], $matches[2], $extension, $path->getPathname()); } } } return $result; }
public function testIgnoreFile() { $fs = MockFileSystem::create()->withAsset('_content/posts/no-date.html', '')->withAsset('_content/posts/2013-01-12_foo-bar.html', ''); $pc = new MockPieCrust(); $pc->setPostsDir($fs->url('kitchen/_content/posts')); $pc->getConfig()->setValue('site/posts_fs', 'flat'); $pc->getPluginLoader()->fileSystems = array(new \PieCrust\IO\FlatFileSystem(), new \PieCrust\IO\ShallowFileSystem(), new \PieCrust\IO\HierarchicalFileSystem()); $pcFs = FileSystemFactory::create($pc); $postFiles = $pcFs->getPostFiles('blog'); foreach ($postFiles as &$pf) { // Fix backslashes when running tests on Windows. $pf->path = str_replace('\\', '/', $pf->path); } $this->assertEquals(array(PostInfo::fromStrings('2013', '01', '12', 'foo-bar', 'html', $fs->url('kitchen/_content/posts/2013-01-12_foo-bar.html'))), $postFiles); }