Skip to content

Commit 4b240c7

Browse files
committed
wip
2 parents 13f3b0f + f03683d commit 4b240c7

14 files changed

+198
-25
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22

33
All notable changes to `laravel-package-tools` will be documented in this file.
44

5+
## 1.4.3 - 2021-03-10
6+
7+
- use package shortname for publishing
8+
9+
## 1.4.2 - 2021-03-05
10+
11+
- fix publishing views (#15)
12+
13+
## 1.4.1 - 2021-03-04
14+
15+
- ensure unique timestamp on migration publish (#14)
16+
17+
## 1.4.0 - 2021-02-15
18+
19+
- allows parameters for setup methods to be passed in as a spread array (#11)
20+
21+
## 1.3.1 - 2021-02-02
22+
23+
- fix `migrationFileExists` (#7)
24+
525
## 1.3.0 - 2021-01-28
626

727
- add `hasRoute`

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ This will register your views with Laravel.
103103
If you have a view `<package root>/resources/views/myView.blade.php`, you can use it like this: `view('your-package-name::myView')`. Of course, you can also use subdirectories to organise your views. A view located at `<package root>/resources/views/subdirectory/myOtherView.blade.php` can be used with `view('your-package-name::subdirectory.myOtherView')`.
104104

105105

106-
Calling `hasViews` will also make views publishable. Users of your package will be able to publish the config file with this command:
106+
Calling `hasViews` will also make views publishable. Users of your package will be able to publish the views with this command:
107107

108108
```bash
109109
php artisan vendor:publish --tag=your-package-name-views
@@ -142,7 +142,7 @@ trans('your-package-name::translations.translatable'); // returns 'translation'
142142
If your package name starts with `laravel-` then you should leave that off in the example above.
143143

144144

145-
Calling `hasTranslations` will also make translations publishable. Users of your package will be able to publish the config file with this command:
145+
Calling `hasTranslations` will also make translations publishable. Users of your package will be able to publish the translations with this command:
146146

147147
```bash
148148
php artisan vendor:publish --tag=your-package-name-translations
@@ -160,7 +160,7 @@ $package
160160
->hasAssets();
161161
```
162162

163-
Users of your package will be able to publish the config file with this command:
163+
Users of your package will be able to publish the assets with this command:
164164

165165
```bash
166166
php artisan vendor:publish --tag=your-package-name-assets
@@ -190,7 +190,7 @@ $package
190190
->hasMigrations(['my_package_tables', 'some_other_migration']);
191191
```
192192

193-
Calling `hasMigration` will also make migrations publishable. Users of your package will be able to publish the config file with this command:
193+
Calling `hasMigration` will also make migrations publishable. Users of your package will be able to publish the migrations with this command:
194194

195195
```bash
196196
php artisan vendor:publish --tag=your-package-name-migrations

src/Package.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public function hasConfigFile(string $configFileName = null): self
3838
return $this;
3939
}
4040

41+
public function shortName(): string
42+
{
43+
return Str::after($this->name, 'laravel-');
44+
}
45+
4146
public function hasViews(): self
4247
{
4348
$this->hasViews = true;
@@ -66,9 +71,12 @@ public function hasMigration(string $migrationFileName): self
6671
return $this;
6772
}
6873

69-
public function hasMigrations(array $migrationFileNames): self
74+
public function hasMigrations(...$migrationFileNames): self
7075
{
71-
$this->migrationFileNames = array_merge($this->migrationFileNames, $migrationFileNames);
76+
$this->migrationFileNames = array_merge(
77+
$this->migrationFileNames,
78+
collect($migrationFileNames)->flatten()->toArray()
79+
);
7280

7381
return $this;
7482
}
@@ -80,9 +88,9 @@ public function hasCommand(string $commandClassName): self
8088
return $this;
8189
}
8290

83-
public function hasCommands(array $commandClassNames): self
91+
public function hasCommands(...$commandClassNames): self
8492
{
85-
$this->commands = array_merge($this->commands, $commandClassNames);
93+
$this->commands = array_merge($this->commands, collect($commandClassNames)->flatten()->toArray());
8694

8795
return $this;
8896
}
@@ -94,9 +102,9 @@ public function hasRoute(string $routeFileName): self
94102
return $this;
95103
}
96104

97-
public function hasRoutes(array $routeFileNames): self
105+
public function hasRoutes(...$routeFileNames): self
98106
{
99-
$this->routeFileNames = array_merge($this->routeFileNames, $routeFileNames);
107+
$this->routeFileNames = array_merge($this->routeFileNames, collect($routeFileNames)->flatten()->toArray());
100108

101109
return $this;
102110
}
@@ -116,9 +124,4 @@ public function setBasePath(string $path): self
116124

117125
return $this;
118126
}
119-
120-
public function shortName(): string
121-
{
122-
return Str::after($this->name, 'laravel-');
123-
}
124127
}

