Skip to content

Commit

Permalink
Merge pull request #18 from markhuot/craft5
Browse files Browse the repository at this point in the history
WIP Craft 5 Compat
  • Loading branch information
markhuot authored Sep 30, 2024
2 parents 195ae93 + 4615a9f commit bf0524f
Show file tree
Hide file tree
Showing 87 changed files with 1,326 additions and 261 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ jobs:
with:
ref: ${{ github.head_ref }}

- uses: shivammathur/setup-php@v2
with:
php-version: '8.3'

- name: Validate composer.json and composer.lock
run: composer validate --strict

Expand Down
25 changes: 18 additions & 7 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ jobs:

strategy:
matrix:
version: [~4.5.0, ~4.6.0]
version:
- constraint: ~4.5.0
config: 4
- constraint: ~4.6.0
config: 4
- constraint: ~4.7.0
config: 4
- constraint: ~5.0.0
config: 5

env:
CRAFT_APP_ID: pest
Expand All @@ -37,7 +45,7 @@ jobs:

services:
mysql:
image: mysql
image: mysql:8
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: pest
Expand All @@ -54,7 +62,7 @@ jobs:

- uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
php-version: '8.3'

- name: Validate composer.json and composer.lock
run: composer validate --strict
Expand All @@ -66,18 +74,19 @@ jobs:
path: |
composer.lock
vendor
key: ${{ runner.os }}-craft-vendor-${{ hashFiles('composer.json') }}
key: ${{ runner.os }}-craft-${{ matrix.version.constraint }}-vendor-${{ hashFiles('composer.json') }}

- name: Install dependencies
run: |
composer require "craftcms/cms:${{ matrix.version }}" --prefer-dist --no-progress
composer require "craftcms/cms:${{ matrix.version.constraint }}" --prefer-dist --no-progress
./bin/post-clone.sh
- name: Copy config files
run: |
mkdir -p ./storage
cp -r ./stubs/config ./
cat config/app.php
cp -r ./stubs/config/app.php ./config/app.php
cp -r ./stubs/config/general.php ./config/general.php
cp -r ./stubs/config/project-${{ matrix.version.config }}/ ./config/project/
- name: Wait for MySQL
run: until mysqladmin ping -h 127.0.0.1 -u root -proot; do sleep 1; done
Expand All @@ -90,6 +99,8 @@ jobs:

- name: Run pint
run: ./vendor/bin/pint --test -vvv src/
if: always()

- name: Run test suite
run: ./vendor/bin/pest --compact -vvv
if: always()
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.idea
cpresources
/src/bin/index.php
.phpunit.cache
.phpunit.result.cache
/config
.env
Expand All @@ -24,4 +25,4 @@ node_modules
/templates
/node_modules
/.env.example
/cpresources
/cpresources
2 changes: 1 addition & 1 deletion bin/create-default-fs.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

