|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\BehatExtension\Compiler; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Behat\SilverStripe container compilation pass. |
|
10
|
|
|
* Passes Base URL available in MinkExtension config. |
|
11
|
|
|
* Used for the {@link \SilverStripe\BehatExtension\MinkExtension} subclass. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Michał Ochman <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class MinkExtensionBaseUrlPass implements CompilerPassInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Passes MinkExtension's base_url parameter |
|
19
|
|
|
* |
|
20
|
|
|
* @param ContainerBuilder $container |
|
21
|
|
|
*/ |
|
22
|
|
|
public function process(ContainerBuilder $container) |
|
23
|
|
|
{ |
|
24
|
|
|
$frameworkPath = $container->getParameter('behat.silverstripe_extension.framework_path'); |
|
25
|
|
|
|
|
26
|
|
|
global $_FILE_TO_URL_MAPPING; |
|
27
|
|
|
if ($container->getParameter('behat.mink.base_url')) { |
|
28
|
|
|
// If base_url is already defined, also set it in the SilverStripe mapping |
|
29
|
|
|
$_FILE_TO_URL_MAPPING[dirname($frameworkPath)] = $container->getParameter('behat.mink.base_url'); |
|
30
|
|
|
} elseif ($envPath = $this->findEnvironmentConfigFile($frameworkPath)) { |
|
31
|
|
|
// Otherwise try to retrieve it from _ss_environment |
|
32
|
|
|
include_once $envPath; |
|
33
|
|
|
if (isset($_FILE_TO_URL_MAPPING) |
|
34
|
|
|
&& !($container->hasParameter('behat.mink.base_url') && $container->getParameter('behat.mink.base_url')) |
|
35
|
|
|
) { |
|
36
|
|
|
$baseUrl = $this->findBaseUrlFromMapping(dirname($frameworkPath), $_FILE_TO_URL_MAPPING); |
|
37
|
|
|
if ($baseUrl) { |
|
|
|
|
|
|
38
|
|
|
$container->setParameter('behat.mink.base_url', $baseUrl); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (!$container->getParameter('behat.mink.base_url')) { |
|
44
|
|
|
throw new \InvalidArgumentException( |
|
45
|
|
|
'"base_url" not configured. Please specify it in your behat.yml configuration, ' . |
|
46
|
|
|
'or in your _ss_environment.php configuration through $_FILE_TO_URL_MAPPING' |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// The Behat\MinkExtension\Extension class copies configuration into an internal hash, |
|
51
|
|
|
// we need to follow this pattern to propagate our changes. |
|
52
|
|
|
$parameters = $container->getParameter('behat.mink.parameters'); |
|
53
|
|
|
$parameters['base_url'] = $container->getParameter('behat.mink.base_url'); |
|
54
|
|
|
$container->setParameter('behat.mink.parameters', $parameters); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Try to auto-detect host for webroot based on _ss_environment.php data (unless explicitly set in behat.yml) |
|
59
|
|
|
* Copied logic from Core.php, because it needs to be executed prior to {@link SilverStripeAwareInitializer}. |
|
60
|
|
|
* |
|
61
|
|
|
* @param String Absolute start path to search upwards from |
|
62
|
|
|
* @return Boolean Absolute path to environment file |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function findEnvironmentConfigFile($path) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
$envPath = null; |
|
67
|
|
|
$envFile = '_ss_environment.php'; //define the name of the environment file |
|
68
|
|
|
$path = '.'; //define the dir to start scanning from (have to add the trailing slash) |
|
69
|
|
|
|
|
70
|
|
|
//check this dir and every parent dir (until we hit the base of the drive) |
|
71
|
|
|
do { |
|
72
|
|
|
$path = realpath($path) . '/'; |
|
73
|
|
|
//if the file exists, then we include it, set relevant vars and break out |
|
74
|
|
|
if (file_exists($path . $envFile)) { |
|
75
|
|
|
$envPath = $path . $envFile; |
|
76
|
|
|
break; |
|
77
|
|
|
} |
|
78
|
|
|
// here we need to check that the real path of the last dir and the next one are |
|
79
|
|
|
// not the same, if they are, we have hit the root of the drive |
|
80
|
|
|
} while (realpath($path) != realpath($path .= '../')); |
|
81
|
|
|
|
|
82
|
|
|
return $envPath; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Copied logic from Core.php, because it needs to be executed prior to {@link SilverStripeAwareInitializer}. |
|
87
|
|
|
* |
|
88
|
|
|
* @param String Absolute start path to search upwards from |
|
89
|
|
|
* @param Array Map of paths to host names |
|
90
|
|
|
* @return String URL |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function findBaseUrlFromMapping($path, $mapping) |
|
93
|
|
|
{ |
|
94
|
|
|
$fullPath = $path; |
|
95
|
|
|
$url = null; |
|
96
|
|
|
while ($path && $path != "/" && !preg_match('/^[A-Z]:\\\\$/', $path)) { |
|
97
|
|
|
if (isset($mapping[$path])) { |
|
98
|
|
|
$url = $mapping[$path] . str_replace(DIRECTORY_SEPARATOR, '/', substr($fullPath, strlen($path))); |
|
99
|
|
|
break; |
|
100
|
|
|
} else { |
|
101
|
|
|
$path = dirname($path); // traverse up |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $url; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: