| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Bex\Behat\BehatRSTSpecificationLocatorExtension\RST\Loader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Behat\Gherkin\Cache\CacheInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Behat\Gherkin\Loader\AbstractFileLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Behat\Gherkin\Node\FeatureNode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Bex\Behat\BehatRSTSpecificationLocatorExtension\RST\Parser as RSTParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Notes: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  *   It was necessary to extend the AbstractFileLoader instead of implementing \Behat\Gherkin\Loader\FileLoaderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  *   because I had to get access to the findAbsolutePath method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  *   Fun fact: The findAbsolutePath method is the exact same method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *   as the one in \Behat\Behat\Gherkin\Specification\Locator\FilesystemFeatureLocator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  *   so it is already duplicated in the main project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *   @TODO Create some kind of service which exposes a public findAbsolutePath and contribute it back to Behat | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *         to avoid copy-pasting and relying on implementation details like this AbstractFileLoader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class RSTFileLoader extends AbstractFileLoader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** @var RSTParser */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $rstParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** @var CacheInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function __construct(RSTParser $rstParser, CacheInterface $cache, string $basePath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->rstParser = $rstParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->cache = $cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->basePath = $basePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function supports($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return is_string($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             && is_file($absolute = $this->findAbsolutePath($path)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             && 'rst' === pathinfo($absolute, PATHINFO_EXTENSION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * Loads features from provided resource. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param string $path Resource to load | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @return FeatureNode[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function load($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $path = $this->findAbsolutePath($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         // TODO clean this up a bit, copied over from GherkinFileLoader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if ($this->cache->isFresh($path, filemtime($path))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $feature = $this->cache->read($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } elseif (null !== $feature = $this->parseFeature($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $this->cache->write($path, $feature); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return null !== $feature ? [$feature] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |     private function parseFeature(string $path): ?FeatureNode | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |         return $this->rstParser->parse(file_get_contents($path), $path); | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 70 |  |  | } |