Skip to content

Commit

Permalink
Add TableToEntity.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Dec 12, 2017
1 parent b4a7570 commit 71d427a
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -29,6 +29,7 @@ This tool is a split-off off the original CakePHP upgrade tool and provides addi
- Url
- Locale (fixing locale files)
- Model to Table (making the model files to Table class files)
- FixtureLoading
- Custom (tons of custom fixes)

Feel free to manually port those things back into the core one.
Expand Down
9 changes: 9 additions & 0 deletions docs/README.md
Expand Up @@ -34,6 +34,15 @@ Update fixtures to use new index/constraint features. This is necessary before r
### tests
Update test cases regarding fixtures.

### fixture_loading
Loading of fixtures is adjusted to new convention:
// Before
public $fixtures = ['app.user_role', 'plugin.foo_bar.user']

// After
public $fixtures = ['app.user_roles', 'plugin.foo_bar.users']
```
### locale
Update locale and PO file folder.
Expand Down
12 changes: 12 additions & 0 deletions src/Shell/Task/FixturesTask.php
Expand Up @@ -112,6 +112,18 @@ protected function _process($path) {
$out['_constraints'] = $constraints;
}

// Shim required type index.
if (!empty($out['_indexes'])) {
foreach ($out['_indexes'] as $i => $index) {
if (isset($index['unique']) && !$index['unique']) {
unset($out['_indexes'][$i]['unique']);
}
if (!isset($index['type'])) {
$out['_indexes'][$i]['type'] = 'index';
}
}
}

// Process table parameters
if (isset($data['tableParameters'])) {
$out['_options'] = $data['tableParameters'];
Expand Down
137 changes: 137 additions & 0 deletions src/Shell/Task/TableToEntityTask.php
@@ -0,0 +1,137 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Upgrade\Shell\Task;

use Cake\Utility\Inflector;

/**
* Make Table classes build Entity classes.
*
* @property \Cake\Upgrade\Shell\Task\StageTask $Stage
*/
class TableToEntityTask extends BaseTask {

use ChangeTrait;
use HelperTrait;

/**
* @var array
*/
public $tasks = ['Stage'];

/**
* Check all moves, and stage moving the file to new location.
*
* @param mixed $path
* @return bool
*/
protected function _process($path) {
$normalizedPath = str_replace(DS, '/', $path);

if (!preg_match('#/Model/Table/([a-z0-9]+?)Table\.php#i', $normalizedPath, $matches)) {
return false;
}
$modelClass = $matches[1];

$entityClass = Inflector::singularize($modelClass);

$new = str_replace(DS . 'Model' . DS . 'Table' . DS . $modelClass . 'Table', DS . 'Model' . DS . 'Entity' . DS . $entityClass, $path);
if (file_exists($new)) {
return false;
}

$content = <<<TXT
<?php
namespace App\Model\Entity;
use Tools\Model\Entity\Entity;
/**
* @property string \$id
*/
class $entityClass extends Entity {
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array
*/
protected \$_accessible = [
'*' => true,
'id' => false,
];
}
TXT;

return (bool)file_put_contents($new, $content);
}

/**
* _shouldProcess
*
* Is the current path within the scope of any move?
*
* @param string $path
* @return bool
*/
protected function _shouldProcess($path) {
$relativeFromRoot = $this->_getRelativePath($path);

if (strpos($relativeFromRoot, DS . 'Plugin' . DS) || strpos($relativeFromRoot, DS . 'plugins' . DS)) {
return false;
}
if (strpos($relativeFromRoot, DS . 'Vendor' . DS) || strpos($relativeFromRoot, DS . 'vendors' . DS)) {
return false;
}

$from = 'Model' . DS . 'Table' . DS;
if (strpos($relativeFromRoot, DS . $from) !== false) {
return true;
}

return false;
}

/**
* Key value map of from and to
*
* @return array
*/
protected function _to($fname) {

}

/**
* Get the option parser for this shell.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser() {
return parent::getOptionParser()
->addOptions([
'root' => [
'default' => '',
'help' => 'Set an application\'s root path. Not defining it makes the current path the root one.',
],
]);
}

}
10 changes: 10 additions & 0 deletions src/Shell/UpgradeShell.php
Expand Up @@ -61,10 +61,12 @@ class UpgradeShell extends Shell {
'I18n',
'Locale',
'Tests',
'FixtureLoading',
'Skeleton',
'Templates',
'PrefixedTemplates',
'ModelToTable',
'TableToEntity',
'Custom',
'Url',
'Cleanup',
Expand Down Expand Up @@ -197,6 +199,10 @@ public function getOptionParser() {
'help' => 'Update test cases regarding fixtures.',
'parser' => $this->I18n->getOptionParser(),
])
->addSubcommand('fixture_loading', [
'help' => 'Update test cases regarding fixture loading.',
'parser' => $this->I18n->getOptionParser(),
])
->addSubcommand('templates', [
'help' => 'Update view templates.',
'parser' => $this->Templates->getOptionParser(),
Expand All @@ -217,6 +223,10 @@ public function getOptionParser() {
'help' => 'Make models to tables.',
'parser' => $this->ModelToTable->getOptionParser(),
])
->addSubcommand('table_to_entity', [
'help' => 'Make entities from tables.',
'parser' => $this->ModelToTable->getOptionParser(),
])
->addSubcommand('prefixed_templates', [
'help' => 'Move view templates for prefixed actions.',
'parser' => $this->PrefixedTemplates->getOptionParser(),
Expand Down

0 comments on commit 71d427a

Please sign in to comment.