Skip to content

Instantly share code, notes, and snippets.

@dmaksimov
Created February 9, 2022 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmaksimov/18dcab0e70c5968ee2a68b79abd60c98 to your computer and use it in GitHub Desktop.
Save dmaksimov/18dcab0e70c5968ee2a68b79abd60c98 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
use App\Services\Views\HasViews;
use Symfony\Component\Finder\Finder;
/**
* Class AddViewsManager
*/
class AddViewsManager extends \App\Upgrade
{
use HasViews;
/**
* In production, the database is backed up before upgrades are run. Since
* the sent emails tables are huge, we speed up the backup process by
* excluding those tables from the backup by default. If this upgrade is
* making changes to the sent emails tables, you may choose to backup
* those tables.
*
* @var boolean
*/
protected $backupSentEmails = false;
/**
* Run the upgrade
*/
public function run()
{
$this->migrateViewsIn(base_path('custom/views'));
}
/**
* Migrate all styles in a give directory.
* @param string $directory
*/
private function migrateViewsIn(string $directory): void
{
\App\Models\View::truncate();
$bladeFiles = (new Finder)->in($directory)->files()->filter($this->onlyBladeFiles());
foreach ($bladeFiles as $bladeFile) {
$name = $this->resolveViewName($bladeFile->getPathname(), $directory);
$bladeFileContent = file_get_contents($bladeFile->getPathname());
if (!empty($bladeFileContent)) {
\App\Models\View::create([
'name' => $name,
'content' => $bladeFileContent
]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment