@@ -45,21 +45,19 @@ class Version
4545 protected $ package ;
4646 protected $ header ;
4747 protected $ version ;
48- protected $ macroName ;
48+ protected $ macroNameRegex ;
4949
5050 public function __construct (Interfaces \Package $ package )
5151 {
5252 $ this ->package = $ package ;
53- $ this ->macroName = strtoupper ($ this ->package ->getSimpleName ()).'_VERSION ' ;
53+ $ this ->macroNameRegex = ' (?:PHP_)? ' . strtoupper ($ this ->package ->getSimpleName ()).'_(?:EXT_)?VERSION ' ;
5454
5555 $ this ->version = $ this ->getVersionFromHeader ();
5656 }
5757
5858 public function fileHasVersionMacro ($ fname )
5959 {
60- $ cont = file_get_contents ($ fname );
61-
62- return false !== strstr ($ cont , $ this ->macroName );
60+ return $ this ->getVersionFromHeaderFile ($ fname ) !== null ;
6361 }
6462
6563 private function rglob ($ pattern , $ flags = 0 ) {
@@ -73,22 +71,44 @@ private function rglob($pattern, $flags = 0) {
7371 public function getVersionFromHeader ()
7472 {
7573 $ headers = self ::rglob ($ this ->package ->getSourceDir ().DIRECTORY_SEPARATOR .'*.h ' );
74+ foreach ($ headers as $ header ) {
75+ $ macroValue = $ this ->getVersionFromHeaderFile ($ header );
76+ if ($ macroValue !== null ) {
77+ return $ macroValue ;
78+ }
79+ }
80+ throw new \Exception ("Couldn't parse or find the version defined in the {$ this ->macroNameRegex } macro " );
81+ }
82+
83+ /**
84+ * Extract the macro value from a header file.
85+ *
86+ * @throws \Exception if we couldn't read the file
87+ *
88+ * @return string|null return NULL if the macro couldn't be found, the macro value otherwise
89+ */
90+ protected function getVersionFromHeaderFile (string $ headerFilePath ): ?string
91+ {
92+ $ headerFileContents = file_get_contents ($ headerFilePath );
93+ if ($ headerFileContents === false ) {
94+ throw new \Exception ("Could not read header file {$ headerFilePath }" );
95+ }
7696
97+ return $ this ->getVersionFromHeaderFileContents ($ headerFileContents );
98+ }
99+
100+ /**
101+ * Extract the macro value from the contents of a header file.
102+ *
103+ * @return string|null return NULL if the macro couldn't be found, the macro value otherwise
104+ */
105+ protected function getVersionFromHeaderFileContents (string $ headerFileContents ): ?string
106+ {
77107 // Match versions surrounded by quotes and versions without quotes
78108 $ versionMatcher = '(".*"|.*\b) ' ;
79- $ pat = ',define\s+(?:PHP_)? ' . preg_quote ( $ this ->macroName , ' , ' ). '\s+ ' . $ versionMatcher. ',i ' ;
109+ $ pat = ',define\s+ ' . $ this ->macroNameRegex . '\s+ ' . $ versionMatcher . ',i ' ;
80110
81- foreach ($ headers as $ header ) {
82- $ headerContent = file_get_contents ($ header );
83- if (!$ headerContent ) {
84- throw new \Exception ("Could not read $ header " );
85- }
86- if (preg_match ($ pat , $ headerContent , $ result )) {
87- // Remove any quote characters we may have matched on
88- return trim ($ result [1 ], '" ' );
89- }
90- }
91- throw new \Exception ("Couldn't parse or find the version defined in the {$ this ->macroName } macro " );
111+ return preg_match ($ pat , $ headerFileContents , $ result ) ? trim ($ result [1 ], '" ' ) : null ;
92112 }
93113
94114 public function updateJSON ()
0 commit comments