Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Lib.Phpini.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Dcp\Core;
4 
5 class LibPhpini
6 {
7  /**
8  * @var \Application
9  */
10  static $coreApplication = null;
11 
12  public static function applyLimits()
13  {
14  $limits = [];
15  $limits["memory_limit"] = self::applyLimit("memory_limit", "MEMORY_LIMIT", 64, -1);
16  return $limits;
17  }
18 
19  /**
20  * @param \Application $coreApplication
21  */
23  {
24  self::$coreApplication = $coreApplication;
25  }
26 
27  /**
28  * Apply a limit to $phpIniValueName value by choosing "best" value between
29  * php.ini and CORE parameter $dcpParameterName
30  *
31  * @param string $phpIniValueName name of the php.ini parameter
32  * @param string $dcpParameterName name of the core parameter containing dcp value
33  * @param int $defaultValue default value for the core parameter if empty
34  * @param int $infinityValue infinity value (often -1 or 0)
35  * @return array
36  */
37  protected static function applyLimit($phpIniValueName, $dcpParameterName, $defaultValue, $infinityValue = 0)
38  {
39  $changes = [];
40 
41  $phpIniLimit = ini_get($phpIniValueName);
42  $changes["php.ini"] = $phpIniLimit;
43 
44  if ($infinityValue !== $phpIniLimit) {
45  $changes["php.ini_is_infinity"] = false;
46 
47  $coreLimit = intval(self::getParam($dcpParameterName, $defaultValue));
48  $changes["core"] = $coreLimit;
49 
50  if (self::return_bytes($phpIniLimit) < $coreLimit * 1024 * 1024) {
51  $changes["best"] = $coreLimit . "M";
52  $changes["success"] = false !==ini_set($phpIniValueName, $coreLimit . "M");
53  } else {
54  $changes["best"] = $phpIniLimit;
55  }
56  } else {
57  $changes["php.ini_is_infinity"] = true;
58  $changes["best"] = $phpIniLimit;
59  }
60 
61  return $changes;
62  }
63 
64  protected static function getParam($name, $defaultValue)
65  {
66  if(is_null(self::$coreApplication)) {
67  global $action;
68  if ($action instanceof \Action &&
69  "CORE" === $action->parent->name) {
70  self::$coreApplication = $action->parent;
71  return self::$coreApplication->getParam($name, $defaultValue);
72  } else {
73  require_once 'Lib.Common.php';
74  require_once 'Class.ApplicationParameterManager.php';
75  $parameterValue = \ApplicationParameterManager::getParameterValue("CORE", $name);
76  return (null === $parameterValue ? $defaultValue : $parameterValue);
77  }
78  } else {
79  return self::$coreApplication->getParam($name, $defaultValue);
80  }
81  }
82 
83  /**
84  * convert a php.ini value into bytes
85  *
86  * @param string|int $val the value from php.ini with optional unit)
87  * @return int
88  */
89  protected static function return_bytes($val)
90  {
91  $val = trim($val);
92  $last = strtolower(substr($val, -1));
93  $val=intval($val);
94  switch($last) {
95  /** @noinspection PhpMissingBreakStatementInspection */
96  case 'g':
97  $val *= 1024;
98  /** @noinspection PhpMissingBreakStatementInspection */
99  case 'm':
100  $val *= 1024;
101  case 'k':
102  $val *= 1024;
103  }
104 
105  return $val;
106  }
107 }
static return_bytes($val)
Definition: Lib.Phpini.php:89
global $action
static $coreApplication
Definition: Lib.Phpini.php:10
static getParameterValue($application, $parameterName)
static setCoreApplication(\Application $coreApplication)
Definition: Lib.Phpini.php:22
getParam($name, $def="")
must be in core or global type
Definition: Lib.Common.php:193
static getParam($name, $defaultValue)
Definition: Lib.Phpini.php:64
static applyLimits()
Definition: Lib.Phpini.php:12
static applyLimit($phpIniValueName, $dcpParameterName, $defaultValue, $infinityValue=0)
Definition: Lib.Phpini.php:37
← centre documentaire © anakeen