src/PackageServiceProvider.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ public function boot()
4949

5050
if ($this->package->hasViews) {
5151
$this->publishes([
52-
$this->package->basePath('/../resources/views') => base_path("resources/views/vendor/{$this->package->name}"),
52+
$this->package->basePath('/../resources/views') => base_path("resources/views/vendor/{$this->package->shortName()}"),
5353
], "{$this->package->shortName()}-views");
5454
}
5555

56+
$now = now();
5657
foreach ($this->package->migrationFileNames as $migrationFileName) {
5758
if (! $this->migrationFileExists($migrationFileName)) {
5859
$this->publishes([
59-
$this->package->basePath("/../database/migrations/{$migrationFileName}.php.stub") => database_path('migrations/' . now()->format('Y_m_d_His') . '_' . Str::finish($migrationFileName, '.php')),
60+
$this->package->basePath("/../database/migrations/{$migrationFileName}.php.stub") => database_path('migrations/' . $now->addSecond()->format('Y_m_d_His') . '_' . Str::finish($migrationFileName, '.php')),
6061
], "{$this->package->shortName()}-migrations");
6162
}
6263
}
@@ -98,10 +99,10 @@ public function boot()
9899

99100
public static function migrationFileExists(string $migrationFileName): bool
100101
{
101-
$len = strlen($migrationFileName);
102+
$len = strlen($migrationFileName) + 4;
102103

103104
foreach (glob(database_path("migrations/*.php")) as $filename) {
104-
if ((substr($filename, -$len) === $migrationFileName)) {
105+
if ((substr($filename, -$len) === $migrationFileName . '.php')) {
105106
return true;
106107
}
107108
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Spatie\LaravelPackageTools\Tests\PackageServiceProviderTests;
4+
5+
use Spatie\LaravelPackageTools\Package;
6+
use Spatie\TestTime\TestTime;
7+
8+
class MultiplePackageMigrationsTest extends PackageServiceProviderTestCase
9+
{
10+
public function configurePackage(Package $package)
11+
{
12+
TestTime::freeze('Y-m-d H:i:s', '2020-01-01 00:00:00');
13+
14+
$package
15+
->name('laravel-package-tools')
16+
->hasMigrations(['create_laravel_package_tools_table'])
17+
->hasMigrations('create_other_laravel_package_tools_table', 'create_third_laravel_package_tools_table');
18+
}
19+
20+
/** @test */
21+
public function it_can_publish_the_migration()
22+
{
23+
$this
24+
->artisan('vendor:publish --tag=package-tools-migrations')
25+
->assertExitCode(0);
26+
27+
$this->assertFileExists(database_path('migrations/2020_01_01_000000_create_laravel_package_tools_table.php'));
28+
$this->assertFileExists(database_path('migrations/2020_01_01_000001_create_other_laravel_package_tools_table.php'));
29+
$this->assertFileExists(database_path('migrations/2020_01_01_000002_create_third_laravel_package_tools_table.php'));
30+
}
31+
}

tests/PackageServiceProviderTests/PackageCommandsTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Spatie\LaravelPackageTools\Tests\PackageServiceProviderTests;
44

55
use Spatie\LaravelPackageTools\Package;
6+
use Spatie\LaravelPackageTools\Tests\TestClasses\FourthTestCommand;
67
use Spatie\LaravelPackageTools\Tests\TestClasses\OtherTestCommand;
78
use Spatie\LaravelPackageTools\Tests\TestClasses\TestCommand;
9+
use Spatie\LaravelPackageTools\Tests\TestClasses\ThirdTestCommand;
810

911
class PackageCommandsTest extends PackageServiceProviderTestCase
1012
{
@@ -13,7 +15,8 @@ public function configurePackage(Package $package)
1315
$package
1416
->name('laravel-package-tools')
1517
->hasCommand(TestCommand::class)
16-
->hasCommands([OtherTestCommand::class]);
18+
->hasCommands([OtherTestCommand::class])
19+
->hasCommands(ThirdTestCommand::class, FourthTestCommand::class);
1720
}
1821

1922
/** @test */

tests/PackageServiceProviderTests/PackageMigrationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ public function configurePackage(Package $package)
1313

1414
$package
1515
->name('laravel-package-tools')
16-
->hasMigration('create_laravel_package_tools_table');
16+
->hasMigration('create_another_laravel_package_tools_table');
1717
}
1818

1919
/** @test */
2020
public function it_can_publish_the_migration()
2121
{
2222
$this
23-
->artisan('vendor:publish --tag=laravel-package-tools-migrations')
23+
->artisan('vendor:publish --tag=package-tools-migrations')
2424
->assertExitCode(0);
2525

26-
$this->assertFileExists(database_path('migrations/2020_01_01_000000_create_laravel_package_tools_table.php'));
26+
$this->assertFileExists(database_path('migrations/2020_01_01_000001_create_another_laravel_package_tools_table.php'));
2727
}
2828
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Spatie\LaravelPackageTools\Tests\PackageServiceProviderTests;
4+
5+
use Spatie\LaravelPackageTools\Package;
6+
use Spatie\TestTime\TestTime;
7+
8+
class PackageRoutesTest extends PackageServiceProviderTestCase
9+
{
10+
public function configurePackage(Package $package)
11+
{
12+
TestTime::freeze('Y-m-d H:i:s', '2020-01-01 00:00:00');
13+
14+
$package
15+
->name('laravel-package-tools')
16+
->hasRoutes('web', 'other');
17+
}
18+
19+
/** @test */
20+
public function it_can_load_the_route()
21+
{
22+
$response = $this->get('my-route');
23+
24+
$response->assertSeeText('my response');
25+
}
26+
27+
/** @test */
28+
public function it_can_load_multiple_route()
29+
{
30+
$adminResponse = $this->get('other-route');
31+
32+
$adminResponse->assertSeeText('other response');
33+
}
34+
}

tests/PackageServiceProviderTests/PackageViewsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public function it_can_load_the_views()
2525
public function it_can_publish_the_views()
2626
{
2727
$this
28-
->artisan('vendor:publish --tag=laravel-package-tools-views')
28+
->artisan('vendor:publish --tag=package-tools-views')
2929
->assertExitCode(0);
3030

31-
$this->assertFileExists(base_path('resources/views/vendor/laravel-package-tools/test.blade.php'));
31+
$this->assertFileExists(base_path('resources/views/vendor/package-tools/test.blade.php'));
3232
}
3333
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Spatie\LaravelPackageTools\Tests\TestClasses;
4+
5+
use Illuminate\Console\Command;
6+
7+
class FourthTestCommand extends Command
8+
{
9+
public $name = 'fourth-test-command';
10+
11+
public function handle()
12+
{
13+
$this->info('output of test command');
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Spatie\LaravelPackageTools\Tests\TestClasses;
4+
5+
use Illuminate\Console\Command;
6+
7+
class ThirdTestCommand extends Command
8+
{
9+
public $name = 'third-test-command';
10+
11+
public function handle()
12+
{
13+
$this->info('output of test command');
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateAnotherLaravelPackageToolsTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('laravel-package-tools_another-table', function (Blueprint $table) {
12+
$table->bigIncrements('id');
13+
14+
$table->timestamps();
15+
});
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateLaravelPackageToolsTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('other-laravel-package-tools_table', function (Blueprint $table) {
12+
$table->bigIncrements('id');
13+
14+
$table->timestamps();
15+
});
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateLaravelPackageToolsTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('third-laravel-package-tools_table', function (Blueprint $table) {
12+
$table->bigIncrements('id');
13+
14+
$table->timestamps();
15+
});
16+
}
17+
}

0 commit comments

Comments
 (0)