PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/php/Arbit/Periodic/Command/System/Exec.php

https://github.com/Arbitracker/Periodic
PHP | 129 lines | 58 code | 13 blank | 58 comment | 7 complexity | 6d87e01bfc46b327c55b347258d5d4af MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Command
  4. *
  5. * This file is part of periodic
  6. *
  7. * periodic is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU Lesser General Public License as published by the Free
  9. * Software Foundation; version 3 of the License.
  10. *
  11. * periodic is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with periodic; if not, write to the Free Software Foundation, Inc., 51
  18. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * @package Core
  21. * @version $Revision: 999 $
  22. * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
  23. */
  24. namespace Arbit\Periodic\Command\System;
  25. use Arbit\Periodic\Command,
  26. Arbit\Periodic\Executor,
  27. Arbit\Periodic\Logger,
  28. Arbit\XML;
  29. /**
  30. * Command
  31. *
  32. * Command to execute system commands
  33. */
  34. class Exec extends Command
  35. {
  36. /**
  37. * Run command
  38. *
  39. * Execute the actual bits.
  40. *
  41. * Should return one of the status constant values, defined as class
  42. * constants in Executor.
  43. *
  44. * @param XML\Node $configuration
  45. * @param Logger $logger
  46. * @return int
  47. */
  48. public function run( XML\Node $configuration, Logger $logger )
  49. {
  50. $command = (string) $configuration;
  51. if ( empty( $command ) )
  52. {
  53. $logger->log( 'No command provided for execution.', Logger::ERROR );
  54. return Executor::ERROR;
  55. }
  56. // Check for availability of PHP command execution functions
  57. if ( !function_exists( 'proc_open' ) )
  58. {
  59. $logger->log( 'Required PHP functions proc_* not available.', Logger::ERROR );
  60. return Executor::ERROR;
  61. }
  62. $failOnError = true;
  63. if ( isset( $configuration['failOnError'] ) )
  64. {
  65. $failOnError = !( (string) $configuration['failOnError'] === 'false' );
  66. }
  67. return $this->execute( $command, $failOnError, $logger );
  68. }
  69. /**
  70. * Execute command
  71. *
  72. * Execute given shell command. All error output from the shell command
  73. * will be added as warnings to the logger.
  74. *
  75. * If the command returns with an non-zero exit code and $failOnError is
  76. * set to true the command will return Executor::ERROR - otherwise
  77. * it will always return with Executor::SUCCESS.
  78. *
  79. * @param string $command
  80. * @param bool $failOnError
  81. * @param Logger $logger
  82. * @return int
  83. */
  84. protected function execute( $command, $failOnError = true, Logger $logger )
  85. {
  86. $descriptors = array(
  87. 0 => array( 'pipe', 'r' ), // STDIN
  88. 1 => array( 'pipe', 'w' ), // STDOUT
  89. 2 => array( 'pipe', 'w' ), // STDERR
  90. );
  91. $proc = proc_open( $command, $descriptors, $pipes );
  92. if ( !is_resource( $proc ) )
  93. {
  94. $logger->log( 'Could not start processs.', Logger::ERROR );
  95. return Executor::ERROR;
  96. }
  97. // Add command output as information to log
  98. $output = trim( stream_get_contents( $pipes[1] ) );
  99. if ( !empty( $output ) )
  100. {
  101. $logger->log( $output );
  102. }
  103. fclose( $pipes[1] );
  104. // Add command error output as warnings to log
  105. $output = trim( stream_get_contents( $pipes[2] ) );
  106. if ( !empty( $output ) )
  107. {
  108. $logger->log( $output, Logger::WARNING );
  109. }
  110. fclose( $pipes[2] );
  111. // Receive process return values
  112. $return = proc_close( $proc );
  113. $logger->log( "Command exited with return value $return" );
  114. return ( $return && $failOnError ) ? Executor::ERROR : Executor::SUCCESS;
  115. }
  116. }