Engine.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs;
  9. use JonnyW\PhantomJs\Exception\InvalidExecutableException;
  10. /**
  11. * PHP PhantomJs
  12. *
  13. * @author Jon Wenmoth <contact@jonnyw.me>
  14. */
  15. class Engine
  16. {
  17. /**
  18. * Executable path.
  19. *
  20. * @var string
  21. * @access protected
  22. */
  23. protected $path;
  24. /**
  25. * Debug flag.
  26. *
  27. * @var boolean
  28. * @access protected
  29. */
  30. protected $debug;
  31. /**
  32. * Cache flag.
  33. *
  34. * @var boolean
  35. * @access protected
  36. */
  37. protected $cache;
  38. /**
  39. * PhantomJs run options.
  40. *
  41. * @var array
  42. * @access protected
  43. */
  44. protected $options;
  45. /**
  46. * Log info
  47. *
  48. * @var string
  49. * @access protected
  50. */
  51. protected $log;
  52. /**
  53. * Internal constructor
  54. *
  55. * @access public
  56. * @return void
  57. */
  58. public function __construct()
  59. {
  60. $this->path = 'bin/phantomjs';
  61. $this->options = array();
  62. $this->debug = false;
  63. $this->cache = true;
  64. }
  65. /**
  66. * Get PhantomJs run command with
  67. * loader run options.
  68. *
  69. * @access public
  70. * @return string
  71. */
  72. public function getCommand()
  73. {
  74. $path = $this->getPath();
  75. $options = $this->getOptions();
  76. $this->validateExecutable($path);
  77. if ($this->cache) {
  78. array_push($options, '--disk-cache=true');
  79. }
  80. if ($this->debug) {
  81. array_push($options, '--debug=true');
  82. }
  83. return sprintf('%s %s', $path, implode(' ', $options));
  84. }
  85. /**
  86. * Set path.
  87. *
  88. * @access public
  89. * @param string $path
  90. * @return \JonnyW\PhantomJs\Client
  91. */
  92. public function setPath($path)
  93. {
  94. $this->validateExecutable($path);
  95. $this->path = $path;
  96. return $this;
  97. }
  98. /**
  99. * Get path.
  100. *
  101. * @access public
  102. * @return string
  103. */
  104. public function getPath()
  105. {
  106. return $this->path;
  107. }
  108. /**
  109. * Set PhantomJs run options.
  110. *
  111. * @access public
  112. * @param array $options
  113. * @return \JonnyW\PhantomJs\Client
  114. */
  115. public function setOptions(array $options)
  116. {
  117. $this->options = $options;
  118. return $this;
  119. }
  120. /**
  121. * Get PhantomJs run options.
  122. *
  123. * @access public
  124. * @return array
  125. */
  126. public function getOptions()
  127. {
  128. return (array) $this->options;
  129. }
  130. /**
  131. * Add single PhantomJs run option.
  132. *
  133. * @access public
  134. * @param string $option
  135. * @return \JonnyW\PhantomJs\Client
  136. */
  137. public function addOption($option)
  138. {
  139. if (!in_array($option, $this->options)) {
  140. $this->options[] = $option;
  141. }
  142. return $this;
  143. }
  144. /**
  145. * Debug.
  146. *
  147. * @access public
  148. * @param boolean $doDebug
  149. * @return \JonnyW\PhantomJs\Client
  150. */
  151. public function debug($doDebug)
  152. {
  153. $this->debug = $doDebug;
  154. return $this;
  155. }
  156. /**
  157. * Cache.
  158. *
  159. * @access public
  160. * @param boolean $doCache
  161. * @return \JonnyW\PhantomJs\Client
  162. */
  163. public function cache($doCache)
  164. {
  165. $this->cache = $doCache;
  166. return $this;
  167. }
  168. /**
  169. * Log info.
  170. *
  171. * @access public
  172. * @param string $info
  173. * @return \JonnyW\PhantomJs\Client
  174. */
  175. public function log($info)
  176. {
  177. $this->log = $info;
  178. return $this;
  179. }
  180. /**
  181. * Get log info.
  182. *
  183. * @access public
  184. * @return string
  185. */
  186. public function getLog()
  187. {
  188. return $this->log;
  189. }
  190. /**
  191. * Clear log info.
  192. *
  193. * @access public
  194. * @return \JonnyW\PhantomJs\Client
  195. */
  196. public function clearLog()
  197. {
  198. $this->log = '';
  199. return $this;
  200. }
  201. /**
  202. * Validate execuable file.
  203. *
  204. * @access private
  205. * @param string $file
  206. * @return boolean
  207. * @throws \JonnyW\PhantomJs\Exception\InvalidExecutableException
  208. */
  209. private function validateExecutable($file)
  210. {
  211. if (!file_exists($file) || !is_executable($file)) {
  212. throw new InvalidExecutableException(sprintf('File does not exist or is not executable: %s', $file));
  213. }
  214. return true;
  215. }
  216. }