Skip to content

Commit

Permalink
upgrade to CakePHP 5 (#593)
Browse files Browse the repository at this point in the history
upgrade to CakePHP 5
  • Loading branch information
arusinowski authored Jun 26, 2023
1 parent cdd3bf2 commit 89b54ef
Show file tree
Hide file tree
Showing 23 changed files with 268 additions and 243 deletions.
3 changes: 1 addition & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ root = true
[*]
indent_style = space
indent_size = 4
charset = "utf-8"
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.yml]
indent_style = space
indent_size = 2

[*.neon]
Expand Down
5 changes: 5 additions & 0 deletions .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpstan" version="1.10.14" installed="1.10.14" location="./tools/phpstan" copy="false"/>
<phar name="psalm" version="5.10.0" installed="5.10.0" location="./tools/psalm" copy="false"/>
</phive>
32 changes: 25 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,34 @@
}
],
"require": {
"cakephp/orm": "^4.0.2",
"league/flysystem": "^2.2|^3.0"
"php": ">=8.1",
"cakephp/orm": "5.x-dev",
"league/flysystem": "^3.15.1.0"
},
"require-dev": {
"cakephp/cakephp": "^4.0.2",
"phpunit/phpunit": "^8.5 || ^9.3",
"cakephp/cakephp-codesniffer": "^4.0",
"league/flysystem-memory": "^2.0|^3.0",
"mikey179/vfsstream": "^1.6.10"
"cakephp/cakephp": "5.x-dev",
"phpunit/phpunit": "^10.1.0",
"cakephp/cakephp-codesniffer": "^5.0",
"league/flysystem-memory": "^3.15",
"mikey179/vfsstream": "^1.6.10",
"cakephp/migrations": "4.x-dev"
},
"scripts": {
"cs-check": "phpcs --colors --parallel=16 -p src/ tests/",
"cs-fix": "phpcbf --colors --parallel=16 -p src/ tests/",
"phpstan": "tools/phpstan analyse",
"psalm": "tools/psalm --show-info=false",
"stan": [
"@phpstan",
"@psalm"
],
"stan-baseline": "tools/phpstan --generate-baseline",
"psalm-baseline": "tools/psalm --set-baseline=psalm-baseline.xml",
"stan-setup": "phive install",
"test": "phpunit"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"Josegonzalez\\Upload\\": "src",
Expand Down
8 changes: 8 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<ruleset name="CakePHP Core">
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />

<rule ref="CakePHP" />

<arg value="s"/>
</ruleset>
45 changes: 14 additions & 31 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,40 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?><phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="./tests/bootstrap.php"
convertDeprecationsToExceptions="true"
>
<?xml version="1.0" encoding="UTF-8"?>
<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.2/phpunit.xsd" cacheDirectory=".phpunit.cache">
<coverage/>
<php>
<ini name="memory_limit" value="-1"/>
<!-- E_ALL & ~E_USER_DEPRECATED (16383)-->
<!-- E_ALL (32767) -->
<ini name="error_reporting" value="32767"/>
<ini name="apc.enable_cli" value="1"/>
<env name="FIXTURE_SCHEMA_METADATA" value="./tests/schema.php"/>
</php>

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Version Test Suite">
<directory>./tests/TestCase</directory>
<testsuite name="cakephp-upload">
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>

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

<!-- Setup a listener for fixtures -->
<listeners>
<listener
class="\Cake\TestSuite\Fixture\FixtureInjector"
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
<arguments>
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
</arguments>
</listener>
</listeners>

<extensions>
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
18 changes: 18 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<psalm
reportMixedIssues="false"
autoloader="tests/bootstrap.php"
errorLevel="7"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
8 changes: 4 additions & 4 deletions src/Database/Type/FileType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Josegonzalez\Upload\Database\Type;

use Cake\Database\DriverInterface;
use Cake\Database\Driver;
use Cake\Database\Type\BaseType;

class FileType extends BaseType
Expand All @@ -17,23 +17,23 @@ class FileType extends BaseType
* @param mixed $value The value to convert.
* @return mixed Converted value.
*/
public function marshal($value)
public function marshal(mixed $value): mixed
{
return $value;
}

/**
* @inheritDoc
*/
public function toDatabase($value, DriverInterface $driver)
public function toDatabase(mixed $value, Driver $driver): mixed
{
return $value;
}

/**
* @inheritDoc
*/
public function toPHP($value, DriverInterface $driver)
public function toPHP(mixed $value, Driver $driver): mixed
{
return $value;
}
Expand Down
4 changes: 2 additions & 2 deletions src/File/Path/Basepath/DefaultTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function basepath(): string
{
$defaultPath = 'webroot{DS}files{DS}{model}{DS}{field}{DS}';
$path = Hash::get($this->settings, 'path', $defaultPath);
if (strpos($path, '{primaryKey}') !== false) {
if (str_contains($path, '{primaryKey}')) {
if ($this->entity->isNew()) {
throw new LogicException('{primaryKey} substitution not allowed for new entities');
}
Expand All @@ -43,7 +43,7 @@ public function basepath(): string
'{microtime}' => microtime(true),
'{DS}' => DIRECTORY_SEPARATOR,
];
if (strpos($path, '{primaryKey}') !== false) {
if (str_contains($path, '{primaryKey}')) {
$replacements['{primaryKey}'] = $this->entity->get($this->table->getPrimaryKey());
}

Expand Down
28 changes: 17 additions & 11 deletions src/File/Path/DefaultProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cake\ORM\Table;
use Josegonzalez\Upload\File\Path\Basepath\DefaultTrait as BasepathTrait;
use Josegonzalez\Upload\File\Path\Filename\DefaultTrait as FilenameTrait;
use Psr\Http\Message\UploadedFileInterface;

class DefaultProcessor implements ProcessorInterface
{
Expand All @@ -18,47 +19,52 @@ class DefaultProcessor implements ProcessorInterface
*
* @var \Cake\ORM\Table
*/
protected $table;
protected Table $table;

/**
* Entity instance.
*
* @var \Cake\Datasource\EntityInterface
*/
protected $entity;
protected EntityInterface $entity;

/**
* Instance of \Psr\Http\Message\UploadedFileInterface conaining the meta info from the file.
*
* @var \Psr\Http\Message\UploadedFileInterface|string
*/
protected $data;
protected UploadedFileInterface|string $data;

/**
* Name of field
*
* @var string
*/
protected $field;
protected string $field;

/**
* Settings for processing a path
*
* @var array
*/
protected $settings;
protected array $settings;

/**
* Constructor
*
* @param \Cake\ORM\Table $table the instance managing the entity
* @param \Cake\ORM\Table $table the instance managing the entity
* @param \Cake\Datasource\EntityInterface $entity the entity to construct a path for.
* @param \Psr\Http\Message\UploadedFileInterface|string $data the data being submitted for a save or filename stored in db
* @param string $field the field for which data will be saved
* @param array $settings the settings for the current field
* @param \Psr\Http\Message\UploadedFileInterface|string $data the data being submitted for a save or filename stored in db
* @param string $field the field for which data will be saved
* @param array $settings the settings for the current field
*/
public function __construct(Table $table, EntityInterface $entity, $data, string $field, array $settings)
{
public function __construct(
Table $table,
EntityInterface $entity,
UploadedFileInterface|string $data,
string $field,
array $settings
) {
$this->table = $table;
$this->entity = $entity;
$this->data = $data;
Expand Down
10 changes: 8 additions & 2 deletions src/File/Path/Filename/DefaultTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ trait DefaultTrait
*/
public function filename(): string
{
$processor = Hash::get($this->settings, 'nameCallback', null);
$processor = Hash::get($this->settings, 'nameCallback');
if (is_callable($processor)) {
return $processor($this->table, $this->entity, $this->data, $this->field, $this->settings);
return $processor(
$this->table,
$this->entity,
$this->data,
$this->field,
$this->settings
);
}

if (is_string($this->data)) {
Expand Down
11 changes: 9 additions & 2 deletions src/File/Path/ProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Cake\Datasource\EntityInterface;
use Cake\ORM\Table;
use Psr\Http\Message\UploadedFileInterface;

interface ProcessorInterface
{
Expand All @@ -13,11 +14,17 @@ interface ProcessorInterface
*
* @param \Cake\ORM\Table $table the instance managing the entity
* @param \Cake\Datasource\EntityInterface $entity the entity to construct a path for.
* @param \Psr\Http\Message\UploadedFileInterface|string $data the data being submitted for a save or filename stored in db
* @param \Psr\Http\Message\UploadedFileInterface|string $data the data being submitted for a save or filename stored in db
* @param string $field the field for which data will be saved
* @param array $settings the settings for the current field
*/
public function __construct(Table $table, EntityInterface $entity, $data, string $field, array $settings);
public function __construct(
Table $table,
EntityInterface $entity,
string|UploadedFileInterface $data,
string $field,
array $settings
);

/**
* Returns the basepath for the current field/data combination
Expand Down
56 changes: 8 additions & 48 deletions src/File/Transformer/DefaultTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,22 @@

class DefaultTransformer implements TransformerInterface
{
/**
* Table instance.
*
* @var \Cake\ORM\Table
*/
protected $table;

/**
* Entity instance.
*
* @var \Cake\Datasource\EntityInterface
*/
protected $entity;

/**
* Array of uploaded data for this field
*
* @var \Psr\Http\Message\UploadedFileInterface
*/
protected $data;

/**
* Name of field
*
* @var string
*/
protected $field;

/**
* Settings for processing a path
*
* @var array
*/
protected $settings;

/**
* Constructor
*
* @param \Cake\ORM\Table $table the instance managing the entity
* @param \Cake\ORM\Table $table the instance managing the entity
* @param \Cake\Datasource\EntityInterface $entity the entity to construct a path for.
* @param \Psr\Http\Message\UploadedFileInterface $data the data being submitted for a save
* @param string $field the field for which data will be saved
* @param array $settings the settings for the current field
* @param string $field the field for which data will be saved
* @param array $settings the settings for the current field
*/
public function __construct(
Table $table,
EntityInterface $entity,
UploadedFileInterface $data,
string $field,
array $settings
protected Table $table,
protected EntityInterface $entity,
protected UploadedFileInterface $data,
protected string $field,
protected array $settings
) {
$this->table = $table;
$this->entity = $entity;
$this->data = $data;
$this->field = $field;
$this->settings = $settings;
}

/**
Expand Down
Loading

0 comments on commit 89b54ef

Please sign in to comment.