Skip to content

Commit

Permalink
Introduce a vendor path
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
franzliedke committed Jun 12, 2019
1 parent 6e26b98 commit 5e1680c
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Database/Console/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function upgrade()
$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'
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Extension/ExtensionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public function __construct(
*/
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'))) {
Expand Down Expand Up @@ -326,6 +326,6 @@ public function isEnabled($extension)
*/
protected function getExtensionsDir()
{
return $this->app->basePath().'/vendor';
return $this->app->vendorPath();
}
}
48 changes: 40 additions & 8 deletions src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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".
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -226,7 +233,7 @@ public function setPublicPath($publicPath)
*/
protected function bindPathsInContainer()
{
foreach (['base', 'public', 'storage'] as $path) {
foreach (['base', 'public', 'storage', 'vendor'] as $path) {
$this->instance('path.'.$path, $this->{$path.'Path'}());
}
}
Expand Down Expand Up @@ -261,6 +268,16 @@ public function storagePath()
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.
*
Expand All @@ -276,6 +293,21 @@ public function useStoragePath($path)
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.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Foundation/InstalledSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ private function bootLaravel(): Application

$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));
Expand Down
4 changes: 4 additions & 0 deletions src/Foundation/UninstalledSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ private function bootLaravel(): Application

$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());
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/FrontendServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function register()
);

$assets->setLessImportDirs([
$this->app->basePath().'/vendor/components/font-awesome/less' => ''
$this->app->vendorPath().'/components/font-awesome/less' => ''
]);

$assets->css([$this, 'addBaseCss']);
Expand Down
3 changes: 2 additions & 1 deletion src/Install/InstallServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function register()
return new Installation(
$this->app->basePath(),
$this->app->publicPath(),
$this->app->storagePath()
$this->app->storagePath(),
$this->app->vendorPath(),
);
});
}
Expand Down
8 changes: 5 additions & 3 deletions src/Install/Installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Installation
private $basePath;
private $publicPath;
private $storagePath;
private $vendorPath;

private $configPath;
private $debug = false;
Expand All @@ -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)
Expand Down Expand Up @@ -137,11 +139,11 @@ function ($connection) {
});

$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;
Expand Down
10 changes: 5 additions & 5 deletions src/Install/Steps/EnableBundledExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -90,15 +90,15 @@ public function run()
*/
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) {
return Arr::get($package, 'type') == 'flarum-extension';
})->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;
Expand Down
8 changes: 4 additions & 4 deletions src/Install/Steps/PublishAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -41,7 +41,7 @@ public function getMessage()
public function run()
{
(new Filesystem)->copyDirectory(
"$this->basePath/vendor/components/font-awesome/webfonts",
"$this->vendorPath/components/font-awesome/webfonts",
$this->targetPath()
);
}
Expand Down

0 comments on commit 5e1680c

Please sign in to comment.