$volume = $app->volumes->getVolumeByHandle('local');
if (! $volume) {
$volume = new \craft\models\Volume();
$volume = new \craft\models\Volume;
$volume->name = 'Local';
$volume->handle = 'local';
$volume->fs = $fs;
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
"symfony/css-selector": "^5.3|^6.0",
"symfony/dom-crawler": "^6.0.3",
"symfony/process": "^5.3|^6.0",
"illuminate/collections": "^8.23|^9.1|^10.0",
"pestphp/pest": "^2.26",
"pestphp/pest": "^3.0",
"vlucas/phpdotenv": "^2.4|^3.4|^5.4",
"craftcms/cms": "^4.5",
"illuminate/support": "^9.52"
"craftcms/cms": "^4.5|^5.0.0-beta.1",
"illuminate/support": "^9.52|^10.0|^11.0",
"composer/composer": "^2.7",
"composer/semver": "^3.4"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -54,7 +55,7 @@
"prefer-stable": true,
"require-dev": {
"craftcms/phpstan": "dev-main",
"craftcms/craft": "^4.0",
"craftcms/craft": "^4.0.0|^5.0.0-alpha.1",
"symfony/var-dumper": "^5.0|^6.0",
"laravel/pint": "^1.13"
}
Expand Down
2 changes: 1 addition & 1 deletion docs/factories/entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ in three ways,

If you do not pass a section, one will be created automatically.

## type($handle)
## type($identifier)
Set the entry type

## postDate(DateTime|string|int $value)
Expand Down
5 changes: 5 additions & 0 deletions phpstan-craft4.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
excludePaths:
- src/actions/RenderCraft5CompiledClasses.php
- src/factories/EntryType.php
- src/factories/MatrixFieldEntries.php
6 changes: 6 additions & 0 deletions phpstan-craft5.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
excludePaths:
- src/actions/RenderCraft4CompiledClasses.php
- src/factories/AddsMatrixBlocks.php
- src/factories/BlockType.php
- src/factories/MatrixFieldBlocks.php
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
includes:
- vendor/craftcms/phpstan/phpstan.neon
- phpstan.php

parameters:
paths:
Expand All @@ -15,3 +16,4 @@ parameters:
excludePaths:
- src/craft/*
- src/illuminate/*
- src/actions/RenderCompiledClasses.php
16 changes: 16 additions & 0 deletions phpstan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;

$config = [];

if (InstalledVersions::satisfies(new VersionParser, 'craftcms/cms', '~4.0')) {
$config['includes'][] = 'phpstan-craft4.neon';
}

if (InstalledVersions::satisfies(new VersionParser, 'craftcms/cms', '~5.0')) {
$config['includes'][] = 'phpstan-craft5.neon';
}

return $config;
8 changes: 7 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
cacheDirectory=".phpunit.cache"
beStrictAboutOutputDuringTests="false"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
Expand Down
17 changes: 17 additions & 0 deletions src/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

namespace markhuot\craftpest;

use Composer\Semver\Semver;
use Craft;
use craft\base\Field;
use craft\elements\db\ElementQuery;
use craft\elements\Entry;
use craft\events\DefineBehaviorsEvent;
use markhuot\craftpest\actions\RenderCraft4CompiledClasses;
use markhuot\craftpest\actions\RenderCraft5CompiledClasses;
use markhuot\craftpest\behaviors\ExpectableBehavior;
use markhuot\craftpest\behaviors\FieldTypeHintBehavior;
use markhuot\craftpest\behaviors\TestableElementBehavior;
use markhuot\craftpest\behaviors\TestableElementQueryBehavior;
use markhuot\craftpest\console\PestController;
use markhuot\craftpest\interfaces\RenderCompiledClassesInterface;
use markhuot\craftpest\interfaces\SectionsServiceInterface;
use yii\base\BootstrapInterface;
use yii\base\Event;

Expand Down Expand Up @@ -52,5 +57,17 @@ function (DefineBehaviorsEvent $event) {
$event->behaviors['fieldTypeHintBehavior'] = FieldTypeHintBehavior::class;
}
);

Craft::$container->set(SectionsServiceInterface::class, function () {
return Semver::satisfies(Craft::$app->version, '~5.0')
? Craft::$app->getEntries() // @phpstan-ignore-line
: Craft::$app->getSections(); // @phpstan-ignore-line
});

Craft::$container->set(RenderCompiledClassesInterface::class, function () {
return Semver::satisfies(Craft::$app->version, '~5.0')
? Craft::$container->get(RenderCraft5CompiledClasses::class) // @phpstan-ignore-line
: Craft::$container->get(RenderCraft4CompiledClasses::class); // @phpstan-ignore-line
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use craft\db\Table;
use craft\helpers\FileHelper;
use craft\helpers\StringHelper;
use markhuot\craftpest\interfaces\RenderCompiledClassesInterface;

class RenderCompiledClasses
class RenderCraft4CompiledClasses implements RenderCompiledClassesInterface
{
public function handle($forceRecreate = false)
public function handle(bool $forceRecreate = false)
{
$contentService = \Craft::$app->getContent();
$originalContentTable = $contentService->contentTable;
Expand Down
58 changes: 58 additions & 0 deletions src/actions/RenderCraft5CompiledClasses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace markhuot\craftpest\actions;

use craft\helpers\FileHelper;
use craft\helpers\StringHelper;
use markhuot\craftpest\interfaces\RenderCompiledClassesInterface;

class RenderCraft5CompiledClasses implements RenderCompiledClassesInterface
{
public function handle(bool $forceRecreate = false)
{
$this->render($forceRecreate);

return true;
}

protected function render(bool $forceRecreate)
{
$storedFieldVersion = \Craft::$app->getFields()->getFieldVersion();
$compiledClassesPath = \Craft::$app->getPath()->getCompiledClassesPath();
$fieldVersionExists = $storedFieldVersion !== null;
if (! $fieldVersionExists) {
$storedFieldVersion = StringHelper::randomString(12);
}

$compiledClassPath = $compiledClassesPath.DIRECTORY_SEPARATOR.'FactoryFields_'.$storedFieldVersion.'.php';

if (file_exists($compiledClassPath) && ! $forceRecreate) {
return false;
}

$this->cleanupOldMixins('FactoryFields_'.$storedFieldVersion.'.php');

$template = file_get_contents(__DIR__.'/../../stubs/compiled_classes/FactoryFields.twig');

$compiledClass = \Craft::$app->view->renderString($template, [
'fields' => \Craft::$app->fields->getAllFields(),
]);

file_put_contents($compiledClassPath, $compiledClass);
}

protected function cleanupOldMixins(?string $except = null)
{
$compiledClassesPath = __DIR__.'/../storage/';

FileHelper::clearDirectory($compiledClassesPath, [
'filter' => function (string $path) use ($except): bool {
$b = basename($path);

return
str_starts_with($b, 'FactoryFields') &&
($except === null || $b !== $except);
},
]);
}
}
4 changes: 2 additions & 2 deletions src/bin/generate-docs.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function parseClass(string $className)
function parseComment(string $comment)
{
preg_match_all('/@see\s+(.+)$/m', $comment, $sees);
foreach (($sees[1] ?? []) as $index => $otherClass) {
foreach ($sees[1] as $index => $otherClass) {
$comment = str_replace($sees[0][$index], 'SEE['.$otherClass.']', $comment);
}

Expand All @@ -71,7 +71,7 @@ function parseComment(string $comment)
$comment = preg_replace('/(^\s+|\s+$)/', '', $comment);

preg_match_all('/^SEE\[(.+)\]$/m', $comment, $sees);
foreach (($sees[1] ?? []) as $index => $otherClass) {
foreach ($sees[1] as $index => $otherClass) {
$comment = str_replace($sees[0][$index], "\n\n".implode("\n\n", parseClass($otherClass))."\n\n", $comment);
}

Expand Down
35 changes: 0 additions & 35 deletions src/console/IdeController.php

This file was deleted.

Loading

0 comments on commit bf0524f

Please sign in to comment.