Skip to content

Commit f939bab

Browse files
committed
Procedures are now compiled and cached from partials allowing for partial script injection
1 parent 128b854 commit f939bab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1560
-956
lines changed

phpunit.xml.dist

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit bootstrap="./src/JonnyW/PhantomJs/Tests/bootstrap.php" colors="true">
3+
<phpunit
4+
bootstrap = "./src/JonnyW/PhantomJs/Tests/bootstrap.php"
5+
backupGlobals = "false"
6+
backupStaticAttributes = "false"
7+
colors = "true"
8+
convertErrorsToExceptions = "true"
9+
convertNoticesToExceptions = "true"
10+
convertWarningsToExceptions = "true"
11+
processIsolation = "false"
12+
stopOnFailure = "true"
13+
syntaxCheck = "false">
414
<testsuites>
515
<testsuite name="PhantomJS Test Suite">
616
<directory suffix="Test.php">./src/JonnyW/PhantomJs/Tests/*</directory>

src/JonnyW/PhantomJs/Cache/FileCache.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ public function fetch($id)
102102
*/
103103
public function delete($id)
104104
{
105-
if ($this->exists($id)) {
106-
unlink($this->getFilename($id));
105+
$files = glob($this->getFilename($id));
106+
107+
if (count($files)) {
108+
array_map('unlink', $files);
107109
}
108110
}
109111

src/JonnyW/PhantomJs/Client.php

+50-182
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
*/
99
namespace JonnyW\PhantomJs;
1010

11-
use JonnyW\PhantomJs\Exception\InvalidExecutableException;
1211
use JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface;
13-
use JonnyW\PhantomJs\Procedure\ProcedureValidatorInterface;
12+
use JonnyW\PhantomJs\Procedure\ProcedureCompilerInterface;
1413
use JonnyW\PhantomJs\Http\MessageFactoryInterface;
1514
use JonnyW\PhantomJs\Http\RequestInterface;
1615
use JonnyW\PhantomJs\Http\ResponseInterface;
@@ -31,6 +30,14 @@ class Client implements ClientInterface
3130
*/
3231
private static $instance;
3332

33+
/**
34+
* PhantomJs engine.
35+
*
36+
* @var \JonnyW\PhantomJs\Engine
37+
* @access protected
38+
*/
39+
protected $engine;
40+
3441
/**
3542
* Procedure loader.
3643
*
@@ -42,10 +49,10 @@ class Client implements ClientInterface
4249
/**
4350
* Procedure validator.
4451
*
45-
* @var \JonnyW\PhantomJs\Procedure\ProcedureValidatorInterface
52+
* @var \JonnyW\PhantomJs\Procedure\ProcedureCompilerInterface
4653
* @access protected
4754
*/
48-
protected $procedureValidator;
55+
protected $procedureCompiler;
4956

5057
/**
5158
* Message factory.
@@ -56,53 +63,30 @@ class Client implements ClientInterface
5663
protected $messageFactory;
5764

5865
/**
59-
* Path to PhantomJs executable
66+
* Procedure template
6067
*
6168
* @var string
6269
* @access protected
6370
*/
64-
protected $phantomJs;
65-
66-
/**
67-
* Debug flag.
68-
*
69-
* @var boolean
70-
* @access protected
71-
*/
72-
protected $debug;
73-
74-
/**
75-
* PhantomJs run options.
76-
*
77-
* @var array
78-
* @access protected
79-
*/
80-
protected $options;
81-
82-
/**
83-
* Log info
84-
*
85-
* @var string
86-
* @access protected
87-
*/
88-
protected $log;
71+
protected $procedure;
8972

9073
/**
9174
* Internal constructor
9275
*
9376
* @access public
94-
* @param \JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface $procedureLoader
95-
* @param \JonnyW\PhantomJs\Procedure\ProcedureValidatorInterface $procedureValidator
96-
* @param \JonnyW\PhantomJs\Http\MessageFactoryInterface $messageFactory
77+
* @param \JonnyW\PhantomJs\Engine $engine
78+
* @param \JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface $procedureLoader
79+
* @param \JonnyW\PhantomJs\Procedure\ProcedureCompilerInterface $procedureCompiler
80+
* @param \JonnyW\PhantomJs\Http\MessageFactoryInterface $messageFactory
9781
* @return void
9882
*/
99-
public function __construct(ProcedureLoaderInterface $procedureLoader, ProcedureValidatorInterface $procedureValidator, MessageFactoryInterface $messageFactory)
83+
public function __construct(Engine $engine, ProcedureLoaderInterface $procedureLoader, ProcedureCompilerInterface $procedureCompiler, MessageFactoryInterface $messageFactory)
10084
{
101-
$this->procedureLoader = $procedureLoader;
102-
$this->procedureValidator = $procedureValidator;
103-
$this->messageFactory = $messageFactory;
104-
$this->phantomJs = 'bin/phantomjs';
105-
$this->options = array();
85+
$this->engine = $engine;
86+
$this->procedureLoader = $procedureLoader;
87+
$this->procedureCompiler = $procedureCompiler;
88+
$this->messageFactory = $messageFactory;
89+
$this->procedure = 'http_default';
10690
}
10791

10892
/**
@@ -118,15 +102,27 @@ public static function getInstance()
118102
$serviceContainer = ServiceContainer::getInstance();
119103

120104
self::$instance = new static(
105+
$serviceContainer->get('engine'),
121106
$serviceContainer->get('procedure_loader'),
122-
$serviceContainer->get('procedure_validator'),
107+
$serviceContainer->get('procedure_compiler'),
123108
$serviceContainer->get('message_factory')
124109
);
125110
}
126111

127112
return self::$instance;
128113
}
129114

115+
/**
116+
* Get PhantomJs engine.
117+
*
118+
* @access public
119+
* @return \JonnyW\PhantomJs\Engine
120+
*/
121+
public function getEngine()
122+
{
123+
return $this->engine;
124+
}
125+
130126
/**
131127
* Get message factory instance
132128
*
@@ -159,174 +155,46 @@ public function getProcedureLoader()
159155
*/
160156
public function send(RequestInterface $request, ResponseInterface $response)
161157
{
162-
$procedure = $this->procedureLoader->load($request->getType());
158+
$procedure = $this->procedureLoader->load($this->procedure);
163159

164-
$this->procedureValidator->validate(
165-
$this,
166-
$procedure,
167-
$request
168-
);
160+
$this->procedureCompiler->compile($procedure, $request);
169161

170-
$procedure->run($this, $request, $response);
162+
$procedure->run($request, $response);
171163

172164
return $response;
173165
}
174166

175167
/**
176-
* Get PhantomJs run command with
177-
* loader and run options.
168+
* Get log.
178169
*
179170
* @access public
180171
* @return string
181172
*/
182-
public function getCommand()
183-
{
184-
$phantomJs = $this->getPhantomJs();
185-
$options = $this->getOptions();
186-
187-
$this->validateExecutable($phantomJs);
188-
189-
if ($this->debug) {
190-
array_push($options, '--debug=true');
191-
}
192-
193-
return sprintf('%s %s', $phantomJs, implode(' ', $options));
194-
}
195-
196-
/**
197-
* Set new PhantomJs executable path.
198-
*
199-
* @access public
200-
* @param string $path
201-
* @return \JonnyW\PhantomJs\Client
202-
*/
203-
public function setPhantomJs($path)
204-
{
205-
$this->validateExecutable($path);
206-
207-
$this->phantomJs = $path;
208-
209-
return $this;
210-
}
211-
212-
/**
213-
* Get PhantomJs executable path.
214-
*
215-
* @access public
216-
* @return string
217-
*/
218-
public function getPhantomJs()
219-
{
220-
return $this->phantomJs;
221-
}
222-
223-
/**
224-
* Set PhantomJs run options.
225-
*
226-
* @access public
227-
* @param array $options
228-
* @return \JonnyW\PhantomJs\Client
229-
*/
230-
public function setOptions(array $options)
231-
{
232-
$this->options = $options;
233-
234-
return $this;
235-
}
236-
237-
/**
238-
* Get PhantomJs run options.
239-
*
240-
* @access public
241-
* @return array
242-
*/
243-
public function getOptions()
244-
{
245-
return (array) $this->options;
246-
}
247-
248-
/**
249-
* Add single PhantomJs run option.
250-
*
251-
* @access public
252-
* @param string $option
253-
* @return \JonnyW\PhantomJs\Client
254-
*/
255-
public function addOption($option)
256-
{
257-
if (!in_array($option, $this->options)) {
258-
$this->options[] = $option;
259-
}
260-
261-
return $this;
262-
}
263-
264-
/**
265-
* Debug.
266-
*
267-
* @access public
268-
* @param boolean $doDebug
269-
* @return \JonnyW\PhantomJs\Client
270-
*/
271-
public function debug($doDebug)
173+
public function getLog()
272174
{
273-
$this->debug = $doDebug;
274-
275-
return $this;
175+
return $this->getEngine()->getLog();
276176
}
277177

278178
/**
279-
* Log info.
179+
* Set procedure template.
280180
*
281181
* @access public
282-
* @param string $info
283-
* @return \JonnyW\PhantomJs\Client
182+
* @param string $procedure
183+
* @return void
284184
*/
285-
public function log($info)
185+
public function setProcedure($procedure)
286186
{
287-
$this->log = $info;
288-
289-
return $this;
187+
$this->procedure = $procedure;
290188
}
291189

292190
/**
293-
* Get log info.
191+
* Get procedure template.
294192
*
295193
* @access public
296194
* @return string
297195
*/
298-
public function getLog()
196+
public function getProcedure()
299197
{
300-
return $this->log;
301-
}
302-
303-
/**
304-
* Clear log info.
305-
*
306-
* @access public
307-
* @return \JonnyW\PhantomJs\Client
308-
*/
309-
public function clearLog()
310-
{
311-
$this->log = '';
312-
313-
return $this;
314-
}
315-
316-
/**
317-
* Validate execuable file.
318-
*
319-
* @access private
320-
* @param string $file
321-
* @return boolean
322-
* @throws \JonnyW\PhantomJs\Exception\InvalidExecutableException
323-
*/
324-
private function validateExecutable($file)
325-
{
326-
if (!file_exists($file) || !is_executable($file)) {
327-
throw new InvalidExecutableException(sprintf('File does not exist or is not executable: %s', $file));
328-
}
329-
330-
return true;
198+
return $this->procedure;
331199
}
332200
}

0 commit comments

Comments
 (0)