Source of file ConfigList.php
Size: 8,908 Bytes - Last Modified: 2021-12-23T10:39:12+00:00
/var/www/docs.ssmods.com/process/src/src/Api/ConfigList.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 | <?php namespace Sunnysideup\ConfigManager\Api; use ReflectionClass; use SilverStripe\Config\Collections\DeltaConfigCollection; use SilverStripe\Control\Director; use SilverStripe\Core\ClassInfo; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Extensible; use SilverStripe\Core\Extension; use SilverStripe\Core\Injector\Injectable; class ConfigList { use Extensible; use Injectable; use Configurable; private static $exceptional_classes = [ Extension::class, ]; private static $do_not_show = [ 'extra_methods', 'built_in_methods', ]; // protected $locationIncludes = []; // // protected $classNameIncludes = []; // // public function setLocationIncludes($a) // { // $this->locationIncludes = $a; // // return $this; // } // // public function setClassNameIncludes($a) // { // $this->classNameIncludes = $a; // // return $this; // } public function getListOfConfigs(): array { $resultArray = []; $doNotShow = $this->Config()->get('do_not_show'); $config = Config::inst(); $alsoSet = $config->getAll(); $base = Director::baseFolder(); $classes = $this->configurableClasses(); foreach ($classes as $class) { $reflector = new ReflectionClass($class); $fileName = $reflector->getFileName(); $fileName = str_replace($base, '', $fileName); //lists $staticListDelta = $this->getDeltas($config, $class); $staticListDynamic = array_keys($alsoSet[strtolower($class)]); $defaultLists = $this->getDefaultLists($reflector, $doNotShow); $originalValues = $defaultLists['OriginalValues']; $lists = [ //do first so that they will always show up 'runtime' => $staticListDelta, 'system' => $defaultLists['Caching'], 'caching' => $defaultLists['System'], 'property' => $defaultLists['Property'], //needs to be last so that only dynamic ones that are //not set as property are included 'dynamic' => $staticListDynamic, ]; foreach ($lists as $type => $list) { foreach ($list as $property) { $key = str_replace('/', '-', $fileName . '-' . $property); if (! isset($resultArray[$key])) { $value = $config->get($class, $property, Config::UNINHERITED); $hasValue = (bool) $value; $originalValue = isset($originalValues[$property]) ? $originalValues[$property] : ''; if (is_object($value)) { $value = 'object'; } $isDefault = true; $default = ''; if ($originalValue !== $value) { $isDefault = false; if ($value && $originalValue) { $default = $originalValue; } } $hasDefault = (bool) $originalValue; $resultArray[$key] = array_merge( $this->getClassIntel($class), [ 'FileLocation' => $fileName, 'Property' => $property, 'Type' => $type, 'IsDefault' => $isDefault, 'HasDefault' => $hasDefault, 'HasValue' => $hasValue, 'Default' => $default, 'Value' => $value, ] ); } } } } return $resultArray; } /** * info about static. * * @param ReflectionClass $reflector * @param bool $doNotShow */ protected function getDefaultLists($reflector, $doNotShow): array { //vars $staticListSystem = []; $staticListCaching = []; $staticListProperty = []; $originalValues = []; //start loop $statics = $reflector->getStaticProperties(); foreach (array_keys($statics) as $property) { $propertyObject = $reflector->getProperty($property); if ($propertyObject->isPrivate()) { $propertyObject->setAccessible(true); $originalValues[$property] = $propertyObject->getValue($reflector); if (in_array($property, $doNotShow, true)) { $staticListSystem[$property] = $property; } elseif ('_' === substr($property, 0, 1) || false !== strpos($property, 'cache') ) { $staticListCaching[$property] = $property; } else { $staticListProperty[$property] = $property; } } } return [ 'System' => $staticListSystem, 'Caching' => $staticListCaching, 'Property' => $staticListProperty, 'OriginalValues' => $originalValues, ]; } /** * info about class. */ protected function getClassIntel(string $class): array { $vendor = 'n/a'; $package = 'n/a'; $shorterClassname = $class; $classNameArray = explode('\\', $class); $shortClassName = ClassInfo::shortName($class); $ancestry = ClassInfo::ancestry($class); $childClasses = ClassInfo::subclassesFor($class, false); if (count($classNameArray) > 1) { $vendor = $classNameArray[0]; $package = $classNameArray[1]; array_shift($classNameArray); array_shift($classNameArray); $shorterClassname = implode(' / ', $classNameArray); } return [ 'Vendor' => $vendor, 'Package' => $package, 'ClassName' => $class, 'ShorterClassName' => $shorterClassname, 'ShortClassName' => $shortClassName, 'ParentClasses' => $ancestry, 'ChildClasses' => $childClasses, ]; } /** * get values set at run time (deltas / changed ones). * * @param Config $config * @param string $className */ protected function getDeltas($config, $className): array { $deltaList = []; if ($config instanceof DeltaConfigCollection) { $deltas = $config->getDeltas($className); if (count($deltas)) { foreach ($deltas as $deltaInners) { if (isset($deltaInners['config'])) { $deltaList = array_merge( $deltaList, array_keys($deltaInners['config']) ); } } } } return $deltaList; } /** * get a list of class with the Configurable Trait. */ protected function configurableClasses(): array { $definedClasses = ClassInfo::allClasses(); return array_filter( $definedClasses, function ($className) { if (class_exists($className)) { $autoload = true; $traits = []; $class = $className; // Get traits of all parent classes do { $traits = array_merge(class_uses($class, $autoload), $traits); $class = get_parent_class($class); if (in_array($class, $this->Config()->get('exceptional_classes'), true)) { $traits[Configurable::class] = Configurable::class; } } while ($class); // Get traits of all parent traits $traitsToSearch = $traits; while (! empty($traitsToSearch)) { $newTraits = class_uses(array_pop($traitsToSearch), $autoload); $traits = array_merge($newTraits, $traits); $traitsToSearch = array_merge($newTraits, $traitsToSearch); } foreach (array_keys($traits) as $trait) { $traits = array_merge(class_uses($trait, $autoload), $traits); } return isset($traits[Configurable::class]); } } ); } } |