Skip to content

Commit

Permalink
Merge pull request #40 from TheMiller2015/cakephp-4.x
Browse files Browse the repository at this point in the history
Cakephp 4.x
  • Loading branch information
josegonzalez authored Apr 24, 2020
2 parents a39f97f + 2b04e9e commit 163b6fb
Show file tree
Hide file tree
Showing 16 changed files with 626 additions and 324 deletions.
67 changes: 33 additions & 34 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,63 +1,62 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.4

sudo: false
services:
- mysql
- postgresql

env:
matrix:
- DB=mysql db_dsn='mysql://travis@0.0.0.0/cakephp_test'
- DB=mysql db_dsn='mysql://root@127.0.0.1/cakephp_test'
- DB=pgsql db_dsn='postgres://[email protected]/cakephp_test'
- DB=sqlite db_dsn='sqlite:///:memory:'

global:
- DEFAULT=1

matrix:
allow_failures:
- php: hhvm

fast_finish: true

include:
- php: 5.6
- php: 7.2
env: PHPCS=1 DEFAULT=0

- php: 5.6
env: COVERALLS=1 DEFAULT=0
- php: 7.2
env: STATIC_ANALYSIS=1 DEFAULT=0

- php: hhvm
env: HHVM=1 DB=sqlite db_dsn='sqlite:///:memory:'

- php: hhvm
env: HHVM=1 DB=mysql db_dsn='mysql://[email protected]/cakephp_test'
- php: 7.2
env: PREFER_LOWEST=1

before_script:
- composer self-update
- composer install --prefer-source --no-interaction --dev

- sh -c "if [ '$DB' = 'mysql' ]; then if [ '$DOCKER' = '1' ]; then apt-get -qq install -qq -y mysql-server && service mysql start; fi; mysql -e 'CREATE DATABASE cakephp_test;'; fi"
- if [[ $TRAVIS_PHP_VERSION != 7.4 ]]; then phpenv config-rm xdebug.ini; fi

- sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi"
- if [[ $PREFER_LOWEST != 1 ]]; then composer update --no-interaction; fi
- if [[ $PREFER_LOWEST == 1 ]]; then composer update --no-interaction --prefer-lowest --prefer-stable; fi

- sh -c "if [ '$PHPCS' = '1' ]; then composer require 'cakephp/cakephp-codesniffer:dev-master'; fi"
- if [[ $DB = 'mysql' ]]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi
- if [[ $DB = 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi

- sh -c "if [ '$COVERALLS' = '1' ]; then composer require --dev php-coveralls/php-coveralls; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then mkdir -p build/logs; fi"

- command -v phpenv > /dev/null && phpenv rehash || true
- if [[ $STATIC_ANALYSIS = 1 ]]; then composer require --dev phpstan/phpstan:^0.12; fi

script:
- sh -c "if [ '$COVERALLS' = '1' ]; then vendor/bin/phpunit --coverage-clover build/logs/clover.xml; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then php vendor/bin/php-coveralls -v; fi"
- sh -c "if [ '$DEFAULT' = '1' ]; then vendor/bin/phpunit ; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -n -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP --ignore=vendor --ignore=docs --ignore=tests/bootstrap.php . ; fi"

- |
if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.4 ]]; then
mkdir -p build/logs
vendor/bin/phpunit --coverage-clover=build/logs/clover.xml
fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.4 ]]; then vendor/bin/phpunit; fi

- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -n -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi

after_success:
- |
if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.2 ]]; then
wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.1.0/php-coveralls.phar
chmod +x php-coveralls.phar
./php-coveralls.phar
fi
notifications:
email: false

addons:
postgresql: "9.4"
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# Version

A CakePHP 3.x plugin that facilitates versioned database entities
A CakePHP 4.x plugin that facilitates versioned database entities

## Installation

