Extract.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\console\command\g11n;
  9. use Exception;
  10. use lithium\g11n\Catalog;
  11. use lithium\core\Libraries;
  12. /**
  13. * The `Extract` class is a command for extracting messages from files.
  14. */
  15. class Extract extends \lithium\console\Command {
  16. public $source;
  17. public $destination;
  18. public $scope;
  19. protected function _init() {
  20. parent::_init();
  21. $this->source = $this->source ?: LITHIUM_APP_PATH;
  22. $this->destination = $this->destination ?: Libraries::get(true, 'resources') . '/g11n';
  23. }
  24. /**
  25. * The main method of the command.
  26. *
  27. * @return void
  28. */
  29. public function run() {
  30. $this->header('Message Extraction');
  31. if (!$data = $this->_extract()) {
  32. $this->error('Yielded no items.');
  33. return 1;
  34. }
  35. $count = count($data);
  36. $this->out("Yielded {$count} item(s).");
  37. $this->out();
  38. $this->header('Message Template Creation');
  39. if (!$this->_writeTemplate($data)) {
  40. $this->error('Failed to write template.');
  41. return 1;
  42. }
  43. $this->out();
  44. return 0;
  45. }
  46. /**
  47. * Extracts translatable strings from multiple files.
  48. *
  49. * @return array Returns the catalog specified. Returns boolean `false` when an error occurs.
  50. */
  51. protected function _extract() {
  52. $message[] = 'A `Catalog` class configuration with an adapter that is capable of';
  53. $message[] = 'handling read requests for the `messageTemplate` category is needed';
  54. $message[] = 'in order to proceed. This may also be referred to as `extractor`.';
  55. $this->out($message);
  56. $this->out();
  57. $name = $this->_configuration(array(
  58. 'adapter' => 'Code',
  59. 'path' => $this->source,
  60. 'scope' => $this->scope
  61. ));
  62. $configs = Catalog::config();
  63. try {
  64. return Catalog::read($name, 'messageTemplate', 'root', array(
  65. 'scope' => $configs[$name]['scope'],
  66. 'lossy' => false
  67. ));
  68. } catch (Exception $e) {
  69. return false;
  70. }
  71. }
  72. /**
  73. * Prompts for data source and writes template.
  74. *
  75. * @param array $data Data to save.
  76. * @return void
  77. */
  78. protected function _writeTemplate($data) {
  79. $message[] = 'In order to proceed you need to choose a `Catalog` configuration';
  80. $message[] = 'which is used for writing the template. The adapter for the configuration';
  81. $message[] = 'should be capable of handling write requests for the `messageTemplate`';
  82. $message[] = 'category.';
  83. $this->out($message);
  84. $this->out();
  85. $name = $this->_configuration(array(
  86. 'adapter' => 'Gettext',
  87. 'path' => $this->destination,
  88. 'scope' => $this->scope
  89. ));
  90. if ($name != 'temporary') {
  91. $scope = $this->in('Scope:', array('default' => $this->scope));
  92. }
  93. $message = array();
  94. $message[] = 'The template is now ready to be saved.';
  95. $message[] = 'Please note that an existing template will be overwritten.';
  96. $this->out($message);
  97. $this->out();
  98. if ($this->in('Save?', array('choices' => array('y', 'n'), 'default' => 'y')) != 'y') {
  99. $this->out('Aborting upon user request.');
  100. $this->stop(1);
  101. }
  102. try {
  103. return Catalog::write($name, 'messageTemplate', 'root', $data, compact('scope'));
  104. } catch (Exception $e) {
  105. return false;
  106. }
  107. }
  108. /**
  109. * Helps in selecting or - if required - adding a new `Catalog` collection
  110. * used for extracting or writing the template. A special configuration
  111. * with the name `temporary` may be created. Should a configuration with
  112. * that same name exist prior to entering this method it will be unset.
  113. *
  114. * @param array $options Options paired with defaults to prompt for.
  115. * @return string The name of the selected or newly created configuration.
  116. */
  117. protected function _configuration(array $options = array()) {
  118. $configs = (array) Catalog::config();
  119. if (isset($configs['temporary'])) {
  120. unset($configs['temporary']);
  121. }
  122. if ($configs) {
  123. $this->out('Available `Catalog` Configurations:');
  124. $prompt = 'Please choose a configuration or hit enter to add a new one:';
  125. foreach ($configs as $name => $config) {
  126. $this->out(" - {$name}");
  127. }
  128. } else {
  129. $this->out(' - No configuration found. -');
  130. $prompt = 'Please hit enter to add a temporary configuration:';
  131. }
  132. $this->out();
  133. $name = $this->in($prompt, array(
  134. 'choices' => array_keys($configs),
  135. 'default' => 'temporary'
  136. ));
  137. if ($name == 'temporary') {
  138. foreach ($options as $option => $default) {
  139. $configs[$name][$option] = $this->in(ucfirst($option) . ':', compact('default'));
  140. }
  141. Catalog::config($configs);
  142. }
  143. return $name;
  144. }
  145. }
  146. ?>