scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of Alchemy\BinaryDriver.
5 *
6 * (c) Alchemy <info@alchemy.fr>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Alchemy\BinaryDriver;
13  
14 use Alchemy\BinaryDriver\Exception\InvalidArgumentException;
15 use Symfony\Component\Process\ProcessBuilder;
16  
17 class ProcessBuilderFactory implements ProcessBuilderFactoryInterface
18 {
19 /**
20 * The binary path
21 *
22 * @var String
23 */
24 protected $binary;
25  
26 /**
27 * The timeout for the generated processes
28 *
29 * @var integer|float
30 */
31 private $timeout;
32  
33 /**
34 * An internal ProcessBuilder.
35 *
36 * Note that this one is used only if Symfony ProcessBuilder has method
37 * setPrefix (2.3)
38 *
39 * @var ProcessBuilder
40 */
41 private $builder;
42  
43 /**
44 * Tells whether Symfony LTS ProcessBuilder should be emulated or not.
45 *
46 * This symfony version provided a brand new ::setPrefix method.
47 *
48 * @var Boolean
49 */
50 public static $emulateSfLTS;
51  
52 /**
53 * Constructor
54 *
55 * @param String $binary The path to the binary
56 *
57 * @throws InvalidArgumentException In case binary path is invalid
58 */
59 public function __construct($binary)
60 {
61 $this->detectEmulation();
62  
63 if (!self::$emulateSfLTS) {
64 $this->builder = new ProcessBuilder();
65 }
66  
67 $this->useBinary($binary);
68 }
69  
70 /**
71 * Covenient method for unit testing
72 *
73 * @return type
74 */
75 public function getBuilder()
76 {
77 return $this->builder;
78 }
79  
80 /**
81 * Covenient method for unit testing
82 *
83 * @param ProcessBuilder $builder
84 * @return ProcessBuilderFactory
85 */
86 public function setBuilder(ProcessBuilder $builder)
87 {
88 $this->builder = $builder;
89  
90 return $this;
91 }
92  
93 /**
94 * @inheritdoc
95 */
96 public function getBinary()
97 {
98 return $this->binary;
99 }
100  
101 /**
102 * @inheritdoc
103 */
104 public function useBinary($binary)
105 {
106 if (!is_executable($binary)) {
107 throw new InvalidArgumentException(sprintf('`%s` is not an executable binary', $binary));
108 }
109  
110 $this->binary = $binary;
111  
112 if (!static::$emulateSfLTS) {
113 $this->builder->setPrefix($binary);
114 }
115  
116 return $this;
117 }
118  
119 /**
120 * @inheritdoc
121 */
122 public function setTimeout($timeout)
123 {
124 $this->timeout = $timeout;
125  
126 if (!static::$emulateSfLTS) {
127 $this->builder->setTimeout($this->timeout);
128 }
129  
130 return $this;
131 }
132  
133 /**
134 * @inheritdoc
135 */
136 public function getTimeout()
137 {
138 return $this->timeout;
139 }
140  
141 /**
142 * @inheritdoc
143 */
144 public function create($arguments = array())
145 {
146 if (null === $this->binary) {
147 throw new InvalidArgumentException('No binary set');
148 }
149  
150 if (!is_array($arguments)) {
151 $arguments = array($arguments);
152 }
153  
154 if (static::$emulateSfLTS) {
155 array_unshift($arguments, $this->binary);
156  
157 return ProcessBuilder::create($arguments)
158 ->setTimeout($this->timeout)
159 ->getProcess();
160 } else {
161 return $this->builder
162 ->setArguments($arguments)
163 ->getProcess();
164 }
165 }
166  
167 private function detectEmulation()
168 {
169 if (null !== static::$emulateSfLTS) {
170 return $this;
171 }
172  
173 static::$emulateSfLTS = !method_exists('Symfony\Component\Process\ProcessBuilder', 'setPrefix');
174  
175 return $this;
176 }
177 }