Skip to content

Commit

Permalink
Merge pull request #22 from hotwired-laravel/tm/l11
Browse files Browse the repository at this point in the history
Adds Laravel 11 support
  • Loading branch information
tonysm authored Mar 6, 2024
2 parents 44a25ac + ee0dcad commit b0de668
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 112 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.1, 8.2]
laravel: [9.*, 10.*]
laravel: [10.*, 11.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 9.*
testbench: 7.*
- laravel: 10.*
testbench: 8.*
- laravel: 11.*
testbench: 9.*
exclude:
- laravel: 11.*
php: 8.1
- laravel: 11.*
php: 8.2
os: windows-latest

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea
.phpunit.result.cache
.phpunit.cache
build
composer.lock
coverage
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
}
],
"require": {
"php": "^8.1",
"php": "^8.1|^8.2",
"spatie/laravel-package-tools": "^1.9.2",
"illuminate/contracts": "^9.0|^10.0"
"illuminate/contracts": "^10.0|^11.0"
},
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^6.0",
"orchestra/testbench": "^7.0|^8.0",
"phpunit/phpunit": "^9.5"
"nunomaduro/collision": "^7.10|^8.1",
"orchestra/testbench": "^8.0|^9.1",
"phpunit/phpunit": "^10.5"
},
"autoload": {
"psr-4": {
Expand Down
42 changes: 12 additions & 30 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
verbose="true"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false"
bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false"
executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<testsuites>
<testsuite name="Tonysm Test Suite">
<testsuite name="Stimulus Laravel Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<logging>
<junit outputFile="build/report.junit.xml" />
</logging>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
</source>
</phpunit>
73 changes: 39 additions & 34 deletions src/Commands/Concerns/InstallsForImportmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace HotwiredLaravel\StimulusLaravel\Commands\Concerns;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;

/**
* @mixin \HotwiredLaravel\StimulusLaravel\Commands\InstallCommand
Expand All @@ -17,51 +18,55 @@ protected function installsForImportmaps()

protected function publishJsFilesForImportmaps()
{
$this->components->task('publishing JS files', function () {
File::ensureDirectoryExists(resource_path('js/controllers'));
File::ensureDirectoryExists(resource_path('js/libs'));
File::ensureDirectoryExists(resource_path('js/controllers'));
File::ensureDirectoryExists(resource_path('js/libs'));

File::copy(__DIR__.'/../../../stubs/resources/js/libs/stimulus.js', resource_path('js/libs/stimulus.js'));
File::copy(__DIR__.'/../../../stubs/resources/js/controllers/hello_controller.js', resource_path('js/controllers/hello_controller.js'));
File::copy(__DIR__.'/../../../stubs/resources/js/controllers/index-importmap.js', resource_path('js/controllers/index.js'));
File::copy(__DIR__.'/../../../stubs/resources/js/libs/stimulus.js', resource_path('js/libs/stimulus.js'));
File::copy(__DIR__.'/../../../stubs/resources/js/controllers/hello_controller.js', resource_path('js/controllers/hello_controller.js'));
File::copy(__DIR__.'/../../../stubs/resources/js/controllers/index-importmap.js', resource_path('js/controllers/index.js'));

$libsIndexFile = resource_path('js/libs/index.js');
$libsIndexSourceFile = __DIR__.'/../../../stubs/resources/js/libs/index-importmap.js';
$libsIndexFile = resource_path('js/libs/index.js');
$libsIndexSourceFile = __DIR__.'/../../../stubs/resources/js/libs/index-importmap.js';

if (File::exists($libsIndexFile)) {
$importLine = trim(File::get($libsIndexSourceFile));
if (File::exists($libsIndexFile)) {
$importLine = trim(File::get($libsIndexSourceFile));

if (! str_contains(File::get($libsIndexFile), $importLine)) {
File::append($libsIndexFile, PHP_EOL.$importLine.PHP_EOL);
}
} else {
File::copy($libsIndexSourceFile, $libsIndexFile);
if (! str_contains(File::get($libsIndexFile), $importLine)) {
File::append($libsIndexFile, PHP_EOL.$importLine.PHP_EOL);
}

return true;
});
} else {
File::copy($libsIndexSourceFile, $libsIndexFile);
}
}

protected function registerImportmapPins()
{
$this->components->task('pinning JS dependency (importmap)', function () {
$this->callSilently('importmap:pin', [
'packages' => collect($this->jsPackages())
->map(fn ($version, $package) => "{$package}@{$version}")
->values()
->all(),
]);

// Publishes the `@hotwired/stimulus-loading` package to public/
$this->callSilently('vendor:publish', [
'--tag' => 'stimulus-laravel-assets',
]);
$dependencies = collect($this->jsPackages())
->map(fn ($version, $package) => "{$package}@{$version}")
->values()
->all();

File::append($this->importmapsFile(), <<<'IMPORTMAP'
Importmap::pin("@hotwired/stimulus-loading", to: "vendor/stimulus-laravel/stimulus-loading.js", preload: true);
IMPORTMAP);
Process::forever()->run(array_merge([
$this->phpBinary(),
'artisan',
'importmap:pin',
], $dependencies), function ($_type, $output) {
$this->output->write($output);
});

return true;
// Publishes the `@hotwired/stimulus-loading` package to public/vendor
Process::forever()->run([
$this->phpBinary(),
'artisan',
'vendor:publish',
'--tag',
'stimulus-laravel-assets',
], function ($_type, $output) {
$this->output->write($output);
});

File::append($this->importmapsFile(), <<<'IMPORTMAP'
Importmap::pin("@hotwired/stimulus-loading", to: "vendor/stimulus-laravel/stimulus-loading.js", preload: true);
IMPORTMAP);
}
}
56 changes: 24 additions & 32 deletions src/Commands/Concerns/InstallsForNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,41 @@ protected function installsForNode()

protected function publishJsFilesForNode()
{
$this->components->task('publishing JS files', function () {
File::ensureDirectoryExists(resource_path('js/controllers'));
File::ensureDirectoryExists(resource_path('js/libs'));
File::ensureDirectoryExists(resource_path('js/controllers'));
File::ensureDirectoryExists(resource_path('js/libs'));

File::copy(__DIR__.'/../../../stubs/resources/js/libs/stimulus.js', resource_path('js/libs/stimulus.js'));
File::copy(__DIR__.'/../../../stubs/resources/js/controllers/hello_controller.js', resource_path('js/controllers/hello_controller.js'));
File::copy(__DIR__.'/../../../stubs/resources/js/controllers/index-node.js', resource_path('js/controllers/index.js'));
File::copy(__DIR__.'/../../../stubs/resources/js/libs/stimulus.js', resource_path('js/libs/stimulus.js'));
File::copy(__DIR__.'/../../../stubs/resources/js/controllers/hello_controller.js', resource_path('js/controllers/hello_controller.js'));
File::copy(__DIR__.'/../../../stubs/resources/js/controllers/index-node.js', resource_path('js/controllers/index.js'));

$libsIndexFile = resource_path('js/libs/index.js');
$libsIndexSourceFile = __DIR__.'/../../../stubs/resources/js/libs/index-node.js';
$libsIndexFile = resource_path('js/libs/index.js');
$libsIndexSourceFile = __DIR__.'/../../../stubs/resources/js/libs/index-node.js';

if (File::exists($libsIndexFile)) {
$importLine = trim(File::get($libsIndexSourceFile));
if (File::exists($libsIndexFile)) {
$importLine = trim(File::get($libsIndexSourceFile));

if (! str_contains(File::get($libsIndexFile), $importLine)) {
File::append($libsIndexFile, $importLine.PHP_EOL);
}
} else {
File::copy($libsIndexSourceFile, $libsIndexFile);
if (! str_contains(File::get($libsIndexFile), $importLine)) {
File::append($libsIndexFile, $importLine.PHP_EOL);
}
} else {
File::copy($libsIndexSourceFile, $libsIndexFile);
}

if (! str_contains(File::get(resource_path('js/app.js')), "import './libs';")) {
File::append(resource_path('js/app.js'), <<<'JS'
import './libs';

JS);
}
if (! str_contains(File::get(resource_path('js/app.js')), "import './libs';")) {
File::append(resource_path('js/app.js'), <<<'JS'
import './libs';

return true;
});
JS);
}
}

protected function updateNpmPackagesForNode()
{
$this->components->task('registering NPM dependency', function () {
$this->updateNodePackages(function ($packages) {
return array_merge(
$packages,
$this->jsPackages(),
);
});

return true;
$this->updateNodePackages(function ($packages) {
return array_merge(
$packages,
$this->jsPackages(),
);
});
}

Expand Down
16 changes: 10 additions & 6 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use RuntimeException;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

class InstallCommand extends Command
Expand All @@ -20,8 +21,6 @@ class InstallCommand extends Command

public function handle(): int
{
$this->components->info('Installing Stimulus Laravel');

if ($this->usingImportmaps()) {
$this->installsForImportmaps();
} else {
Expand All @@ -37,8 +36,8 @@ public function handle(): int
}

$this->newLine();
$this->components->info('Done');
$this->newLine();

$this->components->info('Stimulus Laravel was installed successfully.');

return self::SUCCESS;
}
Expand Down Expand Up @@ -74,13 +73,18 @@ protected function runCommands($commands)
});
}

protected function usingImportmaps(): bool
private function usingImportmaps(): bool
{
return File::exists($this->importmapsFile());
}

protected function importmapsFile(): string
private function importmapsFile(): string
{
return base_path('routes/importmap.php');
}

protected function phpBinary()
{
return (new PhpExecutableFinder())->find(false) ?: 'php';
}
}
4 changes: 2 additions & 2 deletions src/StimulusGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(private ?string $targetFolder = null)
$this->targetFolder ??= rtrim(resource_path('js/controllers'), '/');
}

public function create(string $name, string $stub = null, callable $replacementsCallback = null): array
public function create(string $name, ?string $stub = null, ?callable $replacementsCallback = null): array
{
$replacementsCallback ??= fn ($replacements) => $replacements;
$controllerName = $this->controllerName($name);
Expand All @@ -36,7 +36,7 @@ public function create(string $name, string $stub = null, callable $replacements
];
}

public function createStrada(string $prefix, string $name, string $bridgeName = null): array
public function createStrada(string $prefix, string $name, ?string $bridgeName = null): array
{
return $this->create("$prefix/$name", stub: __DIR__.'/../stubs/strada.stub', replacementsCallback: function (array $replacements) use ($bridgeName) {
return array_merge(
Expand Down

0 comments on commit b0de668

Please sign in to comment.