1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the php-phantomjs. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace JonnyW\PhantomJs\Procedure; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
12
|
|
|
use JonnyW\PhantomJs\Exception\NotExistsException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* PHP PhantomJs |
16
|
|
|
* |
17
|
|
|
* @author Jon Wenmoth <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class ProcedureLoader implements ProcedureLoaderInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Procedure factory. |
23
|
|
|
* |
24
|
|
|
* @var \JonnyW\PhantomJs\Procedure\ProcedureFactoryInterface |
25
|
|
|
* @access protected |
26
|
|
|
*/ |
27
|
|
|
protected $procedureFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* File locator. |
31
|
|
|
* |
32
|
|
|
* @var \Symfony\Component\Config\FileLocatorInterface |
33
|
|
|
* @access protected |
34
|
|
|
*/ |
35
|
|
|
protected $locator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Internal constructor. |
39
|
|
|
* |
40
|
|
|
* @access public |
41
|
|
|
* @param \JonnyW\PhantomJs\Procedure\ProcedureFactoryInterface $procedureFactory |
42
|
|
|
* @param \Symfony\Component\Config\FileLocatorInterface $locator |
43
|
|
|
*/ |
44
|
9 |
|
public function __construct(ProcedureFactoryInterface $procedureFactory, FileLocatorInterface $locator) |
45
|
|
|
{ |
46
|
9 |
|
$this->procedureFactory = $procedureFactory; |
47
|
9 |
|
$this->locator = $locator; |
48
|
9 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Load procedure instance by id. |
52
|
|
|
* |
53
|
|
|
* @access public |
54
|
|
|
* @param string $id |
55
|
|
|
* @return \JonnyW\PhantomJs\Procedure\ProcedureInterface |
56
|
|
|
*/ |
57
|
51 |
|
public function load($id) |
58
|
|
|
{ |
59
|
51 |
|
$procedure = $this->procedureFactory->createProcedure(); |
60
|
51 |
|
$procedure->setTemplate( |
61
|
51 |
|
$this->loadTemplate($id) |
62
|
49 |
|
); |
63
|
|
|
|
64
|
49 |
|
return $procedure; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Load procedure template by id. |
69
|
|
|
* |
70
|
|
|
* @access public |
71
|
|
|
* @param string $id |
72
|
|
|
* @param string $extension (default: 'proc') |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
52 |
|
public function loadTemplate($id, $extension = 'proc') |
76
|
|
|
{ |
77
|
52 |
|
$path = $this->locator->locate(sprintf('%s.%s', $id, $extension)); |
78
|
|
|
|
79
|
52 |
|
return $this->loadFile($path); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Load procedure file content. |
84
|
|
|
* |
85
|
|
|
* @access protected |
86
|
|
|
* @param string $file |
87
|
|
|
* @return string |
88
|
|
|
* @throws \InvalidArgumentException |
89
|
|
|
* @throws \JonnyW\PhantomJs\Exception\NotExistsException |
90
|
|
|
*/ |
91
|
52 |
|
protected function loadFile($file) |
92
|
|
|
{ |
93
|
52 |
|
if (!stream_is_local($file)) { |
94
|
1 |
|
throw new \InvalidArgumentException(sprintf('Procedure file is not a local file: "%s"', $file)); |
95
|
|
|
} |
96
|
|
|
|
97
|
51 |
|
if (!file_exists($file)) { |
98
|
1 |
|
throw new NotExistsException(sprintf('Procedure file does not exist: "%s"', $file)); |
99
|
|
|
} |
100
|
|
|
|
101
|
50 |
|
return file_get_contents($file); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.