Skip to content

Commit 81684d0

Browse files
authored
Merge pull request #1536 from hydephp/refactor-media-asset-transferring-to-a-build-task
Refactor media asset transferring to a build task
2 parents 301f103 + 7e2d3d5 commit 81684d0

8 files changed

+66
-24
lines changed

RELEASE_NOTES.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ This serves two purposes:
1010
2. At release time, you can move the Unreleased section changes into a new release version section.
1111

1212
### Added
13-
- for new features.
13+
- Added a new `\Hyde\Framework\Actions\PreBuildTasks\TransferMediaAssets` build task handle media assets transfers for site builds.
1414

1515
### Changed
1616
- Changed how the documentation search is generated, to be an `InMemoryPage` instead of a post-build task.
17+
- Media asset files are now copied using the new build task instead of the deprecated `BuildService::transferMediaAssets()` method.
1718

1819
### Deprecated
1920
- for soon-to-be removed features.
2021

2122
### Removed
2223
- Breaking: Removed the build task `\Hyde\Framework\Actions\PostBuildTasks\GenerateSearch` (see upgrade guide below)
24+
- Breaking: Removed the deprecated `\Hyde\Framework\Services\BuildService::transferMediaAssets()` method (see upgrade guide below)
2325

2426
### Fixed
2527
- for any bug fixes.
@@ -68,3 +70,17 @@ according to the information below in case you wrote custom code that interacted
6870
- In the highly unlikely event your site customizes any of the search pages by replacing them in the kernel route collection,
6971
you would now need to do that in the kernel page collection due to the search pages being generated earlier in the lifecycle.
7072
https://github.com/hydephp/develop/commit/82dc71f4a0e7b6be7a9f8d822fbebe39d2289ced
73+
74+
### Media asset transfer implementation changes
75+
76+
The internals of how media asset files are copied during the build process have been changed. For most users, this change
77+
has no impact. However, if you have previously extended this method, or called it directly from your custom code,
78+
you will need to adapt your code to use the new `TransferMediaAssets` build task.
79+
80+
For example, if you triggered the media transfer with a build service method call, use the new build task instead:
81+
82+
```php
83+
(new BuildService)->transferMediaAssets();
84+
85+
(new TransferMediaAssets())->run();
86+
```

packages/framework/src/Console/Commands/BuildSiteCommand.php

-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public function handle(): int
5050

5151
$this->runPreBuildActions();
5252

53-
$this->service->transferMediaAssets();
54-
5553
$this->service->compileStaticPages();
5654

5755
$this->runPostBuildActions();

