From 90f4f58ebd353fdd89acf4d6d7e25cfb9ec99989 Mon Sep 17 00:00:00 2001 From: Keith Damiani Date: Tue, 27 Feb 2018 08:41:27 -0500 Subject: [PATCH 1/4] Add __toString method to CollectionItem --- src/Collection/CollectionItem.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Collection/CollectionItem.php b/src/Collection/CollectionItem.php index f5bff720..88f3d22a 100644 --- a/src/Collection/CollectionItem.php +++ b/src/Collection/CollectionItem.php @@ -44,6 +44,11 @@ public function getContent() return $this->_content; } + public function __toString() + { + return (String) $this->getContent(); + } + protected function missingHelperError($functionName) { return 'No function named "' . $functionName. '" for the collection "' . $this->_meta->collectionName . '" was found in the file "config.php".'; From 304b73a3d46d4b8c0f1101cddbab6d5c8bfb848e Mon Sep 17 00:00:00 2001 From: Keith Damiani Date: Tue, 27 Feb 2018 08:57:21 -0500 Subject: [PATCH 2/4] Add resolvePath helper to replace realpath --- src/File/InputFile.php | 2 +- src/Support/helpers.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/File/InputFile.php b/src/File/InputFile.php index c6191173..eea22a0e 100644 --- a/src/File/InputFile.php +++ b/src/File/InputFile.php @@ -45,7 +45,7 @@ public function getExtraBladeExtension() public function getRelativeFilePath() { - $relative_path = str_replace(realpath($this->basePath), '', realpath($this->file->getPathname())); + $relative_path = str_replace(resolvePath($this->basePath), '', resolvePath($this->file->getPathname())); return trimPath($relative_path); } diff --git a/src/Support/helpers.php b/src/Support/helpers.php index 66be9d30..3c1abebc 100644 --- a/src/Support/helpers.php +++ b/src/Support/helpers.php @@ -19,6 +19,22 @@ function trimPath($path) return rightTrimPath(leftTrimPath($path)); } +function resolvePath($path) +{ + $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); + $segments = []; + + collect(explode(DIRECTORY_SEPARATOR, $path))->filter()->each(function ($part) use (&$segments) { + if ($part == '..') { + array_pop($segments); + } elseif ($part != '.') { + $segments[] = $part; + } + }); + + return implode(DIRECTORY_SEPARATOR, $segments); +} + /** * Get the path to the public folder. */ From 120a836f5848959c860dad6f8e0f1ba61339626f Mon Sep 17 00:00:00 2001 From: Keith Damiani Date: Tue, 27 Feb 2018 08:57:45 -0500 Subject: [PATCH 3/4] Replace getRealPath with getPathName --- src/Handlers/BladeHandler.php | 2 +- src/Handlers/DefaultHandler.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Handlers/BladeHandler.php b/src/Handlers/BladeHandler.php index ff785a46..2ee54860 100644 --- a/src/Handlers/BladeHandler.php +++ b/src/Handlers/BladeHandler.php @@ -49,7 +49,7 @@ private function buildOutput($file, $pageData) $extension == 'php' ? 'html' : $extension, $this->hasFrontMatter ? $this->renderWithFrontMatter($file, $pageData) : - $this->render($file->getRealPath(), $pageData), + $this->render($file->getPathName(), $pageData), $pageData ) ]); diff --git a/src/Handlers/DefaultHandler.php b/src/Handlers/DefaultHandler.php index b08277e0..4b7e34c8 100644 --- a/src/Handlers/DefaultHandler.php +++ b/src/Handlers/DefaultHandler.php @@ -21,7 +21,7 @@ public function handle($file, $pageData) { return [ new CopyFile( - $file->getRealPath(), + $file->getPathName(), $file->getRelativePath(), $file->getBasename('.' . $file->getExtension()), $file->getExtension(), From eb1ecd03c3c482dbac9fd0f526383c517e224444 Mon Sep 17 00:00:00 2001 From: Keith Damiani Date: Tue, 27 Feb 2018 08:58:58 -0500 Subject: [PATCH 4/4] Add test for collection item toString --- composer.json | 1 + tests/CollectionItemTest.php | 57 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/CollectionItemTest.php diff --git a/composer.json b/composer.json index d7eeabc7..bb8337b5 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "jigsaw" ], "require-dev": { + "mikey179/vfsStream": "^1.6", "phpunit/phpunit": "~7.0" } } diff --git a/tests/CollectionItemTest.php b/tests/CollectionItemTest.php new file mode 100644 index 00000000..efa7492a --- /dev/null +++ b/tests/CollectionItemTest.php @@ -0,0 +1,57 @@ + ['collection' => []]]); + $yaml_header = implode("\n", ['---', 'section: content', '---']); + $vfs = vfsStream::setup('virtual', null, [ + 'source' => [ + '_collection' => [ + 'item.md' => $yaml_header . '### Collection Item Content', + ], + 'test_get_content.blade.php' => '
{!! $collection->first()->getContent() !!}
', + 'test_to_string.blade.php' => '
{!! $collection->first() !!}
', + ], + ]); + + $this->buildSite($config, $vfs); + + $this->assertEquals( + $vfs->getChild('build/test_get_content.html')->getContent(), + $vfs->getChild('build/test_to_string.html')->getContent() + ); + $this->assertEquals( + '

Collection Item Content

', + $vfs->getChild('build/test_to_string.html')->getContent() + ); + } + + protected function buildSite($config = [], $vfs) + { + $this->app->config = $config; + $this->app->buildPath = [ + 'source' => $vfs->url() . '/source', + 'destination' => $vfs->url() . '/build', + ]; + $this->app->make(SiteBuilder::class)->build( + $this->app->buildPath['source'], + $this->app->buildPath['destination'], + $site_data = $this->buildSiteData($config, $vfs->url() . '/source') + ); + + return $site_data; + } + + protected function buildSiteData($config, $source_url) + { + return $this->app->make(DataLoader::class)->load($source_url, $config); + } +}