Skip to content
This repository was archived by the owner on Jul 7, 2022. It is now read-only.

Commit 3e20e23

Browse files
authored
Merge pull request #50 from AD7six/cake-3.6
Cake 3.5
2 parents ee22ce2 + 6e92eaa commit 3e20e23

File tree

4 files changed

+136
-87
lines changed

4 files changed

+136
-87
lines changed

.travis.yml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ php:
66
- 7.1
77
- 7.2
88

9-
sudo: false
10-
119
env:
1210
matrix:
1311
- DB=mysql db_dsn='mysql://[email protected]/cakephp_test'
@@ -20,29 +18,34 @@ matrix:
2018
fast_finish: true
2119

2220
include:
23-
- php: 7.0
21+
- php: 5.6
22+
env: PREFER_LOWEST=1 DB=mysql db_dsn='mysql://[email protected]/cakephp_test'
23+
24+
- php: 7.1
2425
env: PHPCS=1 DEFAULT=0
2526

26-
- php: 7.0
27+
- php: 7.1
2728
env: PHPSTAN=1 DEFAULT=0
2829

2930
before_script:
30-
- if [[ $TRAVIS_PHP_VERSION != 7.0 && $TRAVIS_PHP_VERSION != 7.2 ]]; then phpenv config-rm xdebug.ini; fi
31+
- phpenv config-rm xdebug.ini
32+
33+
- if [[ $PREFER_LOWEST != 1 ]]; then composer install --no-interaction --prefer-source; fi
34+
- if [[ $PREFER_LOWEST = 1 ]]; then composer update --no-interaction --prefer-source --prefer-lowest --prefer-stable; fi
3135

32-
- composer install --no-interaction --prefer-source
3336
- if [[ $DB = 'mysql' ]]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi
3437
- if [[ $DB = 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi
3538

3639
script:
37-
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
38-
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi
40+
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.1 ]]; then vendor/bin/phpunit; fi
41+
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.1 ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=clover.xml; fi
3942

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

42-
- if [[ $PHPSTAN = 1 ]]; then composer require --dev phpstan/phpstan:^0.8 && vendor/bin/phpstan analyse -l 5 src; fi
45+
- if [[ $PHPSTAN = 1 ]]; then composer require --dev phpstan/phpstan:^0.9 && vendor/bin/phpstan analyse -l 5 src; fi
4346

