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

WIP Craft 5 Compat #18

Merged
merged 76 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
e373d67
some craft5 compat
markhuot Feb 13, 2024
6968546
bumping MySQL for Craft 5
markhuot Feb 17, 2024
6fcbb42
Removing explicit colition
markhuot Feb 17, 2024
77f7af7
phpstan fixes for 4.x
markhuot Feb 17, 2024
84e0132
phpstan fixes
markhuot Feb 17, 2024
35c1155
rerun pint and force all tests to run
markhuot Feb 17, 2024
13906f0
removing bad test
markhuot Feb 17, 2024
a1b70f6
fixing bad cache
markhuot Feb 17, 2024
8dd5b51
fixing table name for 4.x
markhuot Feb 18, 2024
da9c1af
Merge remote-tracking branch 'origin/main' into craft5
markhuot Feb 18, 2024
b317c45
accountig for missing matrix blocks
markhuot Feb 18, 2024
2ff5137
removing unecessary code
markhuot Feb 18, 2024
7303697
abstracting version check
markhuot Feb 18, 2024
a57c081
avoiding Craft 4-isms while testing Craft 5
markhuot Feb 18, 2024
1df9535
changing matrix block types over to entry factories, need to back-fil…
markhuot Feb 18, 2024
35d0309
GitHub action updating docs
markhuot Feb 18, 2024
aeb0bf3
working support for old style block factories
markhuot Feb 18, 2024
57fa04a
Merge remote-tracking branch 'origin/craft5' into craft5
markhuot Feb 18, 2024
22f988b
ignoring phpunit cache
markhuot Feb 18, 2024
3197fa2
finding entry type in Craft 4 and 5
markhuot Feb 18, 2024
69e3dbf
fixing missing first/bad return
markhuot Feb 18, 2024
feff0bc
relaxing types
markhuot Feb 18, 2024
88c0dd7
skips on the right versions
markhuot Feb 18, 2024
1689edf
blocks work across 4 and 5
markhuot Feb 18, 2024
348fda2
fixing flipped version constraints
markhuot Feb 18, 2024
f01ae5b
pint
markhuot Feb 18, 2024
2699393
skipping old files in craft 5
markhuot Feb 18, 2024
dfa47b3
separate configs per schema
markhuot Feb 18, 2024
abafb54
fixing matrix field factories in craft 5
markhuot Mar 10, 2024
030c2e9
passing craft 5 tests
markhuot Mar 20, 2024
90738e7
phpstan update for craft 4
markhuot Mar 20, 2024
6061036
phpstan update for craft 4
markhuot Mar 20, 2024
8c3c206
Remove unused helpers from Matrix factories
markhuot Mar 20, 2024
cae7016
Fix factory class in matrix block test
markhuot Mar 20, 2024
c0044df
craft seems to have force-updated their tags. updating our version co…
markhuot Mar 20, 2024
6aa2833
refactor to use sectionserviceinterface so it satisfies PHPStan on v4…
markhuot Mar 20, 2024
9c91a16
fixing phpstan version constraints
markhuot Mar 20, 2024
d5b7ae3
fixing semver constraints
markhuot Mar 20, 2024
c29b60b
Merge branch 'refs/heads/main' into craft5
markhuot Mar 20, 2024
0ed273c
replacing composer constraint
markhuot Mar 20, 2024
1df3583
fixing project config for beta10
markhuot Mar 20, 2024
05d6e5f
phpstan fixes
markhuot Mar 20, 2024
cf9a502
allowing var_dump output for easier adoption
markhuot Mar 20, 2024
53ccf4f
rerun pint
markhuot Mar 20, 2024
0bfde23
removing old helper
markhuot Mar 21, 2024
684f4b8
removing old helper and reusing name
markhuot Mar 21, 2024
b8ee49f
removing unused code
markhuot Mar 21, 2024
e030918
bringing back types for factories
markhuot Mar 21, 2024
d162428
replacing missing import
markhuot Mar 21, 2024
ff835b1
pint
markhuot Mar 21, 2024
2b16314
v4 v.s. v5 method name
markhuot Mar 21, 2024
e10c68f
v4 v.s. v5 method name
markhuot Mar 22, 2024
81343e1
Update composer.json
bencroker Apr 8, 2024
ee96a97
Update composer.json
bencroker Apr 8, 2024
f1f032a
Update composer.json
bencroker Apr 8, 2024
cd7edd6
Merge pull request #21 from bencroker/craft5
markhuot Apr 16, 2024
62ce5e0
Update FactoryFields.twig
bencroker May 3, 2024
65c0612
Update FactoryFields.twig
bencroker May 3, 2024
4f96b5a
Update FactoryFields.twig
bencroker May 3, 2024
96d1261
Update FactoryFields.twig
bencroker May 3, 2024
af34e1d
fixes for Craft 4
markhuot May 15, 2024
1405f46
Merge branch 'main' into craft5
markhuot May 17, 2024
b57f4d8
Merge pull request #24 from bencroker/patch-1
markhuot May 17, 2024
417e22a
Update FactoryFields.twig
bencroker Jul 1, 2024
e93c34e
Merge branch 'refs/heads/main' into craft5
markhuot Sep 27, 2024
85c31e4
phpstan adjustment
markhuot Sep 30, 2024
981d5cf
pint
markhuot Sep 30, 2024
62ac020
pint
markhuot Sep 30, 2024
b5a79bf
Merge branch 'craft5' into patch-1
markhuot Sep 30, 2024
c1a784e
Merge pull request #27 from bencroker/patch-1
markhuot Sep 30, 2024
43bef3d
only infer entry types if a section is defined.
markhuot Sep 30, 2024
30ed0f5
fixing improper null check
markhuot Sep 30, 2024
a7989d0
using safeEmail instead of potentially real email addresses
markhuot Sep 30, 2024
6267702
update edition to fix user tests
markhuot Sep 30, 2024
4757dbb
update to pest 3
markhuot Sep 30, 2024
4615a9f
bumping to 8.3
markhuot Sep 30, 2024
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
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
Loading