PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Extensions/Console/Command/DataMigrationShell.php

https://github.com/kareypowell/croogo
PHP | 87 lines | 65 code | 7 blank | 15 comment | 4 complexity | 28ab9571553db17aac1e66f4351ed255 MD5 | raw file
  1. <?php
  2. App::uses('AppShell', 'Console/Command');
  3. App::uses('ExtensionsInstaller', 'Extensions.Lib');
  4. App::uses('CroogoPlugin', 'Extensions.Lib');
  5. App::uses('CroogoTheme', 'Extensions.Lib');
  6. App::uses('DataMigration', 'Extensions.Lib/Utility');
  7. /**
  8. * DataMigration Shell
  9. *
  10. * @package Croogo.Extensions.Console.Command
  11. * @since 1.6
  12. * @link http://www.croogo.org
  13. */
  14. class DataMigrationShell extends AppShell {
  15. /**
  16. * Display help/options
  17. */
  18. public function getOptionParser() {
  19. return parent::getOptionParser()
  20. ->description(__d('croogo', 'Data Migration Utility'))
  21. ->addSubcommand('data', array(
  22. 'help' => 'Generate data files',
  23. 'parser' => array(
  24. 'description' => 'Generate installation data files.',
  25. 'options' => array(
  26. 'plugin' => array(
  27. 'help' => 'Plugin to generate data files into',
  28. 'short' => 'p',
  29. ),
  30. 'connection' => array(
  31. 'help' => 'Datasource to use when reading data',
  32. 'short' => 'c',
  33. ),
  34. ),
  35. 'arguments' => array(
  36. 'table' => array(
  37. 'required' => true,
  38. 'help' => 'table name',
  39. ),
  40. ),
  41. ),
  42. ));
  43. }
  44. /**
  45. * Prepares data in Config/Data/ required for install plugin
  46. *
  47. * Usage: ./Console/cake extensions.data_migration data table_name_here
  48. */
  49. public function data() {
  50. if (isset($this->params['plugin'])) {
  51. $plugin = $this->params['plugin'];
  52. }
  53. $connection = 'default';
  54. if (isset($this->params['connection'])) {
  55. $connection = $this->params['connection'];
  56. }
  57. $table = trim($this->args['0']);
  58. $name = Inflector::camelize(Inflector::singularize($table));
  59. $root = isset($plugin) ? CakePlugin::path($plugin) : APP;
  60. $output = $root . 'Config' . DS . 'Data' . DS . $name . 'Data.php';
  61. $records = array();
  62. $options = array(
  63. 'model' => array(
  64. 'name' => $name,
  65. 'table' => $table,
  66. 'connection' => $connection,
  67. ),
  68. 'output' => $output,
  69. );
  70. $DataMigration = new DataMigration();
  71. $success = $DataMigration->generate('all', array(
  72. 'recursive' => -1,
  73. ), $options);
  74. if ($success) {
  75. $this->out('<success>New file generated</success>: ' . str_replace(APP, '', $output));
  76. } else {
  77. $this->err('<error>Failed generating file for table</error>: ' . $table);
  78. }
  79. }
  80. }