introspection.class.inc.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. // Copyright (C) 2017 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Usage:
  20. * require_once(...'introspection.class.inc.php');
  21. */
  22. require_once('attributedef.class.inc.php');
  23. class Introspection
  24. {
  25. protected $aAttributeHierarchy = array(); // class => child classes
  26. protected $aAttributes = array();
  27. public function __construct()
  28. {
  29. $this->InitAttributes();
  30. }
  31. protected function InitAttributes()
  32. {
  33. foreach(get_declared_classes() as $sPHPClass)
  34. {
  35. $oRefClass = new ReflectionClass($sPHPClass);
  36. if ($sPHPClass == 'AttributeDefinition' || $oRefClass->isSubclassOf('AttributeDefinition'))
  37. {
  38. if ($oParentClass = $oRefClass->getParentClass())
  39. {
  40. $sParentClass = $oParentClass->getName();
  41. if (!array_key_exists($sParentClass, $this->aAttributeHierarchy))
  42. {
  43. $this->aAttributeHierarchy[$sParentClass] = array();
  44. }
  45. $this->aAttributeHierarchy[$sParentClass][] = $sPHPClass;
  46. }
  47. else
  48. {
  49. $sParentClass = null;
  50. }
  51. $this->aAttributes[$sPHPClass] = array(
  52. 'parent' => $sParentClass,
  53. 'LoadInObject' => $sPHPClass::LoadInObject(),
  54. 'LoadFromDB' => $sPHPClass::LoadFromDB(),
  55. 'IsBasedOnDBColumns' => $sPHPClass::IsBasedOnDBColumns(),
  56. 'IsBasedOnOQLExpression' => $sPHPClass::IsBasedOnOQLExpression(),
  57. 'IsExternalField' => $sPHPClass::IsExternalField(),
  58. 'IsScalar' => $sPHPClass::IsScalar(),
  59. 'IsLinkset' => $sPHPClass::IsLinkset(),
  60. 'IsHierarchicalKey' => $sPHPClass::IsHierarchicalKey(),
  61. );
  62. }
  63. }
  64. }
  65. public function GetAttributes()
  66. {
  67. return $this->aAttributes;
  68. }
  69. public function GetAttributeHierarchy()
  70. {
  71. return $this->aAttributeHierarchy;
  72. }
  73. public function EnumAttributeCharacteristics()
  74. {
  75. return array(
  76. 'LoadInObject' => 'Is the value stored in the object itself?',
  77. 'LoadFromDB' => 'Is the value read from the DB?',
  78. 'IsBasedOnDBColumns' => 'Is this a value stored within one or several columns?',
  79. 'IsBasedOnOQLExpression' => 'Is this a value computed after other attributes, by the mean of an OQL expression?',
  80. 'IsExternalField' => 'Is this a value stored on a related object (external key)?',
  81. 'IsScalar' => 'Is this a value that makes sense in a SQL/OQL expression?',
  82. 'IsLinkset' => 'Is this a collection (1-N or N-N)?',
  83. 'IsHierarchicalKey' => 'Is this attribute an external key pointing to the host class?',
  84. );
  85. }
  86. }