From 674f4b3f9a5f7509d6dabd41fbf6ebcc03001b5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 15 Aug 2019 19:16:25 +0200 Subject: [PATCH] [1.7.0] Install command (#95) --- src/Commands/Install.php | 76 ++++++++++++++ src/Commands/TenantList.php | 10 -- src/TenancyServiceProvider.php | 2 + tests/CommandsTest.php | 180 +++++++++++++++++++++++++++++++++ 4 files changed, 258 insertions(+), 10 deletions(-) create mode 100644 src/Commands/Install.php diff --git a/src/Commands/Install.php b/src/Commands/Install.php new file mode 100644 index 00000000..de1a81b2 --- /dev/null +++ b/src/Commands/Install.php @@ -0,0 +1,76 @@ +comment('Installing stancl/tenancy...'); + $this->callSilent('vendor:publish', [ + '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', + '--tag' => 'config', + ]); + $this->info('✔️ Created config/tenancy.php'); + + file_put_contents(app_path('Http/Kernel.php'), str_replace( + 'protected $middlewarePriority = [', + "protected \$middlewarePriority = [\n \Stancl\Tenancy\Middleware\InitializeTenancy::class,", + file_get_contents(app_path('Http/Kernel.php')) + )); + $this->info('✔️ Set middleware priority'); + + file_put_contents(base_path('routes/tenant.php'), +"info('✔️ Created routes/tenant.php'); + + $this->line(''); + $this->line("This package lets you store data about tenants either in Redis or in a relational database like MySQL. If you're going to use the database storage, you need to create a tenants table."); + if ($this->confirm('Do you want to publish the default database migration?', true)) { + $this->callSilent('vendor:publish', [ + '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', + '--tag' => 'migrations', + ]); + $this->info('✔️ Created migration.'); + } + + $this->comment('✨️ stancl/tenancy installed successfully.'); + } +} diff --git a/src/Commands/TenantList.php b/src/Commands/TenantList.php index 6a8fc400..a1d2b049 100644 --- a/src/Commands/TenantList.php +++ b/src/Commands/TenantList.php @@ -20,16 +20,6 @@ class TenantList extends Command */ protected $description = 'List tenants.'; - /** - * Create a new command instance. - * - * @return void - */ - public function __construct() - { - parent::__construct(); - } - /** * Execute the console command. * diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index 4ac33bd5..76218665 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -5,6 +5,7 @@ namespace Stancl\Tenancy; use Stancl\Tenancy\Commands\Run; use Stancl\Tenancy\Commands\Seed; use Illuminate\Cache\CacheManager; +use Stancl\Tenancy\Commands\Install; use Stancl\Tenancy\Commands\Migrate; use Illuminate\Support\Facades\Route; use Stancl\Tenancy\Commands\Rollback; @@ -25,6 +26,7 @@ class TenancyServiceProvider extends ServiceProvider $this->commands([ Run::class, Seed::class, + Install::class, Migrate::class, Rollback::class, TenantList::class, diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index d26b71c1..df90a21b 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -113,4 +113,184 @@ class CommandsTest extends TestCase ->expectsOutput('foo') ->expectsOutput('xyz'); } + + /** @test */ + public function install_command_works() + { + if (! is_dir($dir = app_path('Http'))) { + mkdir($dir, 0777, true); + } + if (! is_dir($dir = base_path('routes'))) { + mkdir($dir, 0777, true); + } + + file_put_contents(app_path('Http/Kernel.php'), " [ + \App\Http\Middleware\EncryptCookies::class, + \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, + \Illuminate\Session\Middleware\StartSession::class, + // \Illuminate\Session\Middleware\AuthenticateSession::class, + \Illuminate\View\Middleware\ShareErrorsFromSession::class, + \App\Http\Middleware\VerifyCsrfToken::class, + \Illuminate\Routing\Middleware\SubstituteBindings::class, + ], + + 'api' => [ + 'throttle:60,1', + 'bindings', + ], + ]; + + /** + * The application's route middleware. + * + * These middleware may be assigned to groups or used individually. + * + * @var array + */ + protected \$routeMiddleware = [ + 'auth' => \App\Http\Middleware\Authenticate::class, + 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, + 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, + 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, + 'can' => \Illuminate\Auth\Middleware\Authorize::class, + 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, + 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, + 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, + 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, + ]; + + /** + * The priority-sorted list of middleware. + * + * This forces non-global middleware to always be in the given order. + * + * @var array + */ + protected \$middlewarePriority = [ + \Illuminate\Session\Middleware\StartSession::class, + \Illuminate\View\Middleware\ShareErrorsFromSession::class, + \App\Http\Middleware\Authenticate::class, + \Illuminate\Session\Middleware\AuthenticateSession::class, + \Illuminate\Routing\Middleware\SubstituteBindings::class, + \Illuminate\Auth\Middleware\Authorize::class, + ]; +} +"); + + $this->artisan('tenancy:install') + ->expectsQuestion('Do you want to publish the default database migration?', 'yes'); + $this->assertFileExists(base_path('routes/tenant.php')); + $this->assertFileExists(base_path('config/tenancy.php')); + $this->assertSame(" [ + \App\Http\Middleware\EncryptCookies::class, + \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, + \Illuminate\Session\Middleware\StartSession::class, + // \Illuminate\Session\Middleware\AuthenticateSession::class, + \Illuminate\View\Middleware\ShareErrorsFromSession::class, + \App\Http\Middleware\VerifyCsrfToken::class, + \Illuminate\Routing\Middleware\SubstituteBindings::class, + ], + + 'api' => [ + 'throttle:60,1', + 'bindings', + ], + ]; + + /** + * The application's route middleware. + * + * These middleware may be assigned to groups or used individually. + * + * @var array + */ + protected \$routeMiddleware = [ + 'auth' => \App\Http\Middleware\Authenticate::class, + 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, + 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, + 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, + 'can' => \Illuminate\Auth\Middleware\Authorize::class, + 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, + 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, + 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, + 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, + ]; + + /** + * The priority-sorted list of middleware. + * + * This forces non-global middleware to always be in the given order. + * + * @var array + */ + protected \$middlewarePriority = [ + \Stancl\Tenancy\Middleware\InitializeTenancy::class, + \Illuminate\Session\Middleware\StartSession::class, + \Illuminate\View\Middleware\ShareErrorsFromSession::class, + \App\Http\Middleware\Authenticate::class, + \Illuminate\Session\Middleware\AuthenticateSession::class, + \Illuminate\Routing\Middleware\SubstituteBindings::class, + \Illuminate\Auth\Middleware\Authorize::class, + ]; +} +", \file_get_contents(app_path('Http/Kernel.php'))); + } }