Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CakePHP 5.x #44

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/check-coding-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
php-versions: ['7.2']
php-versions: ['8.1']

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
php-versions: ['7.2']
php-versions: ['8.1']

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
php-versions: ['7.2', '7.4']
php-versions: ['8.1', '8.2']

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Thumbs.db
# Tool specific files #
#######################
# PHPUnit
.phpunit.result.cache
.phpunit.cache
# vim
*~
*.swp
Expand Down
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
"require": {
"ext-json": "*",
"ext-mbstring": "*",
"cakephp/cakephp": "^4.3.0"
"cakephp/cakephp": "5.x-dev"
},
"require-dev": {
"cakephp/cakephp-codesniffer": "^4.2.0",
"phpunit/phpunit": "^8.0",
"phpstan/phpstan": "^0.12.32"
"cakephp/cakephp-codesniffer": "^5.1.0",
"phpunit/phpunit": "^10.2",
"phpstan/phpstan": "^1.10.27"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -54,5 +54,7 @@
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
9 changes: 4 additions & 5 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?xml version="1.0"?>
<ruleset name="CakePHP Core">
<rule ref="./vendor/cakephp/cakephp-codesniffer/CakePHP/ruleset.xml"/>
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />

<!-- Necessary for class aliases used for backwards compat -->
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<severity>0</severity>
</rule>
<rule ref="CakePHP" />

<arg value="s"/>
</ruleset>
26 changes: 13 additions & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
cacheDirectory=".phpunit.cache">

<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
</php>

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Mix">
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>

<extensions>
<extension class="\Cake\TestSuite\Fixture\PHPUnitExtension" />
<bootstrap class="\Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>

<filter>
<whitelist>
<source>
<include>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>

</include>
</source>
</phpunit>
13 changes: 2 additions & 11 deletions src/Plugin.php → src/AssetMixPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,24 @@
use AssetMix\Command\AssetMixCommand;
use Cake\Console\CommandCollection;
use Cake\Core\BasePlugin;
use Cake\Core\PluginApplicationInterface;

/**
* Plugin for AssetMix
*/
class Plugin extends BasePlugin
class AssetMixPlugin extends BasePlugin
{
/**
* Add console commands for the plugin.
*
* @param \Cake\Console\CommandCollection<mixed> $commands The command collection to update
* @return \Cake\Console\CommandCollection<mixed>
*/
public function console($commands): CommandCollection
public function console(CommandCollection $commands): CommandCollection
{
parent::console($commands);

$commands->add('asset_mix generate', AssetMixCommand::class);

return $commands;
}

/**
* @inheritDoc
*/
public function bootstrap(PluginApplicationInterface $app): void
{
parent::bootstrap($app);
}
}
57 changes: 32 additions & 25 deletions src/Command/AssetMixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Exception;
use UnexpectedValueException;
use function Cake\I18n\__;

class AssetMixCommand extends Command
{
Expand All @@ -20,14 +22,14 @@ class AssetMixCommand extends Command
*
* @var \AssetMix\Utility\FileUtility
*/
private $filesystem;
private FileUtility $filesystem;

/**
* Preset type provided via argument.
*
* @var string|null
*/
private $preset;
private ?string $preset = null;

/**
* Directory name where all assets(js, css) files will reside.
Expand All @@ -52,7 +54,6 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
->addArgument('preset', [
'help' => __('The preset/scaffolding type. Defaults to <info>vue</info>'),
'choices' => ['bootstrap', 'vue', 'react', 'inertia-vue', 'inertia-react'],
'default' => 'vue',
])
->addOption('dir', [
'short' => 'd',
Expand Down Expand Up @@ -89,7 +90,7 @@ public function execute(Arguments $args, ConsoleIo $io)
* @param \Cake\Console\ConsoleIo $io Console input/output
* @return void
*/
private function updatePackageJsonFile($io)
private function updatePackageJsonFile(ConsoleIo $io): void
{
$path = $this->getPackageJsonPath();

Expand All @@ -103,11 +104,11 @@ private function updatePackageJsonFile($io)
/**
* Writes `package.json` file.
*
* @param array<mixed> $packages Content to write into the file.
* @param string $to Path to create the file.
* @param array<mixed> $packages Content to write into the file.
* @param string $to Path to create the file.
* @return void
*/
private function writePackageJsonFile($packages, $to)
private function writePackageJsonFile(array $packages, string $to): void
{
if (! is_string($this->preset)) {
throw new Exception('Invalid preset value');
Expand Down Expand Up @@ -136,7 +137,7 @@ private function writePackageJsonFile($packages, $to)
* @param \Cake\Console\ConsoleIo $io Console input/output
* @return void
*/
private function copyWebpackMixJsFile($args, $io)
private function copyWebpackMixJsFile(Arguments $args, ConsoleIo $io): void
{
$dirName = $args->getOption('dir');

Expand All @@ -159,7 +160,7 @@ private function copyWebpackMixJsFile($args, $io)
* @param \Cake\Console\ConsoleIo $io Console input/output
* @return void
*/
private function copyAssetsDirectory($args, $io)
private function copyAssetsDirectory(Arguments $args, ConsoleIo $io): void
{
$dirName = $args->getOption('dir');
$assetPath = ROOT . DS . $dirName;
Expand All @@ -182,7 +183,7 @@ private function copyAssetsDirectory($args, $io)
* @param string $dirName Directory name.
* @return string Updated file contents.
*/
private function setWebpackMixFileContents($filePath, $dirName)
private function setWebpackMixFileContents(string $filePath, string $dirName): string
{
$currentWebpackContents = file_get_contents($filePath);

Expand All @@ -208,7 +209,7 @@ private function setWebpackMixFileContents($filePath, $dirName)
*
* @return array<string>
*/
private function getPackageJsonPath()
private function getPackageJsonPath(): array
{
if (! is_string($this->preset)) {
throw new Exception('Invalid preset value');
Expand All @@ -227,7 +228,7 @@ private function getPackageJsonPath()
*
* @return array<mixed>
*/
private function getPackageJsonFileContentsAsArray()
private function getPackageJsonFileContentsAsArray(): array
{
if (! is_string($this->preset)) {
throw new Exception('Invalid preset value');
Expand All @@ -243,15 +244,21 @@ private function getPackageJsonFileContentsAsArray()
throw new Exception('Invalid path');
}

return json_decode((string)file_get_contents($path['from']), true);
$decoded = json_decode((string)file_get_contents($path['from']), true);

if (!$decoded) {
throw new UnexpectedValueException(sprintf('%s does not contain a valid JSON', $path['from']));
}

return (array)$decoded;
}

/**
* Returns `webpack.mix.js` file path depending on preset.
*
* @return array<string>
*/
private function getWebpackMixJsPath()
private function getWebpackMixJsPath(): array
{
if (! is_string($this->preset)) {
throw new Exception('Invalid preset value');
Expand All @@ -270,7 +277,7 @@ private function getWebpackMixJsPath()
*
* @return array<string>
*/
private function getAssetsDirPaths()
private function getAssetsDirPaths(): array
{
if (! is_string($this->preset)) {
throw new Exception('Invalid preset value');
Expand All @@ -288,10 +295,10 @@ private function getAssetsDirPaths()
* Update packages array for vue.
*
* @phpcs:disable SlevomatCodingStandard.Classes.UnusedPrivateElements
* @param array<mixed> $packages Existing packages array to update.
* @param array<mixed> $packages Existing packages array to update.
* @return array<mixed>
*/
private function updateVuePackagesArray($packages)
private function updateVuePackagesArray(array $packages): array
{
return [
'resolve-url-loader' => '^2.3.1',
Expand All @@ -306,10 +313,10 @@ private function updateVuePackagesArray($packages)
/**
* Update packages array for bootstrap.
*
* @param array<mixed> $packages Existing packages array to update.
* @param array<mixed> $packages Existing packages array to update.
* @return array<mixed>
*/
private function updateBootstrapPackagesArray($packages)
private function updateBootstrapPackagesArray(array $packages): array
{
return [
'bootstrap' => '^5.0.0',
Expand All @@ -321,10 +328,10 @@ private function updateBootstrapPackagesArray($packages)
/**
* Update packages array for react.
*
* @param array<mixed> $packages Existing packages array to update.
* @param array<mixed> $packages Existing packages array to update.
* @return array<mixed>
*/
private function updateReactPackagesArray($packages)
private function updateReactPackagesArray(array $packages): array
{
foreach ($packages as $packageName => $version) {
if (in_array($packageName, ['vue', 'vue-template-compiler'])) {
Expand All @@ -345,10 +352,10 @@ private function updateReactPackagesArray($packages)
/**
* Update packages array for inertia-vue.
*
* @param array<string> $packages Existing packages array to update.
* @param array<string> $packages Existing packages array to update.
* @return array<string>
*/
private function updateInertiavuePackagesArray($packages)
private function updateInertiavuePackagesArray(array $packages): array
{
return [
'@fullhuman/postcss-purgecss' => '^1.3.0',
Expand All @@ -367,10 +374,10 @@ private function updateInertiavuePackagesArray($packages)
/**
* Update packages array for inertia-react.
*
* @param array<string> $packages Existing packages array to update.
* @param array<string> $packages Existing packages array to update.
* @return array<string>
*/
private function updateInertiareactPackagesArray($packages)
private function updateInertiareactPackagesArray(array $packages): array
{
return [
'@babel/preset-react' => '^7.0.0',
Expand Down
Loading
Loading