PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/www/libs/nette-dev/Callback.php

https://github.com/bazo/Mokuji
PHP | 158 lines | 120 code | 14 blank | 24 comment | 10 complexity | 81d153eae8f65cc946aacb632229dad0 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * @copyright Copyright (c) 2004, 2010 David Grudl
  6. * @license http://nettephp.com/license Nette license
  7. * @link http://nettephp.com
  8. * @category Nette
  9. * @package Nette
  10. */
  11. /**
  12. * PHP callback encapsulation.
  13. *
  14. * @copyright Copyright (c) 2004, 2010 David Grudl
  15. * @package Nette
  16. */
  17. final class Callback extends Object
  18. {
  19. /** @var callback */
  20. private $cb;
  21. /** @var bool */
  22. public static $fix520;
  23. /**
  24. * @param mixed class, object, function, callback
  25. * @param string method
  26. */
  27. public function __construct($t, $m = NULL)
  28. {
  29. if ($m === NULL) {
  30. $this->cb = $t;
  31. } else {
  32. $this->cb = array($t, $m);
  33. }
  34. // __invoke support
  35. if (is_object($this->cb)) {
  36. $this->cb = array($this->cb, '__invoke');
  37. } elseif (self::$fix520) {
  38. // explode 'Class::method' into array
  39. if (is_string($this->cb) && strpos($this->cb, ':')) {
  40. $this->cb = explode('::', $this->cb);
  41. }
  42. // remove class namespace
  43. if (is_array($this->cb) && is_string($this->cb[0]) && $a = strrpos($this->cb[0], '\\')) {
  44. $this->cb[0] = substr($this->cb[0], $a + 1);
  45. }
  46. } else {
  47. // remove class namespace
  48. if (is_string($this->cb) && $a = strrpos($this->cb, '\\')) {
  49. $this->cb = substr($this->cb, $a + 1);
  50. } elseif (is_array($this->cb) && is_string($this->cb[0]) && $a = strrpos($this->cb[0], '\\')) {
  51. $this->cb[0] = substr($this->cb[0], $a + 1);
  52. }
  53. }
  54. if (!is_callable($this->cb, TRUE)) {
  55. throw new InvalidArgumentException("Invalid callback.");
  56. }
  57. }
  58. /**
  59. * Invokes callback. Do not call directly.
  60. * @return mixed
  61. */
  62. public function __invoke()
  63. {
  64. if (!is_callable($this->cb)) {
  65. throw new InvalidStateException("Callback '$this' is not callable.");
  66. }
  67. $args = func_get_args();
  68. return call_user_func_array($this->cb, $args);
  69. }
  70. /**
  71. * Invokes callback.
  72. * @return mixed
  73. */
  74. public function invoke()
  75. {
  76. if (!is_callable($this->cb)) {
  77. throw new InvalidStateException("Callback '$this' is not callable.");
  78. }
  79. $args = func_get_args();
  80. return call_user_func_array($this->cb, $args);
  81. }
  82. /**
  83. * Invokes callback with an array of parameters.
  84. * @param array
  85. * @return mixed
  86. */
  87. public function invokeArgs(array $args)
  88. {
  89. if (!is_callable($this->cb)) {
  90. throw new InvalidStateException("Callback '$this' is not callable.");
  91. }
  92. return call_user_func_array($this->cb, $args);
  93. }
  94. /**
  95. * Verifies that callback can be called.
  96. * @return bool
  97. */
  98. public function isCallable()
  99. {
  100. return is_callable($this->cb);
  101. }
  102. /**
  103. * Returns PHP callback pseudotype.
  104. * @return callback
  105. */
  106. public function getNative()
  107. {
  108. return $this->cb;
  109. }
  110. /**
  111. * @return string
  112. */
  113. public function __toString()
  114. {
  115. is_callable($this->cb, TRUE, $textual);
  116. return $textual;
  117. }
  118. }
  119. Callback::$fix520 = version_compare(PHP_VERSION , '5.2.2', '<');