summaryrefslogtreecommitdiffstats
path: root/library/Director/Application/MemoryLimit.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
commitf66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch)
treefbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/Application/MemoryLimit.php
parentInitial commit. (diff)
downloadicingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz
icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/Application/MemoryLimit.php')
-rw-r--r--library/Director/Application/MemoryLimit.php53
1 files changed, 53 insertions, 0 deletions
diff --git a/library/Director/Application/MemoryLimit.php b/library/Director/Application/MemoryLimit.php
new file mode 100644
index 0000000..beb0460
--- /dev/null
+++ b/library/Director/Application/MemoryLimit.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace Icinga\Module\Director\Application;
+
+class MemoryLimit
+{
+ public static function raiseTo($string)
+ {
+ $current = static::getBytes();
+ $desired = static::parsePhpIniByteString($string);
+ if ($current !== -1 && $current < $desired) {
+ ini_set('memory_limit', $string);
+ }
+ }
+
+ public static function getBytes()
+ {
+ return static::parsePhpIniByteString((string) ini_get('memory_limit'));
+ }
+
+ /**
+ * Return Bytes from PHP shorthand bytes notation
+ *
+ * http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
+ *
+ * > The available options are K (for Kilobytes), M (for Megabytes) and G
+ * > (for Gigabytes), and are all case-insensitive. Anything else assumes
+ * > bytes.
+ *
+ * @param $string
+ * @return int
+ */
+ public static function parsePhpIniByteString($string)
+ {
+ $val = trim($string);
+
+ if (preg_match('/^(\d+)([KMG])$/', $val, $m)) {
+ $val = $m[1];
+ switch ($m[2]) {
+ case 'G':
+ $val *= 1024;
+ // Intentional fall-through
+ case 'M':
+ $val *= 1024;
+ // Intentional fall-through
+ case 'K':
+ $val *= 1024;
+ }
+ }
+
+ return intval($val);
+ }
+}