| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Paysera\PhpStormHelper\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use DOMElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use DOMNode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Paysera\PhpStormHelper\Entity\SourceFolder; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | class ConfigurationOptionFinder | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     private $domHelper; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 | 6 |  |     public function __construct(DomHelper $domHelper) | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 | 6 |  |         $this->domHelper = $domHelper; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 6 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 5 |  |     public function findUsedDockerImage(string $projectRootDir) | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 5 |  |         $phpXmlFilePath = $projectRootDir . '/.idea/php.xml'; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 5 |  |         if (!file_exists($phpXmlFilePath)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 5 |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         $contents = file_get_contents($phpXmlFilePath); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         preg_match('/DOCKER_IMAGE_NAME="([^"]+)"/', $contents, $matches); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         return $matches[1] ?? null; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 5 |  |     public function findWebpackConfigPath(string $projectRootDir) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 5 |  |         $phpXmlFilePath = $projectRootDir . '/.idea/misc.xml'; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 5 |  |         if (!file_exists($phpXmlFilePath)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 5 |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $document = $this->domHelper->loadDocument($phpXmlFilePath); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $projectElement = $this->domHelper->findNode($document, 'project'); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $component = $this->domHelper->findOptionalNode($projectElement, 'component', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             'name' => 'WebPackConfiguration', | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         if ($component === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $option = $this->domHelper->findOptionalNode($component, 'option', ['name' => 'path']); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         if ($option === null || !$option instanceof DOMElement) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $value = $option->getAttribute('value'); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $prefix = '$PROJECT_DIR$/'; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         if (strpos($value, $prefix) !== 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         return mb_substr($value, mb_strlen($prefix)); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @param string $projectRootDir | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @return array|SourceFolder[] | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 6 |  |     public function findConfiguredSourceFolders(string $projectRootDir): array | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 6 |  |         $projectName = basename($projectRootDir); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 6 |  |         $configPath = $projectRootDir . '/.idea/' . $projectName . '.iml'; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 6 |  |         if (!file_exists($configPath)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 6 |  |             return []; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         $document = $this->domHelper->loadDocument($configPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         $moduleElement = $this->domHelper->findNode($document, 'module'); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         $componentElement = $this->domHelper->findNode($moduleElement, 'component', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             'name' => 'NewModuleRootManager', | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         $contentElement = $this->domHelper->findNode($componentElement, 'content'); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         $sources = $this->domHelper->findNodes($contentElement, 'sourceFolder', ['isTestSource' => 'false']); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         $testSources = $this->domHelper->findNodes($contentElement, 'sourceFolder', ['isTestSource' => 'true']); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         $excludes = $this->domHelper->findNodes($contentElement, 'excludeFolder'); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         return array_merge( | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             $this->extractFolders($sources, SourceFolder::TYPE_SOURCE), | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             $this->extractFolders($testSources, SourceFolder::TYPE_TEST_SOURCE), | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             $this->extractFolders($excludes, SourceFolder::TYPE_EXCLUDED) | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * @param array|DomNode[] $elements | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * @param string $type | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * @return array|SourceFolder[] | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     private function extractFolders(array $elements, string $type): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $folders = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         foreach ($elements as $element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $url = $element->attributes->getNamedItem('url')->nodeValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $folder = new SourceFolder(mb_substr($url, mb_strlen('file://$MODULE_DIR$/')), $type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $packagePrefixAttribute = $element->attributes->getNamedItem('packagePrefix'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             if ($packagePrefixAttribute !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $folder->setPackagePrefix($packagePrefixAttribute->nodeValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $folders[] = $folder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return $folders; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 115 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 116 |  |  |  |