/framework/support/YPFCommand.php

https://github.com/yonpols/ypframework · PHP · 144 lines · 67 code · 16 blank · 61 comment · 10 complexity · 956c91fe0149c7931f5cf5a678b9652c MD5 · raw file

  1. <?php
  2. /**
  3. * Abstract class to implement command line options on ypf cli command.
  4. */
  5. abstract class YPFCommand extends YPFObject {
  6. const RESULT_OK = 0;
  7. const RESULT_INVALID_PARAMETERS = 1;
  8. const RESULT_FILESYSTEM_ERROR = 2;
  9. const RESULT_FILES_ERROR = 3;
  10. const RESULT_COMMAND_NOT_FOUND = 4;
  11. const RESULT_BAD_ENVIRONMENT = 5;
  12. /**
  13. * Get a command instance for the command name passed
  14. * @param string $commandName
  15. * @return YPFCommand command instance
  16. */
  17. public static function get($commandName) {
  18. $className = YPFramework::camelize(str_replace('.', '_', $commandName).'_command');
  19. return new $className;
  20. }
  21. /**
  22. * Method that a command class must implement. This method
  23. * is called when the command is run. It must return YPFCommand::RESULT_OK
  24. * if finished succesfully.
  25. *
  26. * @param array $parameters Is an array with parameters passed to the command. This array does not contain parameters passed to ypf
  27. * @return int Must return an integer to be passed to the shell
  28. */
  29. public abstract function run($parameters);
  30. /**
  31. * Method that a command class must implement. This method
  32. * is called when the help command is called. It's intended to
  33. * print some help about the command on the screen.
  34. *
  35. * @param array $parameters Is an array with parameters passed to the command. This array does not contain parameters passed to ypf
  36. */
  37. public abstract function help($parameters);
  38. /**
  39. * Method that a command class must implement. This method
  40. * must return a brief description of the command that will be
  41. * printed when the command list is printed on scren.
  42. *
  43. * @return string
  44. */
  45. public abstract function getDescription();
  46. /**
  47. * Require that the command is called in a directory with a YPI package
  48. * of type $types or other. This function can be called with more than
  49. * one parameter to require a package of one type or other.
  50. *
  51. * @param string $types
  52. * @return boolean
  53. */
  54. protected function requirePackage($types) {
  55. $found = false;
  56. $types = func_get_args();
  57. if (isset(YPFramework::getPackage()->type))
  58. $package_type = YPFramework::getPackage()->type;
  59. else
  60. $package_type = null;
  61. foreach($types as $type)
  62. if ($package_type == $type) {
  63. $found = $type;
  64. break;
  65. }
  66. if ($found === false)
  67. $this->exitNow (self::RESULT_BAD_ENVIRONMENT, sprintf ('can\'t find a valid %s on this directory', implode(' or ', $types)));
  68. return $found;
  69. }
  70. /**
  71. * Terminate command execution with an exit code and a text.
  72. * @param integer $code
  73. * @param string $text
  74. */
  75. protected function exitNow($code = YPFCommand::RESULT_OK, $text = '') {
  76. if ($text != '')
  77. echo $text."\n";
  78. exit($code);
  79. }
  80. /**
  81. * Proccess a simple template file replacing every ocurrence of {{key}} by the value
  82. * passed on $data['key']. Output can be written to a file or returned by the function.
  83. * @param string $fileName
  84. * @param array $data
  85. * @param string $destFileName
  86. * @return mixed returns true if written to a file, false if error or a string with the template processed
  87. */
  88. protected function getProcessedTemplate($fileName, $data, $destFileName = null) {
  89. $skeleton = file_get_contents($fileName);
  90. if ($skeleton === false)
  91. $this->exitNow (YPFCommand::RESULT_FILES_ERROR, sprintf('can\'t read skeleton file %s', $fileName));
  92. foreach ($data as $key => $val)
  93. $skeleton = str_replace (sprintf('{{%s}}', $key), $val, $skeleton);
  94. if ($destFileName === null)
  95. return $skeleton;
  96. else
  97. if (file_put_contents($destFileName, $skeleton) === false)
  98. $this->exitNow (YPFCommand::RESULT_FILES_ERROR, sprintf('can\'t write file %s', $destFileName));
  99. return true;
  100. }
  101. /**
  102. * Read configurations of a YAML file
  103. * @param string $configFileName
  104. * @return mixed
  105. */
  106. protected function getConfig($configFileName) {
  107. $yaml = new sfYamlParser();
  108. try
  109. {
  110. $config = $yaml->parse(file_get_contents($configFileName));
  111. return $config;
  112. }
  113. catch (InvalidArgumentException $e) {
  114. $this->exitNow(self::RESULT_FILES_ERROR, sprintf('%s config file corrupted', $configFileName));
  115. }
  116. }
  117. /**
  118. * Write configurations to a YAML file
  119. * @param string $configFileName
  120. * @param mixed $config
  121. */
  122. protected function setConfig($configFileName, $config) {
  123. $yaml = new sfYamlDumper();
  124. if (!file_put_contents($configFileName, $yaml->dump($config, 4)))
  125. $this->exitNow(self::RESULT_FILES_ERROR, sprintf('can\'t write %s config file', $configFileName));
  126. }
  127. }
  128. ?>