refactor: simplify logic for booting the app by stubbing the lumen application file

This commit is contained in:
Feek 2020-04-12 14:13:20 -07:00
parent 94005e5f92
commit c15f1f770e
5 changed files with 62 additions and 86 deletions

View File

@ -1,23 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="3.x-dev@e70a282bce68adaafe00980bba017c7278d511d8">
<file src="src/AbstractPlugin.php">
<MismatchingDocblockParamType occurrences="1">
<code>\Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application|\Illuminate\Container\Container</code>
</MismatchingDocblockParamType>
<PossiblyInvalidArgument occurrences="3">
<code>$app</code>
<code>$app</code>
<code>$app</code>
</PossiblyInvalidArgument>
<UndefinedDocblockClass occurrences="2">
<code>\Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application|\Illuminate\Container\Container</code>
<code>\Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application|\Illuminate\Container\Container</code>
</UndefinedDocblockClass>
<UndefinedInterfaceMethod occurrences="2">
<code>$app</code>
<code>$app</code>
</UndefinedInterfaceMethod>
</file>
<files psalm-version="3.11.0@5bc9b095d1a6f1a2e0890353fc2c6cd7de62371d">
<file src="src/FakeMetaCommand.php">
<PropertyNotSetInConstructor occurrences="1">
<code>FakeMetaCommand</code>
@ -28,20 +10,4 @@
<code>FakeModelsCommand</code>
</PropertyNotSetInConstructor>
</file>
<file src="src/LumenPlugin.php">
<UndefinedClass occurrences="1">
<code>\Laravel\Lumen\Application</code>
</UndefinedClass>
<UndefinedDocblockClass occurrences="1">
<code>\Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application|\Illuminate\Container\Container</code>
</UndefinedDocblockClass>
</file>
<file src="src/Plugin.php">
<UndefinedDocblockClass occurrences="1">
<code>\Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application|\Illuminate\Container\Container</code>
</UndefinedDocblockClass>
<UndefinedMethod occurrences="1">
<code>getProvider</code>
</UndefinedMethod>
</file>
</files>

View File

@ -11,6 +11,7 @@
<ignoreFiles>
<directory name="vendor" />
<directory name="src/Stubs" />
<directory name="src/cache" />
<directory name="tests" />
</ignoreFiles>
</projectFiles>
@ -18,4 +19,8 @@
<issueHandlers>
<LessSpecificReturnType errorLevel="info" />
</issueHandlers>
<stubs>
<file name="src/Stubs/LumenApplication.stubphp"/>
</stubs>
</psalm>

View File

@ -23,55 +23,12 @@ class Plugin implements PluginEntryPointInterface
/** @var array<string> */
public static $model_classes = [];
/**
* Get and load ide provider for Laravel or Lumen Application container
*
* @param \Illuminate\Container\Container $app
* @param string $ide_helper_provider
* @return \Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application|\Illuminate\Container\Container
*/
protected function loadIdeProvider($app, $ide_helper_provider)
{
if ($app instanceof \Illuminate\Contracts\Foundation\Application) {
/** @var \Illuminate\Contracts\Http\Kernel $kernel */
$kernel = $app->make(\Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
// If we're running a Laravel container, let's see if we need to register the IDE helper if it isn't
// already. If we don't do this, the plugin will crash out because the IDE helper doesn't have configs
// it bootstraps present in the app container.
if (!$app->getProvider($ide_helper_provider)) {
$app->register($ide_helper_provider);
}
}
if ($app instanceof \Laravel\Lumen\Application) {
/** @var \Illuminate\Contracts\Http\Kernel $kernel */
$kernel = $app->make(\Illuminate\Contracts\Console\Kernel::class);
$app->register($ide_helper_provider);
}
return $app;
}
/**
* @return void
*/
public function __invoke(RegistrationInterface $registration, ?SimpleXMLElement $config = null)
{
$ide_helper_provider = \Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class;
if (file_exists($applicationPath = __DIR__.'/../../../../bootstrap/app.php')) { // Applications
$app = require $applicationPath;
} elseif (file_exists($applicationPath = getcwd().'/bootstrap/app.php')) { // Local Dev
$app = require $applicationPath;
} else { // Packages
$app = (new static)->createApplication();
$app->register($ide_helper_provider);
}
$app = $this->loadIdeProvider($app, $ide_helper_provider);
$app = $this->bootApp();
$fake_filesystem = new FakeFilesystem();
@ -100,7 +57,7 @@ class Plugin implements PluginEntryPointInterface
}
/**
* @param \Illuminate\Contracts\Container\Container $app
* @param \Illuminate\Foundation\Application|\Laravel\Lumen\Application $app
* @param \Illuminate\View\Factory $view_factory
*/
private function ingestFacadeStubs(
@ -131,7 +88,7 @@ class Plugin implements PluginEntryPointInterface
}
/**
* @param \Illuminate\Contracts\Container\Container $app
* @param \Illuminate\Foundation\Application|\Laravel\Lumen\Application $app
* @param \Illuminate\View\Factory $view_factory
*/
private function ingestMetaStubs(
@ -163,7 +120,7 @@ class Plugin implements PluginEntryPointInterface
}
/**
* @param \Illuminate\Contracts\Container\Container $app
* @param \Illuminate\Foundation\Application|\Laravel\Lumen\Application $app
*/
private function ingestModelStubs(
RegistrationInterface $registration,
@ -207,9 +164,7 @@ class Plugin implements PluginEntryPointInterface
}
/**
* Undocumented function
*
* @param \Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application|\Illuminate\Container\Container $app
* @param \Illuminate\Foundation\Application|\Laravel\Lumen\Application $app
* @param FakeFilesystem $fake_filesystem
* @return Factory
*/
@ -249,4 +204,28 @@ class Plugin implements PluginEntryPointInterface
$registration->addStubFile($stubFilePath);
}
}
/**
* @return \Illuminate\Foundation\Application|\Laravel\Lumen\Application
*/
private function bootApp()
{
if (file_exists($applicationPath = __DIR__.'/../../../../bootstrap/app.php')) { // Applications
$app = require $applicationPath;
} elseif (file_exists($applicationPath = getcwd().'/bootstrap/app.php')) { // Local Dev
$app = require $applicationPath;
} else { // Packages
$app = (new static)->createApplication();
}
if ($app instanceof \Illuminate\Contracts\Foundation\Application) {
$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();
} elseif ($app instanceof \Laravel\Lumen\Application) {
$app->boot();
}
$app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
return $app;
}
}

View File

@ -17,7 +17,7 @@ class ModelPropertyProvider implements
/** @return array<string, string> */
public static function getClassLikeNames() : array
{
return \Psalm\LaravelPlugin\AbstractPlugin::$model_classes;
return \Psalm\LaravelPlugin\Plugin::$model_classes;
}
/**

View File

@ -0,0 +1,26 @@
<?php
namespace Laravel\Lumen;
use Illuminate\Container\Container;
class Application extends Container
{
/**
* Register a service provider with the application.
*
* @param \Illuminate\Support\ServiceProvider|string $provider
* @return \Illuminate\Support\ServiceProvider
*/
public function register($provider)
{
}
/**
* Boots the registered providers.
*/
public function boot()
{
}
}