/Core/legacy.php

https://github.com/catx23/xapp-php · PHP · 123 lines · 85 code · 5 blank · 33 comment · 16 complexity · 32fb73e8ec5d77616f32869c7923eb35 MD5 · raw file

  1. <?php
  2. /**
  3. * @version 0.1.0
  4. * @link http://www.xapp-studio.com
  5. * @author XApp-Studio.com support@xapp-studio.com
  6. * @license : GPL v2. http://www.gnu.org/licenses/gpl-2.0.html
  7. */
  8. // *****************************************************************
  9. // legacy functions for php version < 5.3.0. this file must be
  10. // included when using php version lesser then 5.3.0. no need to
  11. // include this file manually since it will be included with
  12. // core.php file
  13. // *****************************************************************
  14. if(!function_exists('parse_ini_string'))
  15. {
  16. /**
  17. * emulates php parse_ini_string function
  18. *
  19. * @see http://www.php.net/manual/en/function.parse-ini-string.php
  20. * @param string $string expects the ini settings as string
  21. * @return array
  22. */
  23. function parse_ini_string($string)
  24. {
  25. $array = array();
  26. $lines = explode("\n", trim($string));
  27. foreach($lines as $line)
  28. {
  29. $regex = preg_match("/^(?!;)(?P<key>[\w+\.\-]+?)\s*=\s*(?P<value>.+?)\s*$/", $line, $match);
  30. if($regex)
  31. {
  32. $key = $match['key'];
  33. $value = $match['value'];
  34. if(preg_match( "/^\".*\"$/", $value) || preg_match( "/^'.*'$/", $value))
  35. {
  36. $value = mb_substr($value, 1, mb_strlen($value) - 2);
  37. }
  38. $array[$key] = $value;
  39. }
  40. }
  41. return $array;
  42. }
  43. }
  44. if(!function_exists('get_called_class'))
  45. {
  46. /**
  47. * emulates php get_called_class. will fail on very deep nested callees
  48. * since implementation tries to get always the first callee which does
  49. * not mean its the first callee in chain. use php versions > 5.3.0 since
  50. * everything below that version is considered to be outdated. will throw
  51. * exception if callee can not be determined
  52. *
  53. * @see http://www.php.net/manual/en/function.get-called-class.php
  54. * @param bool|array $bt expects either debug backtrace array or false to force getting backtrace in function
  55. * @param int $l expects the level or steps of backtrace will look by default
  56. * @return string
  57. * @throws Exception
  58. */
  59. function get_called_class($bt = false, $l = 1)
  60. {
  61. if(!$bt) $bt = debug_backtrace();
  62. if(!isset($bt[$l])) throw new Exception("Cannot find called class -> stack level too deep.");
  63. if(!isset($bt[$l]['type']))
  64. {
  65. throw new Exception('type not set');
  66. }else{
  67. switch ($bt[$l]['type'])
  68. {
  69. case '::':
  70. if(($lines = file($bt[$l]['file'])) !== false)
  71. {
  72. $matches = array();
  73. $i = 0;
  74. $line = '';
  75. do
  76. {
  77. $i++;
  78. $line = $lines[$bt[$l]['line']-$i] . $line;
  79. }
  80. while(stripos($line, $bt[$l]['function']) === false);
  81. preg_match('/([a-zA-Z0-9\_]+)\:\:'.$bt[$l]['function'].'/su', $line, $matches);
  82. //fall back for static singleton instantiation with method call in same line
  83. if(sizeof($matches) === 0)
  84. {
  85. preg_match('/([a-zA-Z0-9\_]+)\:\:(?:[^\(]{1,})\((?:[^\)]*)(?:[\)]{1,})\-\>'.$bt[$l]['function'].'/su', $line, $matches);
  86. }
  87. if(!isset($matches[1]))
  88. {
  89. throw new Exception("could not find caller class: originating method call is obscured.");
  90. }
  91. switch ($matches[1])
  92. {
  93. case 'self':
  94. case 'parent':
  95. return get_called_class($bt,$l+1);
  96. default:
  97. return $matches[1];
  98. }
  99. }else{
  100. throw new Exception("unable to open file for introspection");
  101. }
  102. case '->':
  103. switch ($bt[$l]['function'])
  104. {
  105. case '__get':
  106. if(!is_object($bt[$l]['object']))
  107. {
  108. throw new Exception("edge case fail. __get called on non object.");
  109. }
  110. return get_class($bt[$l]['object']);
  111. default: return $bt[$l]['class'];
  112. }
  113. default:
  114. throw new Exception("unknown backtrace method type");
  115. }
  116. }
  117. }
  118. }