1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 20:10:08 +02:00

#41: Notify about broken PHP-FPM configuration

This commit is contained in:
2021-04-19 20:30:37 +02:00
parent 55f6c3c6cd
commit f9faa03b92
5 changed files with 53 additions and 23 deletions

View File

@ -9,9 +9,9 @@ import Foundation
class PhpInstallation {
var version: Version
var configuration: Configuration
var extensions: [PhpExtension]
var version: Version!
var configuration: Configuration!
var extensions: [PhpExtension]!
// MARK: - Computed
@ -23,7 +23,7 @@ class PhpInstallation {
init() {
// Show information about the current version
version = Self.getVersion()
self.getVersion()
// If an error occurred, exit early
if (version.error) {
@ -38,9 +38,9 @@ class PhpInstallation {
// Get configuration values
configuration = Configuration(
memory_limit: Self.getByteCount(key: "memory_limit"),
upload_max_filesize: Self.getByteCount(key: "upload_max_filesize"),
post_max_size: Self.getByteCount(key: "post_max_size")
memory_limit: self.getByteCount(key: "memory_limit"),
upload_max_filesize: self.getByteCount(key: "upload_max_filesize"),
post_max_size: self.getByteCount(key: "post_max_size")
)
// Return a list of .ini files parsed after php.ini
@ -62,27 +62,26 @@ class PhpInstallation {
When the app tries to retrieve the version, the installation is considered broken if the output is nothing,
_or_ if the output contains the word "Warning" or "Error". In normal situations this should not be the case.
*/
private static func getVersion() -> Version {
var versionStruct = Version()
private func getVersion() -> Void {
self.version = Version()
let version = Command.execute(path: Paths.phpConfig, arguments: ["--version"], trimNewlines: true)
if (version == "" || version.contains("Warning") || version.contains("Error")) {
versionStruct.short = "💩 BROKEN"
versionStruct.long = "";
versionStruct.error = true
return versionStruct;
self.version.short = "💩 BROKEN"
self.version.long = ""
self.version.error = true
return
}
// That's the long version
versionStruct.long = version
self.version.long = version
// Next up, let's strip away the minor version number
let segments = versionStruct.long.components(separatedBy: ".")
let segments = self.version.long.components(separatedBy: ".")
// Get the first two elements
versionStruct.short = segments[0...1].joined(separator: ".")
return versionStruct
self.version.short = segments[0...1].joined(separator: ".")
}
/**
@ -98,7 +97,7 @@ class PhpInstallation {
- Parameter key: The key of the `ini` value that needs to be retrieved. For example, you can use `memory_limit`.
*/
private static func getByteCount(key: String) -> String {
private func getByteCount(key: String) -> String {
let value = Command.execute(path: Paths.php, arguments: ["-r", "echo ini_get('\(key)');"])
// Check if the value is unlimited
@ -112,6 +111,28 @@ class PhpInstallation {
return (match == nil) ? "⚠️" : "\(value)B"
}
public func notifyAboutBrokenPhpFpm() {
if !self.checkPhpFpmStatus() {
DispatchQueue.main.async {
Alert.notify(
message: "alert.php_fpm_broken.title".localized,
info: "alert.php_fpm_broken.info".localized
)
}
}
}
private func checkPhpFpmStatus() -> Bool {
if self.version.short == "5.6" {
// The main PHP config file should contain `valet.sock` and then we're probably fine?
let fileName = "\(Paths.etcPath)/php/5.6/php-fpm.conf"
return Shell.pipe("cat \(fileName)").contains("valet.sock")
}
// Make sure to check if valet-fpm.conf exists. If it does, we should be fine :)
return Shell.fileExists("\(Paths.etcPath)/php/\(self.version.short)/php-fpm.d/valet-fpm.conf")
}
// MARK: - Structs
struct Version {

View File

@ -44,6 +44,9 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate {
App.shared.availablePhpVersions = Actions.detectPhpVersions()
updatePhpVersionInStatusBar()
let installation = App.phpInstall!
installation.notifyAboutBrokenPhpFpm()
// Schedule a request to fetch the PHP version every 60 seconds
DispatchQueue.main.async { [self] in
App.shared.timer = Timer.scheduledTimer(
@ -325,6 +328,8 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate {
title: String(format: "notification.version_changed_title".localized, sender.version),
subtitle: String(format: "notification.version_changed_desc".localized, sender.version)
)
App.phpInstall?.notifyAboutBrokenPhpFpm()
}
}

View File

@ -98,9 +98,9 @@ class StatusMenu : NSMenu {
// Stats
self.addItem(NSMenuItem.separator())
self.addItem(StatsView.asMenuItem(
memory: stats.memory_limit,
post: stats.post_max_size,
upload: stats.upload_max_filesize)
memory: stats!.memory_limit,
post: stats!.post_max_size,
upload: stats!.upload_max_filesize)
)
// Extensions

View File

@ -16,7 +16,7 @@ class Shell {
}
public static func pipe(_ command: String) -> String {
Shell.user.pipe(command)
return Shell.user.pipe(command)
}
// MARK: - Singleton

View File

@ -82,6 +82,10 @@
"alert.force_reload_done.title" = "PHP has been force reloaded";
"alert.force_reload_done.info" = "All appropriate services have been restarted, and the latest version of PHP is now active. You can now try switching to another version of PHP. If visiting sites still does not work, you may try running `valet install` again, this can fix a 502 issue (Bad Gateway).";
// PHP FPM Broken
"alert.php_fpm_broken.title" = "PHP-FPM configuration is incorrect";
"alert.php_fpm_broken.info" = "PHP Monitor has determined that there are issues with your PHP-FPM config: it's not pointing to the Valet socket. This will result in 502 Bad Gateway if you visit websites linked via Valet.\n\nYou can usually fix this by running\n`valet install`, which updates your\n PHP-FPM configuration.";
// PHP Monitor Cannot Start
"alert.cannot_start.title" = "PHP Monitor cannot start";
"alert.cannot_start.info" = "The issue you were just notified about is keeping PHP Monitor from functioning correctly. Please fix the issue and restart PHP Monitor. After clicking on OK, PHP Monitor will close.\n\nIf you have fixed the issue (or don't remember what the exact issue is) you can click on Retry, which will have PHP Monitor retry the startup checks.";