From a0e7aec228b9e1ebcfaf285471de37a2b1c6d804 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Mon, 14 Mar 2022 23:35:08 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Parse=20nginx=20files=20+=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP Monitor.xcodeproj/project.pbxproj | 12 +++ phpmon-tests/Parsers/NginxParserTest.swift | 26 +++++ phpmon-tests/Test Files/nicoverbruggen.test | 93 ++++++++++++++++++ .../Test Files/nicoverbruggen_isolated.test | 94 +++++++++++++++++++ .../Versions/PhpVersionDetectionTest.swift | 2 +- .../Versions/ValetVersionExtractorTest.swift | 2 +- .../Domain/Integrations/Valet/ValetSite.swift | 36 ++++++- 7 files changed, 260 insertions(+), 5 deletions(-) create mode 100644 phpmon-tests/Parsers/NginxParserTest.swift create mode 100644 phpmon-tests/Test Files/nicoverbruggen.test create mode 100644 phpmon-tests/Test Files/nicoverbruggen_isolated.test diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 5002ac6..307ab11 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -66,6 +66,9 @@ C42759672627662800093CAE /* NSMenuExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C42759662627662800093CAE /* NSMenuExtension.swift */; }; C42759682627662800093CAE /* NSMenuExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C42759662627662800093CAE /* NSMenuExtension.swift */; }; C42C49DB27C2806F0074ABAC /* MainMenu+FixMyValet.swift in Sources */ = {isa = PBXBuildFile; fileRef = C42C49DA27C2806F0074ABAC /* MainMenu+FixMyValet.swift */; }; + C42CFB1627DFDE7900862737 /* nicoverbruggen.test in Resources */ = {isa = PBXBuildFile; fileRef = C42CFB1527DFDE7900862737 /* nicoverbruggen.test */; }; + C42CFB1827DFDFDC00862737 /* nicoverbruggen_isolated.test in Resources */ = {isa = PBXBuildFile; fileRef = C42CFB1727DFDFDC00862737 /* nicoverbruggen_isolated.test */; }; + C42CFB1A27DFE8BD00862737 /* NginxParserTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C42CFB1927DFE8BD00862737 /* NginxParserTest.swift */; }; C43603A0275E67610028EFC6 /* AppDelegate+Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = C436039F275E67610028EFC6 /* AppDelegate+Notifications.swift */; }; C43603A1275E67610028EFC6 /* AppDelegate+Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = C436039F275E67610028EFC6 /* AppDelegate+Notifications.swift */; }; C43A8A1A25D9CD1000591B77 /* Utility.swift in Sources */ = {isa = PBXBuildFile; fileRef = C43A8A1925D9CD1000591B77 /* Utility.swift */; }; @@ -254,6 +257,9 @@ C4232EE42612526500158FC6 /* Credits.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = Credits.html; sourceTree = ""; }; C42759662627662800093CAE /* NSMenuExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSMenuExtension.swift; sourceTree = ""; }; C42C49DA27C2806F0074ABAC /* MainMenu+FixMyValet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MainMenu+FixMyValet.swift"; sourceTree = ""; }; + C42CFB1527DFDE7900862737 /* nicoverbruggen.test */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = nicoverbruggen.test; sourceTree = ""; }; + C42CFB1727DFDFDC00862737 /* nicoverbruggen_isolated.test */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = nicoverbruggen_isolated.test; sourceTree = ""; }; + C42CFB1927DFE8BD00862737 /* NginxParserTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NginxParserTest.swift; sourceTree = ""; }; C436039F275E67610028EFC6 /* AppDelegate+Notifications.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppDelegate+Notifications.swift"; sourceTree = ""; }; C43A8A1925D9CD1000591B77 /* Utility.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utility.swift; sourceTree = ""; }; C43A8A1F25D9D1D700591B77 /* brew.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = brew.json; sourceTree = ""; }; @@ -417,6 +423,8 @@ C40C7F1C27720E1400DDDCDC /* Test Files */ = { isa = PBXGroup; children = ( + C42CFB1527DFDE7900862737 /* nicoverbruggen.test */, + C42CFB1727DFDFDC00862737 /* nicoverbruggen_isolated.test */, C4AF9F70275445FF00D44ED0 /* valet-config.json */, C43A8A1F25D9D1D700591B77 /* brew.json */, C4F30B06278E195800755FCE /* brew-services.json */, @@ -639,6 +647,7 @@ C4AF9F76275447F100D44ED0 /* ValetConfigParserTest.swift */, C4F780AD25D80B37000DBC97 /* ExtensionParserTest.swift */, C43A8A2325D9D20D00591B77 /* BrewJsonParserTest.swift */, + C42CFB1927DFE8BD00862737 /* NginxParserTest.swift */, ); path = Parsers; sourceTree = ""; @@ -863,12 +872,14 @@ files = ( 54FCFD27276C883F004CE748 /* SelectPreferenceView.xib in Resources */, 54FCFD2E276C8D67004CE748 /* HotkeyPreferenceView.xib in Resources */, + C42CFB1827DFDFDC00862737 /* nicoverbruggen_isolated.test in Resources */, C4F780A825D80AE8000DBC97 /* php.ini in Resources */, C4068CA527B0780A00544CD5 /* CheckboxPreferenceView.xib in Resources */, C43A8A2025D9D1D700591B77 /* brew.json in Resources */, C4AF9F72275445FF00D44ED0 /* valet-config.json in Resources */, C44C1992276E44CB0072762D /* ProgressWindow.storyboard in Resources */, C4F30B08278E195800755FCE /* brew-services.json in Resources */, + C42CFB1627DFDE7900862737 /* nicoverbruggen.test in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1031,6 +1042,7 @@ C44C198E276E3A1C0072762D /* ProgressWindow.swift in Sources */, C415938027A1B54F00D2E1B7 /* PhpFrameworks.swift in Sources */, C4D9ADC9277611A0007277F4 /* InternalSwitcher.swift in Sources */, + C42CFB1A27DFE8BD00862737 /* NginxParserTest.swift in Sources */, C4F30B0B278E203C00755FCE /* MainMenu+Startup.swift in Sources */, C40B24F227A310770018C7D2 /* Events.swift in Sources */, C4F30B0A278E1A1A00755FCE /* ComposerJson.swift in Sources */, diff --git a/phpmon-tests/Parsers/NginxParserTest.swift b/phpmon-tests/Parsers/NginxParserTest.swift new file mode 100644 index 0000000..105ad77 --- /dev/null +++ b/phpmon-tests/Parsers/NginxParserTest.swift @@ -0,0 +1,26 @@ +// +// NginxParserTest.swift +// phpmon-tests +// +// Created by Nico Verbruggen on 29/11/2021. +// Copyright © 2021 Nico Verbruggen. All rights reserved. +// + +import XCTest + +class NginxParserTest: XCTestCase { + + static var regularUrl: URL { + return Bundle(for: Self.self).url(forResource: "nicoverbruggen", withExtension: "test")! + } + + static var isolatedUrl: URL { + return Bundle(for: Self.self).url(forResource: "nicoverbruggen_isolated", withExtension: "test")! + } + + func testCanDetermineIsolation() throws { + XCTAssertNil(ValetSite.isolatedVersion(NginxParserTest.regularUrl.path)) + XCTAssertEqual("8.1", ValetSite.isolatedVersion(NginxParserTest.isolatedUrl.path)) + } + +} diff --git a/phpmon-tests/Test Files/nicoverbruggen.test b/phpmon-tests/Test Files/nicoverbruggen.test new file mode 100644 index 0000000..bf929f0 --- /dev/null +++ b/phpmon-tests/Test Files/nicoverbruggen.test @@ -0,0 +1,93 @@ +server { + listen 127.0.0.1:80; + #listen 127.0.0.1:80; # valet loopback + server_name nicoverbruggen.test www.nicoverbruggen.test *.nicoverbruggen.test; + return 301 https://$host$request_uri; +} + +server { + listen 127.0.0.1:443 ssl http2; + #listen 127.0.0.1:443 ssl http2; # valet loopback + server_name nicoverbruggen.test www.nicoverbruggen.test *.nicoverbruggen.test; + root /; + charset utf-8; + client_max_body_size 512M; + http2_push_preload on; + + location /41c270e4-5535-4daa-b23e-c269744c2f45/ { + internal; + alias /; + try_files $uri $uri/; + } + + ssl_certificate "/Users/nicoverbruggen/.config/valet/Certificates/nicoverbruggen.test.crt"; + ssl_certificate_key "/Users/nicoverbruggen/.config/valet/Certificates/nicoverbruggen.test.key"; + + location / { + rewrite ^ "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php" last; + } + + location = /favicon.ico { access_log off; log_not_found off; } + location = /robots.txt { access_log off; log_not_found off; } + + access_log off; + error_log "/Users/nicoverbruggen/.config/valet/Log/nginx-error.log"; + + error_page 404 "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php"; + + location ~ [^/]\.php(/|$) { + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass "unix:/Users/nicoverbruggen/.config/valet/valet.sock"; + fastcgi_index "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php"; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php"; + fastcgi_param PATH_INFO $fastcgi_path_info; + } + + location ~ /\.ht { + deny all; + } +} + +server { + listen 127.0.0.1:60; + #listen 127.0.0.1:60; # valet loopback + server_name nicoverbruggen.test www.nicoverbruggen.test *.nicoverbruggen.test; + root /; + charset utf-8; + client_max_body_size 128M; + + add_header X-Robots-Tag 'noindex, nofollow, nosnippet, noarchive'; + + location /41c270e4-5535-4daa-b23e-c269744c2f45/ { + internal; + alias /; + try_files $uri $uri/; + } + + location / { + rewrite ^ "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php" last; + } + + location = /favicon.ico { access_log off; log_not_found off; } + location = /robots.txt { access_log off; log_not_found off; } + + access_log off; + error_log "/Users/nicoverbruggen/.config/valet/Log/nginx-error.log"; + + error_page 404 "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php"; + + location ~ [^/]\.php(/|$) { + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass "unix:/Users/nicoverbruggen/.config/valet/valet.sock"; + fastcgi_index "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php"; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php"; + fastcgi_param PATH_INFO $fastcgi_path_info; + } + + location ~ /\.ht { + deny all; + } +} + diff --git a/phpmon-tests/Test Files/nicoverbruggen_isolated.test b/phpmon-tests/Test Files/nicoverbruggen_isolated.test new file mode 100644 index 0000000..d436a75 --- /dev/null +++ b/phpmon-tests/Test Files/nicoverbruggen_isolated.test @@ -0,0 +1,94 @@ +server { + listen 127.0.0.1:80; + #listen 127.0.0.1:80; # valet loopback + server_name nicoverbruggen.test www.nicoverbruggen.test *.nicoverbruggen.test; + return 301 https://$host$request_uri; +} + +server { + listen 127.0.0.1:443 ssl http2; + #listen 127.0.0.1:443 ssl http2; # valet loopback + server_name nicoverbruggen.test www.nicoverbruggen.test *.nicoverbruggen.test; + root /; + charset utf-8; + client_max_body_size 512M; + http2_push_preload on; + + location /41c270e4-5535-4daa-b23e-c269744c2f45/ { + internal; + alias /; + try_files $uri $uri/; + } + + ssl_certificate "/Users/nicoverbruggen/.config/valet/Certificates/nicoverbruggen.test.crt"; + ssl_certificate_key "/Users/nicoverbruggen/.config/valet/Certificates/nicoverbruggen.test.key"; + + location / { + rewrite ^ "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php" last; + } + + location = /favicon.ico { access_log off; log_not_found off; } + location = /robots.txt { access_log off; log_not_found off; } + + access_log off; + error_log "/Users/nicoverbruggen/.config/valet/Log/nginx-error.log"; + + error_page 404 "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php"; + + location ~ [^/]\.php(/|$) { + fastcgi_split_path_info ^(.+\.php)(/.+)$; + # ISOLATED_PHP_VERSION=php@8.1 + fastcgi_pass "unix:/Users/nicoverbruggen/.config/valet/valet81.sock"; + fastcgi_index "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php"; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php"; + fastcgi_param PATH_INFO $fastcgi_path_info; + } + + location ~ /\.ht { + deny all; + } +} + +server { + listen 127.0.0.1:60; + #listen 127.0.0.1:60; # valet loopback + server_name nicoverbruggen.test www.nicoverbruggen.test *.nicoverbruggen.test; + root /; + charset utf-8; + client_max_body_size 128M; + + add_header X-Robots-Tag 'noindex, nofollow, nosnippet, noarchive'; + + location /41c270e4-5535-4daa-b23e-c269744c2f45/ { + internal; + alias /; + try_files $uri $uri/; + } + + location / { + rewrite ^ "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php" last; + } + + location = /favicon.ico { access_log off; log_not_found off; } + location = /robots.txt { access_log off; log_not_found off; } + + access_log off; + error_log "/Users/nicoverbruggen/.config/valet/Log/nginx-error.log"; + + error_page 404 "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php"; + + location ~ [^/]\.php(/|$) { + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass "unix:/Users/nicoverbruggen/.config/valet/valet.sock"; + fastcgi_index "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php"; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME "/Users/nicoverbruggen/.composer/vendor/laravel/valet/server.php"; + fastcgi_param PATH_INFO $fastcgi_path_info; + } + + location ~ /\.ht { + deny all; + } +} + diff --git a/phpmon-tests/Versions/PhpVersionDetectionTest.swift b/phpmon-tests/Versions/PhpVersionDetectionTest.swift index fedd72d..88a4b24 100644 --- a/phpmon-tests/Versions/PhpVersionDetectionTest.swift +++ b/phpmon-tests/Versions/PhpVersionDetectionTest.swift @@ -24,6 +24,6 @@ class PhpVersionDetectionTest: XCTestCase { "php@5.4" // should be omitted, not supported ], checkBinaries: false) - XCTAssertEqual(outcome, ["8.0", "7.0", "5.6"]) + XCTAssertEqual(outcome, ["8.0", "7.0"]) } } diff --git a/phpmon-tests/Versions/ValetVersionExtractorTest.swift b/phpmon-tests/Versions/ValetVersionExtractorTest.swift index 8d7ceb6..f923008 100644 --- a/phpmon-tests/Versions/ValetVersionExtractorTest.swift +++ b/phpmon-tests/Versions/ValetVersionExtractorTest.swift @@ -12,7 +12,7 @@ class ValetVersionExtractorTest: XCTestCase { func testDetermineValetVersion() { let version = valet("--version", sudo: false) - XCTAssert(version.contains("Laravel Valet 2.")) + XCTAssert(version.contains("Laravel Valet")) } } diff --git a/phpmon/Domain/Integrations/Valet/ValetSite.swift b/phpmon/Domain/Integrations/Valet/ValetSite.swift index a4abf76..b751cb6 100644 --- a/phpmon/Domain/Integrations/Valet/ValetSite.swift +++ b/phpmon/Domain/Integrations/Valet/ValetSite.swift @@ -83,10 +83,15 @@ class ValetSite { determineDriver() } + /** + Determine whether a site is isolated. + */ public func determineIsolated() { - // TODO: (ISOLATION) Determine whether the domain is isolated by checking for `# Valet isolated PHP version` - // This needs to be checked in: "~/.config/valet/Nginx/\(self.name).\(self.tld)" - self.isolatedPhpVersion = nil + if let version = ValetSite.isolatedVersion("~/.config/valet/Nginx/\(self.name).\(self.tld)") { + self.isolatedPhpVersion = PhpEnv.shared.cachedPhpInstallations[version] + } else { + self.isolatedPhpVersion = nil + } } /** @@ -195,4 +200,29 @@ class ValetSite { Log.err("Something went wrong parsing the .valetphprc file") } } + + // MARK: File Parsing + + public static func isolatedVersion(_ file: String) -> String? { + if Filesystem.fileExists(file) && grepContains(file: file, query: "# ISOLATED_PHP_VERSION=") { + let regex = try! NSRegularExpression( + pattern: #"(ISOLATED_PHP_VERSION=(php@)?)((?\d)(.)?(?\d))"#, + options: [] + ) + + let fileContents = try! String(contentsOfFile: file) + + let match = regex.firstMatch(in: fileContents, range: NSMakeRange(0, fileContents.count)) + + let majorRange = Range(match!.range(withName: "major"), in: fileContents)! + let minorRange = Range(match!.range(withName: "minor"), in: fileContents)! + + let major: String = fileContents[majorRange] + let minor: String = fileContents[minorRange] + + return "\(major).\(minor)" + } + + return nil + } }