PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/sonata-project/cache-bundle/Sonata/CacheBundle/Invalidation/ModelCollectionIdentifiers.php

https://bitbucket.org/ingsol/music_sonata
PHP | 64 lines | 44 code | 12 blank | 8 comment | 7 complexity | afadba97c54e98367fdddf85c2cb4735 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, Apache-2.0, JSON, LGPL-3.0, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\CacheBundle\Invalidation;
  11. class ModelCollectionIdentifiers
  12. {
  13. protected $classes = array();
  14. public function __construct(array $classes = array())
  15. {
  16. foreach ($classes as $class => $identifier) {
  17. $this->addClass($class, $identifier);
  18. }
  19. }
  20. public function addClass($class, $identifier)
  21. {
  22. $this->classes[$class] = $identifier;
  23. }
  24. public function getIdentifier($object)
  25. {
  26. $identifier = $this->getMethod($object);
  27. if (!$identifier) {
  28. return false;
  29. }
  30. return call_user_func(array($object, $identifier));
  31. }
  32. public function getMethod($object)
  33. {
  34. if ($object === null) {
  35. return false;
  36. }
  37. foreach ($this->classes as $class => $identifier) {
  38. if ($object instanceof $class) {
  39. return $identifier;
  40. }
  41. }
  42. $class = get_class($object);
  43. if (method_exists($object, 'getCacheIdentifier')) {
  44. $this->addClass($class, 'getCacheIdentifier');
  45. } else if (method_exists($object, 'getId')) {
  46. $this->addClass($class, 'getId');
  47. } else {
  48. return false;
  49. }
  50. return $this->classes[$class];
  51. }
  52. }