Support multiple plugins directories

This commit is contained in:
Pig Fang 2019-08-19 23:06:17 +08:00
parent 5fa956dfb6
commit 4fcdfc2b7e
3 changed files with 50 additions and 5 deletions

View File

@ -84,7 +84,7 @@ class MarketController extends Controller
$url = $metadata['dist']['url'];
$filename = Arr::last(explode('/', $url));
$pluginsDir = $manager->getPluginsDir();
$pluginsDir = $manager->getPluginsDirs()->first();
$path = storage_path("packages/$name".'_'.$metadata['version'].'.zip');
try {

View File

@ -83,7 +83,11 @@ class PluginManager
});
$plugins = collect();
collect($this->filesystem->directories($this->getPluginsDir()))
$this->getPluginsDirs()
->flatMap(function ($directory) {
return $this->filesystem->directories($directory);
})
->unique()
->filter(function ($directory) {
return $this->filesystem->exists($directory.DIRECTORY_SEPARATOR.'package.json');
})
@ -365,10 +369,18 @@ class PluginManager
/**
* The plugins path.
*
* @return string
* @return Collection
*/
public function getPluginsDir()
public function getPluginsDirs()
{
return config('plugins.directory') ? realpath(config('plugins.directory')) : base_path('plugins');
$config = config('plugins.directory');
if ($config) {
return collect(preg_split('/,\s*/', $config))
->map(function ($directory) {
return realpath($directory) ?: $directory;
});
} else {
return collect([base_path('plugins')]);
}
}
}

View File

@ -496,4 +496,37 @@ class PluginManagerTest extends TestCase
$this->expectExceptionMessage('No such plugin.');
plugin_assets('nope', 'relative');
}
public function testReadMultipleDirectories()
{
$old = config('plugins.directory');
config(['plugins.directory' => '/kumiko,/reina']);
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('directories')
->with('/kumiko')
->once()
->andReturn(collect(['/a', '/b']));
$mock->shouldReceive('directories')
->with('/reina')
->once()
->andReturn(collect(['/b', '/c']));
$mock->shouldReceive('exists')
->with('/a'.DIRECTORY_SEPARATOR.'package.json')
->once()
->andReturn(false);
$mock->shouldReceive('exists')
->with('/b'.DIRECTORY_SEPARATOR.'package.json')
->once()
->andReturn(false);
$mock->shouldReceive('exists')
->with('/c'.DIRECTORY_SEPARATOR.'package.json')
->once()
->andReturn(false);
});
app('plugins')->all();
config(['plugins.directory' => $old]);
}
}