diff --git a/config.php b/config.php index b77d917..f3742b9 100644 --- a/config.php +++ b/config.php @@ -7,6 +7,7 @@ return [ 'code_dir' => __DIR__ . '/code', 'templates_dir' => __DIR__ . '/assets/templates', 'output_dir' => __DIR__ . '/.build', + 'files_dir' => __DIR__ . '/assets/files', 'pages' => [ Page::create('index', 'index.phtml'), Page::create('credits', 'credits.phtml'), diff --git a/src/Build.php b/src/Build.php index 50b8d5e..2dda936 100644 --- a/src/Build.php +++ b/src/Build.php @@ -15,7 +15,9 @@ class Build */ public function buildAll(): void { - $this->cleanPublicFolder(); + $this->createOutputFolder(); + $this->cleanOutputFolder(); + $this->copyFiles(); $pages = config('pages'); foreach ($pages as $page) { @@ -31,6 +33,8 @@ class Build */ public function runSingleBuild(string $name): string { + $this->createOutputFolder(); + $pages = config('pages'); foreach ($pages as $page) { @@ -47,7 +51,7 @@ class Build * * @return void */ - private function cleanPublicFolder(): void + private function cleanOutputFolder(): void { $files = glob(config('output_dir') . '/*.html'); foreach ($files as $file) { @@ -55,6 +59,33 @@ class Build } } + private function copyFiles(): void + { + $filesDir = config('files_dir'); + if (!file_exists($filesDir)) { + return; + } + + $outputDir = config('output_dir'); + foreach (glob($filesDir . '/*') as $file) { + $name = basename($file); + + copy($file, $outputDir . '/' . $name); + } + } + + /** + * Creates output folder if it does not exist + * + * @return void + */ + private function createOutputFolder(): void + { + if (!file_exists(config('output_dir'))) { + mkdir(config('output_dir')); + } + } + /** * Builds single Page into html and * outputs to public directory @@ -76,10 +107,6 @@ class Build } $output = $this->getOutput($template, $page->variables); - if (!file_exists(config('output_dir'))) { - mkdir(config('output_dir')); - } - file_put_contents(config('output_dir') . '/' . $page->name . '.html', $output); return $output; diff --git a/test/BuildTest.php b/test/BuildTest.php index 5003a4c..6ffae18 100644 --- a/test/BuildTest.php +++ b/test/BuildTest.php @@ -9,7 +9,6 @@ class BuildTest extends BaseTestCase public function setUp() { load_config(__DIR__ . '/static/config.php'); - mkdir('/tmp/apprentice_output'); } public function tearDown() @@ -53,5 +52,7 @@ class BuildTest extends BaseTestCase $this->assertEquals($expectedHtml, $html); $this->assertEquals($expectedHtml2, $html2); + $this->assertTrue(file_exists('/tmp/apprentice_output/test.txt')); + $this->assertTrue(file_exists('/tmp/apprentice_output/TEST')); } } diff --git a/test/static/config.php b/test/static/config.php index 5f8b618..ea3bdaa 100644 --- a/test/static/config.php +++ b/test/static/config.php @@ -7,6 +7,7 @@ return [ 'code_dir' => __DIR__ . '/code', 'templates_dir' => __DIR__ . '/templates', 'output_dir' => '/tmp/apprentice_output', + 'files_dir' => __DIR__ . '/files', 'pages' => [ Page::create('index', 'index.phtml'), Page::create('test', null, 'test.php', [ diff --git a/test/static/files/TEST b/test/static/files/TEST new file mode 100644 index 0000000..e69de29 diff --git a/test/static/files/test.txt b/test/static/files/test.txt new file mode 100644 index 0000000..e69de29