Source of file AnnotatePermissionChecker.php
Size: 3,543 Bytes - Last Modified: 2021-12-23T10:24:49+00:00
/var/www/docs.ssmods.com/process/src/src/Helpers/AnnotatePermissionChecker.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 | <?php namespace SilverLeague\IDEAnnotator\Helpers; use Psr\Container\NotFoundExceptionInterface; use ReflectionException; use SilverLeague\IDEAnnotator\DataObjectAnnotator; use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Core\Extension; use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Manifest\ModuleManifest; use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\DataObject; /** * Class AnnotatePermissionChecker * * Helperclass to check if the current environment, class or module is allowed to be annotated. * This is abstracted from @see DataObjectAnnotator to separate and clean up. * * @package IDEAnnotator/Helpers */ class AnnotatePermissionChecker { /** * In the future we will support other Classes as well. * We list the core classes, but in fact only it's subclasses are supported * @see AnnotatePermissionChecker::classNameIsSupported(); */ protected $supportedParentClasses = [ DataObject::class, DataExtension::class, Controller::class, Extension::class ]; /** * @return bool */ public function environmentIsAllowed() { if (!$this->isEnabled()) { return false; } return Director::isDev(); } /** * @return bool */ public function isEnabled() { return (bool)DataObjectAnnotator::config()->get('enabled'); } /** * @return array */ public function getSupportedParentClasses() { return $this->supportedParentClasses; } /** * Check if a DataObject or DataExtension subclass is allowed by checking if the file * is in the $allowed_modules array * The permission is checked by matching the filePath and modulePath * * @param $className * * @return bool * @throws NotFoundExceptionInterface * @throws ReflectionException */ public function classNameIsAllowed($className) { if ($this->classNameIsSupported($className)) { $classInfo = new AnnotateClassInfo($className); $filePath = $classInfo->getClassFilePath(); $module = Injector::inst()->createWithArgs( ModuleManifest::class, [Director::baseFolder()] )->getModuleByPath($filePath); $allowedModules = (array)DataObjectAnnotator::config()->get('enabled_modules'); return in_array($module->getName(), $allowedModules, true); } return false; } /** * Check if a (subclass of ) class is a supported * * @param $className * @return bool */ public function classNameIsSupported($className) { foreach ($this->supportedParentClasses as $supportedParent) { if (is_subclass_of($className, $supportedParent)) { return true; } } return false; } /** * Check if a module is in the $allowed_modules array * Required for the buildTask. * * @param string $moduleName * * @return bool */ public function moduleIsAllowed($moduleName) { return in_array($moduleName, $this->enabledModules(), null); } /** * @return array */ public function enabledModules() { $enabled = (array)DataObjectAnnotator::config()->get('enabled_modules'); // modules might be enabled more then once. return array_combine($enabled, $enabled); } } |