Expand Down Expand Up @@ -182,7 +182,7 @@ The above `insertAdditionalData()` method could then look like this:
*
* @return array
*/
public function insertAdditionalData(Event $event)
public function insertAdditionalData(Event $event)
{
$data = [
...
Expand All @@ -191,7 +191,7 @@ The above `insertAdditionalData()` method could then look like this:
if ($event->data('_footprint')) {
$user = $event->data('_footprint');
$data += [
'user_id' => $user['id'],
'user_id' => $user->id,
];
}

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
}
],
"require": {
"cakephp/orm": "~3.6",
"php": ">=5.6",
"cakephp/orm": "~4.0",
"php": ">=7.2",
"php-coveralls/php-coveralls": "^2.0"
},
"require-dev": {
"cakephp/cakephp": "~3.6",
"phpunit/phpunit": "^4.1 || ^5.0",
"cakephp/cakephp": "~4.0",
"phpunit/phpunit": "^8",
"cakephp/cakephp-codesniffer": "dev-master"
},
"autoload": {
Expand Down
6 changes: 0 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php"
>
<php>
Expand All @@ -22,11 +21,6 @@
<directory suffix=".php">./src</directory>
<directory suffix=".php">./tests</directory>
</whitelist>
<blacklist>
<directory suffix=".php">./docs</directory>
<directory suffix=".php">./vendor</directory>
<file>./tests/bootstrap.php</file>
</blacklist>
</filter>

<!-- Setup a listener for fixtures -->
Expand Down
21 changes: 21 additions & 0 deletions src/Event/EventListener.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
<?php
declare(strict_types=1);

/**
* Class EventListener
*
* @category CakePHP-Plugin
* @package Josegonzalez\Version\Event
* @author Jose Diaz-Gonzalez <[email protected]>
* @license MIT License (https://github.com/josegonzalez/cakephp-version/blob/master/LICENSE.txt)
* @link https://github.com/josegonzalez/cakephp-version
*/

namespace Josegonzalez\Version\Event;

use Cake\Event\Event as CakeEvent;
use Cake\Event\EventListenerInterface;
use Cake\Event\EventManagerTrait;

/**
* Class EventListener
*
* @category CakePHP-Plugin
* @package Josegonzalez\Version\Event
* @author Jose Diaz-Gonzalez <[email protected]>
* @license MIT License (https://github.com/josegonzalez/cakephp-version/blob/master/LICENSE.txt)
* @link https://github.com/josegonzalez/cakephp-version
*/
abstract class EventListener implements EventListenerInterface
{
use EventManagerTrait;
Expand Down
54 changes: 37 additions & 17 deletions src/Event/VersionListener.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
<?php
declare(strict_types=1);

/**
* Class VersionListener
*
* @category CakePHP-Plugin
* @package Josegonzalez\Version\Event
* @author Jose Diaz-Gonzalez <[email protected]>
* @license MIT License (https://github.com/josegonzalez/cakephp-version/blob/master/LICENSE.txt)
* @link https://github.com/josegonzalez/cakephp-version
*/

namespace Josegonzalez\Version\Event;

use Cake\Datasource\ConnectionManager;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;

/**
* Class VersionListener
*
* @category CakePHP-Plugin
* @package Josegonzalez\Version\Event
* @author Jose Diaz-Gonzalez <[email protected]>
* @license MIT License (https://github.com/josegonzalez/cakephp-version/blob/master/LICENSE.txt)
* @link https://github.com/josegonzalez/cakephp-version
*/
class VersionListener extends EventListener
{
/**
* Called before the entity template is rendered
*
* @param Event $event An Event instance
* @param \Cake\Event\Event $event An Event instance
* @return void
*/
public function beforeRenderEntity(Event $event)
{
$this->_checkAssociation($event, 'versions');
$this->checkAssociation($event, 'versions');
}

/**
* Called before the test case template is rendered
*
* @param Event $event An Event instance
* @param \Cake\Event\Event $event An Event instance
* @return void
*/
public function beforeRenderTestCase(Event $event)
Expand All @@ -39,22 +59,22 @@ public function beforeRenderTestCase(Event $event)
/**
* Called before the table template is rendered
*
* @param Event $event An Event instance
* @param \Cake\Event\Event $event An Event instance
* @return void
*/
public function beforeRenderTable(Event $event)
{
$this->_checkAssociation($event, 'versions');
$this->_fixVersionTables($event);
$this->checkAssociation($event, 'versions');
$this->fixVersionTables($event);
}

/**
* Removes unnecessary associations
*
* @param Event $event An Event instance
* @param \Cake\Event\Event $event An Event instance
* @return void
*/
protected function _fixVersionTables(Event $event)
protected function fixVersionTables(Event $event)
{
if (!preg_match('/Versions$/', $event->subject->viewVars['name'])) {
return;
Expand All @@ -71,11 +91,11 @@ protected function _fixVersionTables(Event $event)
/**
* Attaches the behavior and modifies associations as necessary
*
* @param Event $event An Event instance
* @param \Cake\Event\Event $event An Event instance
* @param string $tableSuffix a suffix for the primary table
* @return bool true if modified, false otherwise
*/
protected function _checkAssociation(Event $event, $tableSuffix)
protected function checkAssociation(Event $event, $tableSuffix)
{
$subject = $event->subject;
$connection = ConnectionManager::get($subject->viewVars['connection']);
Expand All @@ -90,19 +110,19 @@ protected function _checkAssociation(Event $event, $tableSuffix)
sprintf("'versionTable' => '%s'", $versionTable),
];

$event->subject->viewVars['associations']['belongsTo'] = $this->_modifyBelongsTo($event);
$event->subject->viewVars['rulesChecker'] = $this->_modifyRulesChecker($event);
$event->subject->viewVars['associations']['belongsTo'] = $this->modifyBelongsTo($event);
$event->subject->viewVars['rulesChecker'] = $this->modifyRulesChecker($event);

return true;
}

/**
* Removes unnecessary belongsTo associations
*
* @param Event $event An Event instance
* @param \Cake\Event\Event $event An Event instance
* @return array
*/
protected function _modifyBelongsTo(Event $event)
protected function modifyBelongsTo(Event $event)
{
$belongsTo = $event->subject->viewVars['associations']['belongsTo'];

Expand All @@ -120,10 +140,10 @@ protected function _modifyBelongsTo(Event $event)
/**
* Removes unnecessary rulesChecker entries
*
* @param Event $event An Event instance
* @param \Cake\Event\Event $event An Event instance
* @return array
*/
protected function _modifyRulesChecker(Event $event)
protected function modifyRulesChecker(Event $event)
{
$rulesChecker = $event->subject->viewVars['rulesChecker'];

Expand Down
26 changes: 23 additions & 3 deletions src/Model/Behavior/Version/VersionTrait.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
<?php
declare(strict_types=1);

/**
* Trait VersionTrait
*
* @category CakePHP-Plugin
* @package Josegonzalez\Version\Model\Behavior\Version
* @author Jose Diaz-Gonzalez <[email protected]>
* @license MIT License (https://github.com/josegonzalez/cakephp-version/blob/master/LICENSE.txt)
* @link https://github.com/josegonzalez/cakephp-version
*/

namespace Josegonzalez\Version\Model\Behavior\Version;

use Cake\Collection\Collection;
use Cake\ORM\TableRegistry;

/**
* Trait VersionTrait
*
* @category CakePHP-Plugin
* @package Josegonzalez\Version\Model\Behavior\Version
* @author Jose Diaz-Gonzalez <[email protected]>
* @license MIT License (https://github.com/josegonzalez/cakephp-version/blob/master/LICENSE.txt)
* @link https://github.com/josegonzalez/cakephp-version
*/
trait VersionTrait
{
/**
* Retrieves a specified version for the current entity
*
* @param int $versionId The version number to retrieve
* @param bool $reset If true, will re-retrieve the related version collection
* @param int $versionId The version number to retrieve
* @param bool $reset If true, will re-retrieve the related version collection
* @return \Cake\ORM\Entity|null
*/
public function version($versionId, $reset = false)
Expand Down
Loading

0 comments on commit 163b6fb

Please sign in to comment.