taxonomylist.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\GravTrait;
  4. class Taxonomylist
  5. {
  6. use GravTrait;
  7. /**
  8. * @var array
  9. */
  10. protected $taxonomylist;
  11. /**
  12. * Get taxonomy list.
  13. *
  14. * @return array
  15. */
  16. public function get()
  17. {
  18. if (!$this->taxonomylist) {
  19. $this->build();
  20. }
  21. return $this->taxonomylist;
  22. }
  23. /**
  24. * @internal
  25. */
  26. protected function build()
  27. {
  28. $taxonomylist = self::getGrav()['taxonomy']->taxonomy();
  29. $cache = self::getGrav()['cache'];
  30. $hash = hash('md5', serialize($taxonomylist));
  31. if ($taxonomy = $cache->fetch($hash)) {
  32. $this->taxonomylist = $taxonomy;
  33. } else {
  34. $newlist = [];
  35. foreach ($taxonomylist as $x => $y) {
  36. $partial = [];
  37. foreach ($taxonomylist[$x] as $key => $value) {
  38. $taxonomylist[$x][strval($key)] = count($value);
  39. $partial[strval($key)] = count($value);
  40. }
  41. arsort($partial);
  42. $newlist[$x] = $partial;
  43. }
  44. $cache->save($hash, $newlist);
  45. $this->taxonomylist = $newlist;
  46. }
  47. }
  48. }