packages/framework/src/Console/Commands/RebuildPageCommand.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Hyde\Foundation\Facades\Pages;
1010
use Hyde\Framework\Actions\StaticPageBuilder;
1111
use Hyde\Framework\Features\BuildTasks\BuildTask;
12-
use Hyde\Framework\Services\BuildService;
12+
use Hyde\Framework\Actions\PreBuildTasks\TransferMediaAssets;
1313
use Hyde\Hyde;
1414
use Hyde\Pages\BladePage;
1515
use Hyde\Pages\DocumentationPage;
@@ -37,7 +37,7 @@ class RebuildPageCommand extends Command
3737
public function handle(): int
3838
{
3939
if ($this->argument('path') === Hyde::getMediaDirectory()) {
40-
(new BuildService($this->getOutput()))->transferMediaAssets();
40+
return (new TransferMediaAssets())->run($this->output);
4141

4242
$this->info('All done!');
4343

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\Framework\Actions\PreBuildTasks;
6+
7+
use Hyde\Hyde;
8+
use Hyde\Support\Filesystem\MediaFile;
9+
use Hyde\Framework\Features\BuildTasks\PreBuildTask;
10+
use Hyde\Framework\Concerns\InteractsWithDirectories;
11+
12+
class TransferMediaAssets extends PreBuildTask
13+
{
14+
protected static string $message = 'Transferring Media Assets';
15+
16+
use InteractsWithDirectories;
17+
18+
public function handle(): void
19+
{
20+
$this->needsDirectory(Hyde::siteMediaPath());
21+
22+
$this->newLine();
23+
24+
$this->withProgressBar(MediaFile::files(), function (string $identifier): void {
25+
$sitePath = Hyde::siteMediaPath($identifier);
26+
$this->needsParentDirectory($sitePath);
27+
copy(Hyde::mediaPath($identifier), $sitePath);
28+
});
29+
30+
$this->newLine();
31+
}
32+
33+
public function printFinishMessage(): void
34+
{
35+
// We don't need a finish message for this task.
36+
}
37+
}

packages/framework/src/Framework/Services/BuildService.php

-18
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@
88
use Hyde\Foundation\Facades\Routes;
99
use Hyde\Foundation\Kernel\RouteCollection;
1010
use Hyde\Framework\Actions\StaticPageBuilder;
11-
use Hyde\Framework\Concerns\InteractsWithDirectories;
1211
use Hyde\Pages\Concerns\HydePage;
13-
use Hyde\Support\Filesystem\MediaFile;
1412
use Hyde\Support\Models\Route;
1513
use Illuminate\Console\Concerns\InteractsWithIO;
1614
use Illuminate\Console\OutputStyle;
1715

1816
use function class_basename;
1917
use function preg_replace;
2018
use function collect;
21-
use function copy;
2219

2320
/**
2421
* Moves logic from the build command to a service.
@@ -30,7 +27,6 @@
3027
class BuildService
3128
{
3229
use InteractsWithIO;
33-
use InteractsWithDirectories;
3430

3531
protected RouteCollection $router;
3632

@@ -48,20 +44,6 @@ public function compileStaticPages(): void
4844
});
4945
}
5046

51-
public function transferMediaAssets(): void
52-
{
53-
$this->needsDirectory(Hyde::siteMediaPath());
54-
55-
$this->comment('Transferring Media Assets...');
56-
$this->withProgressBar(MediaFile::files(), function (string $identifier): void {
57-
$sitePath = Hyde::siteMediaPath($identifier);
58-
$this->needsParentDirectory($sitePath);
59-
copy(Hyde::mediaPath($identifier), $sitePath);
60-
});
61-
62-
$this->newLine(2);
63-
}
64-
6547
/**
6648
* @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass
6749
*/

packages/framework/src/Framework/Services/BuildTaskService.php

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Hyde\Framework\Actions\PreBuildTasks\CleanSiteDirectory;
1414
use Hyde\Framework\Actions\PostBuildTasks\GenerateRssFeed;
1515
use Hyde\Framework\Actions\PostBuildTasks\GenerateSitemap;
16+
use Hyde\Framework\Actions\PreBuildTasks\TransferMediaAssets;
1617
use Hyde\Framework\Actions\PostBuildTasks\GenerateBuildManifest;
1718
use Illuminate\Console\OutputStyle;
1819
use Illuminate\Support\Str;
@@ -132,6 +133,7 @@ protected function makeTaskIdentifier(BuildTask $class): string
132133
private function registerFrameworkTasks(): void
133134
{
134135
$this->registerIf(CleanSiteDirectory::class, $this->canCleanSiteDirectory());
136+
$this->registerIf(TransferMediaAssets::class, $this->canTransferMediaAssets());
135137
$this->registerIf(GenerateBuildManifest::class, $this->canGenerateManifest());
136138
$this->registerIf(GenerateSitemap::class, $this->canGenerateSitemap());
137139
$this->registerIf(GenerateRssFeed::class, $this->canGenerateFeed());
@@ -142,6 +144,11 @@ private function canCleanSiteDirectory(): bool
142144
return Config::getBool('hyde.empty_output_directory', true);
143145
}
144146

147+
private function canTransferMediaAssets(): bool
148+
{
149+
return Config::getBool('hyde.transfer_media_assets', true);
150+
}
151+
145152
private function canGenerateManifest(): bool
146153
{
147154
return Config::getBool('hyde.generate_build_manifest', true);

packages/framework/tests/Unit/BuildTaskServiceUnitTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ protected function setUp(): void
3939
self::mockConfig(['hyde' => [
4040
'empty_output_directory' => false,
4141
'generate_build_manifest' => false,
42+
'transfer_media_assets' => false,
4243
]]);
4344
$this->createService();
4445
}

packages/framework/tests/Unit/RelativeLinksAcrossPagesRetainsIntegrityTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Hyde\Framework\Services\BuildService;
1010
use Illuminate\Console\OutputStyle;
1111
use Mockery;
12+
use Hyde\Framework\Actions\PreBuildTasks\TransferMediaAssets;
1213
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
1314
use Symfony\Component\Console\Helper\ProgressBar;
1415

@@ -92,7 +93,7 @@ public function test_relative_links_across_pages_retains_integrity()
9293
'write' => null,
9394
]));
9495

95-
$service->transferMediaAssets();
96+
(new TransferMediaAssets())->run();
9697
$service->compileStaticPages();
9798

9899
$this->assertSee('root', [

0 commit comments

Comments
 (0)