4447
after_success:
45-
- if [[ $DEFAULT = 1 && $DB = 'mysql' && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi
48+
- if [[ $DEFAULT = 1 && $DB = 'mysql' && $TRAVIS_PHP_VERSION = 7.1 ]]; then bash <(curl -s https://codecov.io/bash); fi
4649

4750
notifications:
4851
email: false

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
}
1111
],
1212
"require": {
13-
"cakephp/orm": "^3.4.3",
14-
"cakephp/i18n": "^3.4.3"
13+
"cakephp/orm": "^3.5",
14+
"cakephp/i18n": "^3.5"
1515
},
1616
"require-dev": {
17-
"phpunit/phpunit": "<6.0",
18-
"cakephp/cakephp": "^3.4.3",
19-
"cakephp/cakephp-codesniffer": "~2.3"
17+
"phpunit/phpunit": "^5.7.14|^6.0",
18+
"cakephp/cakephp": "^3.5",
19+
"cakephp/cakephp-codesniffer": "^3.0"
2020
},
2121
"autoload": {
2222
"psr-4": {

src/Model/Behavior/ShadowTranslateBehavior.php

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Cake\Database\Expression\FieldInterface;
66
use Cake\Datasource\EntityInterface;
77
use Cake\Event\Event;
8+
use Cake\I18n\I18n;
89
use Cake\ORM\Behavior\TranslateBehavior;
910
use Cake\ORM\Query;
1011
use Cake\ORM\Table;
@@ -23,8 +24,13 @@ class ShadowTranslateBehavior extends TranslateBehavior
2324
*/
2425
public function __construct(Table $table, array $config = [])
2526
{
26-
$tableAlias = $table->alias();
27-
list($plugin) = pluginSplit($table->registryAlias(), true);
27+
$this->_defaultConfig['implementedMethods'] += [
28+
'setLocale' => 'setLocale',
29+
'getLocale' => 'getLocale',
30+
];
31+
32+
$tableAlias = $table->getAlias();
33+
list($plugin) = pluginSplit($table->getRegistryAlias(), true);
2834

2935
if (isset($config['referenceName'])) {
3036
$tableReferenceName = $config['referenceName'];
@@ -41,6 +47,46 @@ public function __construct(Table $table, array $config = [])
4147
parent::__construct($table, $config);
4248
}
4349

50+
/**
51+
* Sets the locale that should be used for all future find and save operations on
52+
* the table where this behavior is attached to.
53+
*
54+
* When fetching records, the behavior will include the content for the locale set
55+
* via this method, and likewise when saving data, it will save the data in that
56+
* locale.
57+
*
58+
* Note that in case an entity has a `_locale` property set, that locale will win
59+
* over the locale set via this method (and over the globally configured one for
60+
* that matter)!
61+
*
62+
* @param string|null $locale The locale to use for fetching and saving records. Pass `null`
63+
* in order to unset the current locale, and to make the behavior fall back to using the
64+
* globally configured locale.
65+
* @return $this
66+
* @see \ShadowTranslate\ORM\Behavior\ShadowTranslateBehavior::getLocale()
67+
*/
68+
public function setLocale($locale)
69+
{
70+
$this->_locale = $locale;
71+
72+
return $this;
73+
}
74+
75+
/**
76+
* Returns the current locale.
77+
*
78+
* If no locale has been explicitly set via `setLocale()`, this method will return
79+
* the currently configured global locale.
80+
*
81+
* @return string
82+
* @see \Cake\I18n\I18n::getLocale()
83+
* @see \ShadowTranslate\ORM\Behavior\ShadowTranslateBehavior::setLocale()
84+
*/
85+
public function getLocale()
86+
{
87+
return $this->_locale ?: I18n::getLocale();
88+
}
89+
4490
/**
4591
* Create a hasMany association for all records
4692
*
@@ -56,14 +102,14 @@ public function __construct(Table $table, array $config = [])
56102
*/
57103
public function setupFieldAssociations($fields, $table, $fieldConditions, $strategy)
58104
{
59-
$config = $this->config();
105+
$config = $this->getConfig();
60106

61107
$this->_table->hasMany($config['translationTable'], [
62108
'className' => $config['translationTable'],
63109
'foreignKey' => 'id',
64110
'strategy' => $strategy,
65111
'propertyName' => '_i18n',
66-
'dependent' => true
112+
'dependent' => true,
67113
]);
68114
}
69115

@@ -79,13 +125,13 @@ public function setupFieldAssociations($fields, $table, $fieldConditions, $strat
79125
*/
80126
public function beforeFind(Event $event, Query $query, $options)
81127
{
82-
$locale = $this->locale();
128+
$locale = $this->getLocale();
83129

84-
if ($locale === $this->config('defaultLocale')) {
130+
if ($locale === $this->getConfig('defaultLocale')) {
85131
return;
86132
}
87133

88-
$config = $this->config();
134+
$config = $this->getConfig();
89135

90136
if (isset($options['filterByCurrentLocale'])) {
91137
$joinType = $options['filterByCurrentLocale'] ? 'INNER' : 'LEFT';
@@ -133,7 +179,7 @@ public function beforeFind(Event $event, Query $query, $options)
133179
*/
134180
protected function _addFieldsToQuery(Query $query, array $config)
135181
{
136-
if ($query->autoFields()) {
182+
if ($query->isAutoFieldsEnabled()) {
137183
return true;
138184
}
139185

@@ -264,8 +310,8 @@ protected function _traverseClause(Query $query, $name = '', $config = [])
264310
*/
265311
public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options)
266312
{
267-
$locale = $entity->get('_locale') ?: $this->locale();
268-
$newOptions = [$this->_translationTable->alias() => ['validate' => false]];
313+
$locale = $entity->get('_locale') ?: $this->getLocale();
314+
$newOptions = [$this->_translationTable->getAlias() => ['validate' => false]];
269315
$options['associated'] = $newOptions + $options['associated'];
270316

271317
// Check early if empty translations are present in the entity.
@@ -281,7 +327,7 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o
281327

282328
// No additional translation records need to be saved,
283329
// as the entity is in the default locale.
284-
if ($noBundled && $locale === $this->config('defaultLocale')) {
330+
if ($noBundled && $locale === $this->getConfig('defaultLocale')) {
285331
return;
286332
}
287333

@@ -296,15 +342,15 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o
296342
return;
297343
}
298344

299-
$primaryKey = (array)$this->_table->primaryKey();
345+
$primaryKey = (array)$this->_table->getPrimaryKey();
300346
$id = $entity->get(current($primaryKey));
301347

302348
// When we have no key and bundled translations, we
303349
// need to mark the entity dirty so the root
304350
// entity persists.
305351
if ($noFields && $bundled && !$id) {
306352
foreach ($this->_translationFields() as $field) {
307-
$entity->dirty($field, true);
353+
$entity->setDirty($field, true);
308354
}
309355

310356
return;
@@ -319,7 +365,7 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o
319365
$translation = $this->_translationTable()->find()
320366
->select(array_merge(['id', 'locale'], $fields))
321367
->where($where)
322-
->bufferResults(false)
368+
->enableBufferedResults(false)
323369
->first();
324370

325371
if ($translation) {
@@ -331,17 +377,17 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o
331377
$where + $values,
332378
[
333379
'useSetters' => false,
334-
'markNew' => true
380+
'markNew' => true,
335381
]
336382
);
337383
}
338384

339385
$entity->set('_i18n', array_merge($bundled, [$translation]));
340386
$entity->set('_locale', $locale, ['setter' => false]);
341-
$entity->dirty('_locale', false);
387+
$entity->setDirty('_locale', false);
342388

343389
foreach ($fields as $field) {
344-
$entity->dirty($field, false);
390+
$entity->setDirty($field, false);
345391
}
346392
}
347393

@@ -367,13 +413,13 @@ public function buildMarshalMap($marshaller, $map, $options)
367413
*/
368414
public function translationField($field)
369415
{
370-
if ($this->locale() === $this->getConfig('defaultLocale')) {
416+
if ($this->getLocale() === $this->getConfig('defaultLocale')) {
371417
return $this->_table->aliasField($field);
372418
}
373419

374420
$translatedFields = $this->_translationFields();
375421
if (in_array($field, $translatedFields)) {
376-
return $this->config('hasOneAlias') . '.' . $field;
422+
return $this->getConfig('hasOneAlias') . '.' . $field;
377423
}
378424

379425
return $this->_table->aliasField($field);
@@ -399,7 +445,7 @@ protected function _rowMapper($results, $locale)
399445
$hydrated = !is_array($row);
400446

401447
if (empty($row['translation'])) {
402-
$row['_locale'] = $this->locale();
448+
$row['_locale'] = $this->getLocale();
403449
unset($row['translation']);
404450

405451
if ($hydrated) {
@@ -459,7 +505,7 @@ public function groupTranslations($results)
459505

460506
$row['_translations'] = $result;
461507
unset($row['_i18n']);
462-
if (is_object($row)) {
508+
if ($row instanceof EntityInterface) {
463509
$row->clean();
464510
}
465511

@@ -479,11 +525,11 @@ protected function _bundleTranslatedFields($entity)
479525
{
480526
$translations = (array)$entity->get('_translations');
481527

482-
if (empty($translations) && !$entity->dirty('_translations')) {
528+
if (empty($translations) && !$entity->isDirty('_translations')) {
483529
return;
484530
}
485531

486-
$primaryKey = (array)$this->_table->primaryKey();
532+
$primaryKey = (array)$this->_table->getPrimaryKey();
487533
$key = $entity->get(current($primaryKey));
488534

489535
foreach ($translations as $lang => $translation) {
@@ -512,7 +558,7 @@ protected function _bundleTranslatedFields($entity)
512558
protected function _translationTable($config = [])
513559
{
514560
if (!$config) {
515-
$config = $this->config();
561+
$config = $this->getConfig();
516562
}
517563

518564
return TableRegistry::get($config['translationTable']);
@@ -525,16 +571,16 @@ protected function _translationTable($config = [])
525571
*/
526572
protected function _mainFields()
527573
{
528-
$fields = $this->config('mainTableFields');
574+
$fields = $this->getConfig('mainTableFields');
529575

530576
if ($fields) {
531577
return $fields;
532578
}
533579

534580
$table = $this->_table;
535-
$fields = $table->schema()->columns();
581+
$fields = $table->getSchema()->columns();
536582

537-
$this->config('mainTableFields', $fields);
583+
$this->setConfig('mainTableFields', $fields);
538584

539585
return $fields;
540586
}
@@ -546,17 +592,17 @@ protected function _mainFields()
546592
*/
547593
protected function _translationFields()
548594
{
549-
$fields = $this->config('fields');
595+
$fields = $this->getConfig('fields');
550596

551597
if ($fields) {
552598
return $fields;
553599
}
554600

555601
$table = $this->_translationTable();
556-
$fields = $table->schema()->columns();
602+
$fields = $table->getSchema()->columns();
557603
$fields = array_values(array_diff($fields, ['id', 'locale']));
558604

559-
$this->config('fields', $fields);
605+
$this->setConfig('fields', $fields);
560606

561607
return $fields;
562608
}

0 commit comments

Comments
 (0)