TraceableValidator.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Validator;
  11. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  12. use Symfony\Contracts\Service\ResetInterface;
  13. /**
  14. * Collects some data about validator calls.
  15. *
  16. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  17. */
  18. class TraceableValidator implements ValidatorInterface, ResetInterface
  19. {
  20. private $validator;
  21. private $collectedData = [];
  22. public function __construct(ValidatorInterface $validator)
  23. {
  24. $this->validator = $validator;
  25. }
  26. /**
  27. * @return array
  28. */
  29. public function getCollectedData()
  30. {
  31. return $this->collectedData;
  32. }
  33. public function reset()
  34. {
  35. $this->collectedData = [];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getMetadataFor($value)
  41. {
  42. return $this->validator->getMetadataFor($value);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function hasMetadataFor($value)
  48. {
  49. return $this->validator->hasMetadataFor($value);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function validate($value, $constraints = null, $groups = null)
  55. {
  56. $violations = $this->validator->validate($value, $constraints, $groups);
  57. $trace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 7);
  58. $file = $trace[0]['file'];
  59. $line = $trace[0]['line'];
  60. for ($i = 1; $i < 7; ++$i) {
  61. if (isset($trace[$i]['class'], $trace[$i]['function'])
  62. && 'validate' === $trace[$i]['function']
  63. && is_a($trace[$i]['class'], ValidatorInterface::class, true)
  64. ) {
  65. $file = $trace[$i]['file'];
  66. $line = $trace[$i]['line'];
  67. while (++$i < 7) {
  68. if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos($trace[$i]['function'], 'call_user_func')) {
  69. $file = $trace[$i]['file'];
  70. $line = $trace[$i]['line'];
  71. break;
  72. }
  73. }
  74. break;
  75. }
  76. }
  77. $name = str_replace('\\', '/', $file);
  78. $name = substr($name, strrpos($name, '/') + 1);
  79. $this->collectedData[] = [
  80. 'caller' => compact('name', 'file', 'line'),
  81. 'context' => compact('value', 'constraints', 'groups'),
  82. 'violations' => iterator_to_array($violations),
  83. ];
  84. return $violations;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function validateProperty($object, string $propertyName, $groups = null)
  90. {
  91. return $this->validator->validateProperty($object, $propertyName, $groups);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function validatePropertyValue($objectOrClass, string $propertyName, $value, $groups = null)
  97. {
  98. return $this->validator->validatePropertyValue($objectOrClass, $propertyName, $value, $groups);
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function startContext()
  104. {
  105. return $this->validator->startContext();
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function inContext(ExecutionContextInterface $context)
  111. {
  112. return $this->validator->inContext($context);
  113. }
  114. }