| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Superdesk Web Publisher Core Bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright 2017 Sourcefabric z.u. and contributors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please see the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * AUTHORS and LICENSE files distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright 2017 Sourcefabric z.ú | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license http://www.superdesk.org/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace SWP\Bundle\CoreBundle\Theme\Locator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use SWP\Bundle\CoreBundle\Theme\Provider\TenantThemesPathsProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Sylius\Bundle\ThemeBundle\Locator\FileLocatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sylius\Bundle\ThemeBundle\Factory\FinderFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Finder\SplFileInfo; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | final class TenantThemesConfigurationFileLocator implements FileLocatorInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     private $finderFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     private $paths; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     private $tenantThemesPathsProvider; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function __construct(FinderFactoryInterface $finderFactory, array $paths, TenantThemesPathsProviderInterface $tenantThemesPathsProvider) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->finderFactory = $finderFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->paths = $paths; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->tenantThemesPathsProvider = $tenantThemesPathsProvider; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function locateFileNamed(string $name): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return $this->doLocateFilesNamed($name)->current(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function locateFilesNamed(string $name): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         return iterator_to_array($this->doLocateFilesNamed($name)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     private function doLocateFilesNamed($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->assertNameIsNotEmpty($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $found = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         foreach ($this->tenantThemesPathsProvider->getTenantThemesPaths($this->paths) as $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $finder = $this->finderFactory->create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $finder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     ->files() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     ->followLinks() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     ->name($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     ->depth(['>= 1', '< 3']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     ->ignoreUnreadableDirs() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     ->in($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 /** @var SplFileInfo $file */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 foreach ($finder as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                     $found = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     yield $file->getPathname(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } catch (\InvalidArgumentException $exception) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if (false === $found) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             throw new \InvalidArgumentException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 'The file "%s" does not exist (searched in the following directories: %s).', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 implode(', ', $this->paths) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     private function assertNameIsNotEmpty(?string $name): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if (null === $name || '' === $name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             throw new \InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 'An empty file name is not valid to be located.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  |