InterceptableValidator.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\ObjectManager;
  7. class InterceptableValidator
  8. {
  9. /**
  10. * @param string $className
  11. * @return bool
  12. */
  13. public function validate($className)
  14. {
  15. return !$this->isInterceptor($className) && $this->isInterceptable($className);
  16. }
  17. /**
  18. *
  19. * Check if instance type is interceptor
  20. *
  21. * @param string $instanceName
  22. * @return bool
  23. */
  24. private function isInterceptor($instanceName)
  25. {
  26. return $this->endsWith($instanceName, '\Interceptor');
  27. }
  28. /**
  29. *
  30. * Check if instance type is interceptable
  31. *
  32. * @param string $instanceName
  33. * @return bool
  34. */
  35. private function isInterceptable($instanceName)
  36. {
  37. return !is_subclass_of(
  38. $instanceName,
  39. '\\' . \Magento\Framework\ObjectManager\Code\Generator\Proxy::NON_INTERCEPTABLE_INTERFACE
  40. );
  41. }
  42. /**
  43. * Check if a string ends with a substring
  44. *
  45. * @param string $haystack
  46. * @param string $needle
  47. * @return bool
  48. */
  49. private function endsWith($haystack, $needle)
  50. {
  51. // Search forward starting from end minus needle length characters
  52. $temp = strlen($haystack) - strlen($needle);
  53. return $needle === '' || ($temp >= 0 && strpos($haystack, $needle, $temp) !== false);
  54. }
  55. }