Introduce a vendor path

This lets us or anyone modify the path from where dependencies (usually
installed into /vendor by Composer) are loaded. We need to be able to
tweak this in our integration tests, where the application code under
test needs access to certain dependencies.
This commit is contained in:
Franz Liedke 2019-06-12 23:45:49 +02:00
parent 6e26b988bd
commit 5e1680c458
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
10 changed files with 69 additions and 26 deletions

View File

@ -80,7 +80,7 @@ class MigrateCommand extends AbstractCommand
$this->info('Publishing assets...');
$this->app->make('files')->copyDirectory(
$this->app->basePath().'/vendor/components/font-awesome/webfonts',
$this->app->vendorPath().'/components/font-awesome/webfonts',
$this->app->publicPath().'/assets/fonts'
);
}

View File

@ -67,11 +67,11 @@ class ExtensionManager
*/
public function getExtensions()
{
if (is_null($this->extensions) && $this->filesystem->exists($this->app->basePath().'/vendor/composer/installed.json')) {
if (is_null($this->extensions) && $this->filesystem->exists($this->app->vendorPath().'/composer/installed.json')) {
$extensions = new Collection();
// Load all packages installed by composer.
$installed = json_decode($this->filesystem->get($this->app->basePath().'/vendor/composer/installed.json'), true);
$installed = json_decode($this->filesystem->get($this->app->vendorPath().'/composer/installed.json'), true);
foreach ($installed as $package) {
if (Arr::get($package, 'type') != 'flarum-extension' || empty(Arr::get($package, 'name'))) {
@ -326,6 +326,6 @@ class ExtensionManager
*/
protected function getExtensionsDir()
{
return $this->app->basePath().'/vendor';
return $this->app->vendorPath();
}
}

View File

@ -41,6 +41,20 @@ class Application extends Container implements ApplicationContract
*/
protected $publicPath;
/**
* The custom storage path defined by the developer.
*
* @var string
*/
protected $storagePath;
/**
* A custom vendor path to find dependencies in non-standard environments.
*
* @var string
*/
protected $vendorPath;
/**
* Indicates if the application has "booted".
*
@ -83,13 +97,6 @@ class Application extends Container implements ApplicationContract
*/
protected $deferredServices = [];
/**
* The custom storage path defined by the developer.
*
* @var string
*/
protected $storagePath;
/**
* Create a new Flarum application instance.
*
@ -226,7 +233,7 @@ class Application extends Container implements ApplicationContract
*/
protected function bindPathsInContainer()
{
foreach (['base', 'public', 'storage'] as $path) {
foreach (['base', 'public', 'storage', 'vendor'] as $path) {
$this->instance('path.'.$path, $this->{$path.'Path'}());
}
}
@ -261,6 +268,16 @@ class Application extends Container implements ApplicationContract
return $this->storagePath ?: $this->basePath.DIRECTORY_SEPARATOR.'storage';
}
/**
* Get the path to the vendor directory where dependencies are installed.
*
* @return string
*/
public function vendorPath()
{
return $this->vendorPath ?: $this->basePath.DIRECTORY_SEPARATOR.'vendor';
}
/**
* Set the storage directory.
*
@ -276,6 +293,21 @@ class Application extends Container implements ApplicationContract
return $this;
}
/**
* Set the vendor directory.
*
* @param string $path
* @return $this
*/
public function useVendorPath($path)
{
$this->vendorPath = $path;
$this->instance('path.vendor', $path);
return $this;
}
/**
* Get or check the current application environment.
*

View File

@ -100,6 +100,10 @@ class InstalledSite implements SiteInterface
$laravel->useStoragePath($this->paths['storage']);
if (isset($this->paths['vendor'])) {
$laravel->useVendorPath($this->paths['vendor']);
}
$laravel->instance('env', 'production');
$laravel->instance('flarum.config', $this->config);
$laravel->instance('config', $config = $this->getIlluminateConfig($laravel));

View File

@ -59,6 +59,10 @@ class UninstalledSite implements SiteInterface
$laravel->useStoragePath($this->paths['storage']);
if (isset($this->paths['vendor'])) {
$laravel->useVendorPath($this->paths['vendor']);
}
$laravel->instance('env', 'production');
$laravel->instance('flarum.config', []);
$laravel->instance('config', $config = $this->getIlluminateConfig());

View File

@ -30,7 +30,7 @@ class FrontendServiceProvider extends AbstractServiceProvider
);
$assets->setLessImportDirs([
$this->app->basePath().'/vendor/components/font-awesome/less' => ''
$this->app->vendorPath().'/components/font-awesome/less' => ''
]);
$assets->css([$this, 'addBaseCss']);

View File

@ -30,7 +30,8 @@ class InstallServiceProvider extends AbstractServiceProvider
return new Installation(
$this->app->basePath(),
$this->app->publicPath(),
$this->app->storagePath()
$this->app->storagePath(),
$this->app->vendorPath(),
);
});
}

View File

@ -16,6 +16,7 @@ class Installation
private $basePath;
private $publicPath;
private $storagePath;
private $vendorPath;
private $configPath;
private $debug = false;
@ -35,11 +36,12 @@ class Installation
/** @var \Illuminate\Database\ConnectionInterface */
private $db;
public function __construct($basePath, $publicPath, $storagePath)
public function __construct($basePath, $publicPath, $storagePath, $vendorPath)
{
$this->basePath = $basePath;
$this->publicPath = $publicPath;
$this->storagePath = $storagePath;
$this->vendorPath = $vendorPath;
}
public function configPath($path)
@ -137,11 +139,11 @@ class Installation
});
$pipeline->pipe(function () {
return new Steps\PublishAssets($this->basePath, $this->getAssetPath());
return new Steps\PublishAssets($this->vendorPath, $this->getAssetPath());
});
$pipeline->pipe(function () {
return new Steps\EnableBundledExtensions($this->db, $this->basePath, $this->getAssetPath());
return new Steps\EnableBundledExtensions($this->db, $this->vendorPath, $this->getAssetPath());
});
return $pipeline;

View File

@ -32,17 +32,17 @@ class EnableBundledExtensions implements Step
/**
* @var string
*/
private $basePath;
private $vendorPath;
/**
* @var string
*/
private $assetPath;
public function __construct(ConnectionInterface $database, $basePath, $assetPath)
public function __construct(ConnectionInterface $database, $vendorPath, $assetPath)
{
$this->database = $database;
$this->basePath = $basePath;
$this->vendorPath = $vendorPath;
$this->assetPath = $assetPath;
}
@ -90,7 +90,7 @@ class EnableBundledExtensions implements Step
*/
private function loadExtensions()
{
$json = file_get_contents("$this->basePath/vendor/composer/installed.json");
$json = file_get_contents("$this->vendorPath/composer/installed.json");
return (new Collection(json_decode($json, true)))
->filter(function ($package) {
@ -98,7 +98,7 @@ class EnableBundledExtensions implements Step
})->filter(function ($package) {
return ! empty(Arr::get($package, 'name'));
})->map(function ($package) {
$extension = new Extension($this->basePath.'/vendor/'.Arr::get($package, 'name'), $package);
$extension = new Extension($this->vendorPath.'/'.Arr::get($package, 'name'), $package);
$extension->setVersion(Arr::get($package, 'version'));
return $extension;

View File

@ -20,16 +20,16 @@ class PublishAssets implements Step, ReversibleStep
/**
* @var string
*/
private $basePath;
private $vendorPath;
/**
* @var string
*/
private $assetPath;
public function __construct($basePath, $assetPath)
public function __construct($vendorPath, $assetPath)
{
$this->basePath = $basePath;
$this->vendorPath = $vendorPath;
$this->assetPath = $assetPath;
}
@ -41,7 +41,7 @@ class PublishAssets implements Step, ReversibleStep
public function run()
{
(new Filesystem)->copyDirectory(
"$this->basePath/vendor/components/font-awesome/webfonts",
"$this->vendorPath/components/font-awesome/webfonts",
$this->targetPath()
);
}