Skip to content

Commit

Permalink
Move upgrade calls into Upgrader class, and add upgrade to check for …
Browse files Browse the repository at this point in the history
…old custom drivers
  • Loading branch information
mattstauffer committed Dec 21, 2022
1 parent ab8c4e8 commit 9b09621
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 19 deletions.
61 changes: 61 additions & 0 deletions cli/Valet/Upgrader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Valet;

use Configuration;
use Site;

class Upgrader
{
public function __construct(public Filesystem $files)
{
}

/**
* Relocate config dir to ~/.config/valet/ if found in old location.
*
* @return void
*/
public function relocateOldConfig()
{
if (is_dir(VALET_LEGACY_HOME_PATH) && ! is_dir(VALET_HOME_PATH)) {
Configuration::createConfigurationDirectory();
}
}

public function pruneMissingDirectories()
{
Configuration::prune();
}

public function pruneSymbolicLinks()
{
Site::pruneLinks();
}

public function fixOldSampleValetDriver()
{
$samplePath = VALET_HOME_PATH.'/Drivers/SampleValetDriver.php';

if ($this->files->exists($samplePath)) {
if (! str_contains($this->files->get($samplePath), 'namespace')) {
$this->files->putAsUser(
VALET_HOME_PATH.'/Drivers/SampleValetDriver.php',
$this->files->getStub('SampleValetDriver.php')
);
}
}
}

public function errorIfOldCustomDrivers()
{
$driversPath = VALET_HOME_PATH.'/Drivers';

foreach ($this->files->scanDir($driversPath) as $driver) {
if (! str_contains($this->files->get($driversPath.'/'.$driver), 'namespace')) {
warning('Please make sure all custom drivers have been upgraded for Valet 4.');
exit;
}
}
}
}
28 changes: 9 additions & 19 deletions cli/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Valet\Drivers\ValetDriver;

use function Valet\info;
use function Valet\output;
use function Valet\table;
use function Valet\warning;
use function Valet\writer;

$version = '4.0.0';

/**
* Load correct autoloader depending on install location.
*/
Expand All @@ -26,39 +29,26 @@
require_once getenv('HOME').'/.composer/vendor/autoload.php';
}

/**
* Relocate config dir to ~/.config/valet/ if found in old location.
*/
if (is_dir(VALET_LEGACY_HOME_PATH) && ! is_dir(VALET_HOME_PATH)) {
Configuration::createConfigurationDirectory();
}

/**
* Create the application.
*/
Container::setInstance(new Container);

$version = '4.0.0';

$app = new Application('Laravel Valet', $version);

$dispatcher = new EventDispatcher();
$app->setDispatcher($dispatcher);
$app->setDispatcher($dispatcher = new EventDispatcher());

$dispatcher->addListener(
ConsoleEvents::COMMAND,
function (ConsoleCommandEvent $event) {
writer($event->getOutput());
});

/**
* Prune missing directories and symbolic links on every command.
*/
if (is_dir(VALET_HOME_PATH)) {
Configuration::prune();

Site::pruneLinks();
}
Upgrader::relocateOldConfig();
Upgrader::pruneMissingDirectories();
Upgrader::pruneSymbolicLinks();
Upgrader::fixOldSampleValetDriver();
Upgrader::errorIfOldCustomDrivers();

/**
* Install Valet and any required services.
Expand Down
3 changes: 3 additions & 0 deletions cli/includes/facades.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class PhpFpm extends Facade
class Site extends Facade
{
}
class Upgrader extends Facade
{
}
class Valet extends Facade
{
}
4 changes: 4 additions & 0 deletions cli/stubs/SampleValetDriver.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

namespace Valet\Drivers\Specific;

use Valet\Drivers\ValetDriver;

class SampleValetDriver extends ValetDriver
{
/**
Expand Down

0 comments on commit 9b09621

Please sign in to comment.