ClassMetadata.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Metadata;
  18. /**
  19. * Base class for class metadata.
  20. *
  21. * This class is intended to be extended to add your own application specific
  22. * properties, and flags.
  23. *
  24. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  25. */
  26. class ClassMetadata implements \Serializable
  27. {
  28. public $name;
  29. public $reflection;
  30. public $methodMetadata = array();
  31. public $propertyMetadata = array();
  32. public $fileResources = array();
  33. public $createdAt;
  34. public function __construct($name)
  35. {
  36. $this->name = $name;
  37. $this->reflection = new \ReflectionClass($name);
  38. $this->createdAt = time();
  39. }
  40. public function addMethodMetadata(MethodMetadata $metadata)
  41. {
  42. $this->methodMetadata[$metadata->name] = $metadata;
  43. }
  44. public function addPropertyMetadata(PropertyMetadata $metadata)
  45. {
  46. $this->propertyMetadata[$metadata->name] = $metadata;
  47. }
  48. public function isFresh($timestamp = null)
  49. {
  50. if (null === $timestamp) {
  51. $timestamp = $this->createdAt;
  52. }
  53. foreach ($this->fileResources as $filepath) {
  54. if (!file_exists($filepath)) {
  55. return false;
  56. }
  57. if ($timestamp < filemtime($filepath)) {
  58. return false;
  59. }
  60. }
  61. return true;
  62. }
  63. public function serialize()
  64. {
  65. return serialize(array(
  66. $this->name,
  67. $this->methodMetadata,
  68. $this->propertyMetadata,
  69. $this->fileResources,
  70. $this->createdAt,
  71. ));
  72. }
  73. public function unserialize($str)
  74. {
  75. list(
  76. $this->name,
  77. $this->methodMetadata,
  78. $this->propertyMetadata,
  79. $this->fileResources,
  80. $this->createdAt
  81. ) = unserialize($str);
  82. $this->reflection = new \ReflectionClass($this->name);
  83. }
  84. }