Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Gherkin / Suite / Setup / SuiteWithPathsSetup.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Behat\Gherkin\Suite\Setup;
12
13 use Behat\Testwork\Filesystem\FilesystemLogger;
14 use Behat\Testwork\Suite\Setup\SuiteSetup;
15 use Behat\Testwork\Suite\Suite;
16
17 /**
18  * Sets up gherkin suite in the filesystem (creates feature folders).
19  *
20  * @author Konstantin Kudryashov <ever.zet@gmail.com>
21  */
22 final class SuiteWithPathsSetup implements SuiteSetup
23 {
24     /**
25      * @var string
26      */
27     private $basePath;
28     /**
29      * @var null|FilesystemLogger
30      */
31     private $logger;
32
33     /**
34      * Initializes setup.
35      *
36      * @param string                $basePath
37      * @param null|FilesystemLogger $logger
38      */
39     public function __construct($basePath, FilesystemLogger $logger = null)
40     {
41         $this->basePath = $basePath;
42         $this->logger = $logger;
43     }
44
45     /**
46      * {@inheritdoc}
47      */
48     public function supportsSuite(Suite $suite)
49     {
50         return $suite->hasSetting('paths') && is_array($suite->getSetting('paths'));
51     }
52
53     /**
54      * {@inheritdoc}
55      */
56     public function setupSuite(Suite $suite)
57     {
58         foreach ($suite->getSetting('paths') as $locator) {
59             if (0 !== strpos($locator, '@') && !is_dir($path = $this->locatePath($locator))) {
60                 $this->createFeatureDirectory($path);
61             }
62         }
63     }
64
65     /**
66      * Creates feature directory.
67      *
68      * @param string $path
69      */
70     private function createFeatureDirectory($path)
71     {
72         mkdir($path, 0777, true);
73
74         if ($this->logger) {
75             $this->logger->directoryCreated($path, 'place your *.feature files here');
76         }
77     }
78
79     /**
80      * Locates path from a relative one.
81      *
82      * @param string $path
83      *
84      * @return string
85      */
86     private function locatePath($path)
87     {
88         if ($this->isAbsolutePath($path)) {
89             return $path;
90         }
91
92         return $this->basePath . DIRECTORY_SEPARATOR . $path;
93     }
94
95     /**
96      * Returns whether the file path is an absolute path.
97      *
98      * @param string $file A file path
99      *
100      * @return Boolean
101      */
102     private function isAbsolutePath($file)
103     {
104         if ($file[0] == '/' || $file[0] == '\\'
105             || (strlen($file) > 3 && ctype_alpha($file[0])
106                 && $file[1] == ':'
107                 && ($file[2] == '\\' || $file[2] == '/')
108             )
109             || null !== parse_url($file, PHP_URL_SCHEME)
110         ) {
111             return true;
112         }
113
114         return false;
115     }
116 }