| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace KamiYang\ProjectVersion\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * This file is part of the ProjectVersion project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * It is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * it under the terms of the GNU General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * For the full copyright and license information, please read | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * LICENSE file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use KamiYang\ProjectVersion\Configuration\ExtensionConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use KamiYang\ProjectVersion\Enumeration\GitCommandEnumeration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use KamiYang\ProjectVersion\Enumeration\ProjectVersionModeEnumeration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use KamiYang\ProjectVersion\Facade\CommandUtilityFacade; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use KamiYang\ProjectVersion\Facade\SystemEnvironmentBuilderFacade; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use TYPO3\CMS\Core\SingletonInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use TYPO3\CMS\Core\Utility\GeneralUtility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * Class ProjectVersionService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | class ProjectVersionService implements SingletonInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var \KamiYang\ProjectVersion\Facade\CommandUtilityFacade | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected $commandUtility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var \KamiYang\ProjectVersion\Facade\SystemEnvironmentBuilderFacade | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $systemEnvironmentBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * ProjectVersionService constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param \KamiYang\ProjectVersion\Facade\SystemEnvironmentBuilderFacade $systemEnvironmentBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param \KamiYang\ProjectVersion\Facade\CommandUtilityFacade $commandUtility | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         CommandUtilityFacade $commandUtility, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         SystemEnvironmentBuilderFacade $systemEnvironmentBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->systemEnvironmentBuilder = $systemEnvironmentBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->commandUtility = $commandUtility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function getProjectVersion(): ProjectVersion | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $projectVersion = GeneralUtility::makeInstance(ProjectVersion::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         switch (ExtensionConfiguration::getMode()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             case ProjectVersionModeEnumeration::STATIC_VERSION: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 $this->setStaticVersion($projectVersion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             case ProjectVersionModeEnumeration::GIT: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $this->setVersionFromGit($projectVersion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             case ProjectVersionModeEnumeration::GIT_FILE_FALLBACK: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $this->setVersionFromGit($projectVersion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 if ($projectVersion->getVersion() === ProjectVersion::UNKNOWN_VERSION) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     //if version is still unknown, try to resolve version by file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     $this->setVersionFromFile($projectVersion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             case ProjectVersionModeEnumeration::FILE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $this->setVersionFromFile($projectVersion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return $projectVersion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param $revision | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param $tag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param $branch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     private function formatVersionBasedOnConfiguration($revision, $tag, $branch): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         switch (ExtensionConfiguration::getGitFormat()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             case GitCommandEnumeration::FORMAT_REVISION: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 $format = $revision; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             case GitCommandEnumeration::FORMAT_REVISION_TAG: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 $format = \sprintf('[%s] %s', $revision, $tag); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             case GitCommandEnumeration::FORMAT_BRANCH: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $format = $branch; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             case GitCommandEnumeration::FORMAT_TAG: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $format = $tag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             case GitCommandEnumeration::FORMAT_REVISION_BRANCH: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $format = \sprintf('[%s] %s', $revision, $branch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return $format; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     protected function isGitAvailable(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         return $this->systemEnvironmentBuilder->isFunctionDisabled('exec') === false && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             // check if git exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $this->commandUtility->exec('git --version', $_, $returnCode) && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $returnCode === 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param \KamiYang\ProjectVersion\Service\ProjectVersion $projectVersion | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     private function setStaticVersion(ProjectVersion $projectVersion): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $projectVersion->setVersion(ExtensionConfiguration::getStaticVersion()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * Resolve version by common VERSION-file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param \KamiYang\ProjectVersion\Service\ProjectVersion $projectVersion | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     private function setVersionFromFile(ProjectVersion $projectVersion): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $versionFilePath = ExtensionConfiguration::getAbsVersionFilePath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         if (\file_exists($versionFilePath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             $versionFileContent = \file_get_contents($versionFilePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             $projectVersion->setVersion($versionFileContent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param \KamiYang\ProjectVersion\Service\ProjectVersion $projectVersion | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 148 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |     private function setVersionFromGit(ProjectVersion $projectVersion): void | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |         if ($this->isGitAvailable() === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |         $version = $this->getVersionByFormat(); | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |         if (!empty($version)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |             $projectVersion->setVersion($version); | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |             $projectVersion->setIconIdentifier('information-git'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     private function getVersionByFormat(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $branch = \trim($this->commandUtility->exec(GitCommandEnumeration::CMD_BRANCH)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         $revision = \trim($this->commandUtility->exec(GitCommandEnumeration::CMD_REVISION)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         $tag = \trim($this->commandUtility->exec(GitCommandEnumeration::CMD_TAG)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         $format = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         if ($branch || $revision || $tag) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             $format = $this->formatVersionBasedOnConfiguration($revision, $tag, $branch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         return $format; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 179 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 180 |  |  |  |