annotate core/lib/Drupal/Component/Utility/Environment.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Component\Utility;
Chris@0 4
Chris@0 5 /**
Chris@0 6 * Provides PHP environment helper methods.
Chris@0 7 */
Chris@0 8 class Environment {
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Compares the memory required for an operation to the available memory.
Chris@0 12 *
Chris@0 13 * @param string $required
Chris@0 14 * The memory required for the operation, expressed as a number of bytes with
Chris@0 15 * optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8bytes,
Chris@0 16 * 9mbytes).
Chris@0 17 * @param $memory_limit
Chris@0 18 * (optional) The memory limit for the operation, expressed as a number of
Chris@0 19 * bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G,
Chris@0 20 * 6GiB, 8bytes, 9mbytes). If no value is passed, the current PHP
Chris@0 21 * memory_limit will be used. Defaults to NULL.
Chris@0 22 *
Chris@0 23 * @return bool
Chris@0 24 * TRUE if there is sufficient memory to allow the operation, or FALSE
Chris@0 25 * otherwise.
Chris@0 26 */
Chris@0 27 public static function checkMemoryLimit($required, $memory_limit = NULL) {
Chris@0 28 if (!isset($memory_limit)) {
Chris@0 29 $memory_limit = ini_get('memory_limit');
Chris@0 30 }
Chris@0 31
Chris@0 32 // There is sufficient memory if:
Chris@0 33 // - No memory limit is set.
Chris@0 34 // - The memory limit is set to unlimited (-1).
Chris@0 35 // - The memory limit is greater than or equal to the memory required for
Chris@0 36 // the operation.
Chris@0 37 return ((!$memory_limit) || ($memory_limit == -1) || (Bytes::toInt($memory_limit) >= Bytes::toInt($required)));
Chris@0 38 }
Chris@0 39
Chris@18 40 /**
Chris@18 41 * Attempts to set the PHP maximum execution time.
Chris@18 42 *
Chris@18 43 * This function is a wrapper around the PHP function set_time_limit(). When
Chris@18 44 * called, set_time_limit() restarts the timeout counter from zero. In other
Chris@18 45 * words, if the timeout is the default 30 seconds, and 25 seconds into script
Chris@18 46 * execution a call such as set_time_limit(20) is made, the script will run
Chris@18 47 * for a total of 45 seconds before timing out.
Chris@18 48 *
Chris@18 49 * If the current time limit is not unlimited it is possible to decrease the
Chris@18 50 * total time limit if the sum of the new time limit and the current time
Chris@18 51 * spent running the script is inferior to the original time limit. It is
Chris@18 52 * inherent to the way set_time_limit() works, it should rather be called with
Chris@18 53 * an appropriate value every time you need to allocate a certain amount of
Chris@18 54 * time to execute a task than only once at the beginning of the script.
Chris@18 55 *
Chris@18 56 * Before calling set_time_limit(), we check if this function is available
Chris@18 57 * because it could be disabled by the server administrator.
Chris@18 58 *
Chris@18 59 * @param int $time_limit
Chris@18 60 * An integer time limit in seconds, or 0 for unlimited execution time.
Chris@18 61 *
Chris@18 62 * @return bool
Chris@18 63 * Whether set_time_limit() was successful or not.
Chris@18 64 */
Chris@18 65 public static function setTimeLimit($time_limit) {
Chris@18 66 if (function_exists('set_time_limit')) {
Chris@18 67 $current = ini_get('max_execution_time');
Chris@18 68 // Do not set time limit if it is currently unlimited.
Chris@18 69 if ($current != 0) {
Chris@18 70 return set_time_limit($time_limit);
Chris@18 71 }
Chris@18 72 }
Chris@18 73 return FALSE;
Chris@18 74 }
Chris@18 75
Chris@18 76 /**
Chris@18 77 * Determines the maximum file upload size by querying the PHP settings.
Chris@18 78 *
Chris@18 79 * @return int
Chris@18 80 * A file size limit in bytes based on the PHP upload_max_filesize and
Chris@18 81 * post_max_size settings.
Chris@18 82 */
Chris@18 83 public static function getUploadMaxSize() {
Chris@18 84 static $max_size = -1;
Chris@18 85
Chris@18 86 if ($max_size < 0) {
Chris@18 87 // Start with post_max_size.
Chris@18 88 $max_size = Bytes::toInt(ini_get('post_max_size'));
Chris@18 89
Chris@18 90 // If upload_max_size is less, then reduce. Except if upload_max_size is
Chris@18 91 // zero, which indicates no limit.
Chris@18 92 $upload_max = Bytes::toInt(ini_get('upload_max_filesize'));
Chris@18 93 if ($upload_max > 0 && $upload_max < $max_size) {
Chris@18 94 $max_size = $upload_max;
Chris@18 95 }
Chris@18 96 }
Chris@18 97 return $max_size;
Chris@18 98 }
Chris@18 99
Chris@0 100 }