Пример #1
0
 /**
  * @hass-todo
  */
 public function actionIndex()
 {
     $namespaces = \HassClassLoader::getHassCoreFile();
     $result = [];
     foreach ($namespaces as $namespace => $dir) {
         $controllerDir = $dir . DIRECTORY_SEPARATOR . "controllers";
         if (!is_dir($controllerDir)) {
             continue;
         }
         $files = FileHelper::findFiles($controllerDir);
         $moduleId = trim(substr($namespace, strrpos(rtrim($namespace, "\\"), "\\")), "\\");
         $result[$moduleId]["module"] = $moduleId;
         $result[$moduleId]["permissions"] = [];
         foreach ($files as $file) {
             $childDir = rtrim(pathinfo(substr($file, strpos($file, "controllers") + 12), PATHINFO_DIRNAME), ".");
             if ($childDir) {
                 $class = $namespace . "controllers\\" . str_replace("/", "\\", $childDir) . "\\" . rtrim(basename($file), ".php");
             } else {
                 $class = $namespace . "controllers\\" . rtrim(basename($file), ".php");
             }
             $controllerId = Inflector::camel2id(str_replace("Controller", "", pathinfo($file, PATHINFO_FILENAME)));
             $reflect = new \ReflectionClass($class);
             $methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
             /** @var \ReflectionMethod $method */
             foreach ($methods as $method) {
                 if (!StringHelper::startsWith($method->name, "action")) {
                     continue;
                 }
                 if ($method->name == "actions") {
                     $object = \Yii::createObject(["class" => $class], [$controllerId, $moduleId]);
                     $actions = $method->invoke($object);
                     foreach ($actions as $actionId => $config) {
                         $route = $moduleId . "/" . $controllerId . "/" . $actionId;
                         if ($childDir) {
                             $route = $moduleId . "/" . $childDir . "/" . $controllerId . "/" . $actionId;
                         }
                         $result[$moduleId]["permissions"][$route] = ["type" => Item::TYPE_PERMISSION, "description" => $controllerId . "-" . Inflector::camel2words($actionId)];
                     }
                     continue;
                 }
                 $actionId = Inflector::camel2id(substr($method->name, 6));
                 $route = $moduleId . "/" . $controllerId . "/" . $actionId;
                 if ($childDir) {
                     $route = $moduleId . "/" . $childDir . "/" . $controllerId . "/" . $actionId;
                 }
                 $result[$moduleId]["permissions"][$route] = ["type" => Item::TYPE_PERMISSION, "description" => $controllerId . "-" . Inflector::camel2words(substr($method->name, 6))];
             }
         }
     }
     $result["super"] = ['module' => 'SUPER_PERMISSION', 'permissions' => array(\hass\rbac\Module::SUPER_PERMISSION => array('type' => Item::TYPE_PERMISSION, 'description' => 'SUPER_PERMISSION'))];
     $result = "<?php \n return " . var_export($result, true) . ";";
     file_put_contents(dirname(__DIR__) . "/permissions.php", $result);
     return $this->render("index");
 }
Пример #2
0
    }
    public static function registerAlias($classMaps)
    {
        foreach ($classMaps as $namespace => $paths) {
            foreach ($paths as $path) {
                \Yii::setAlias(rtrim(str_replace('\\', '/', $namespace), "/"), $path);
            }
        }
    }
    public static function getHassCoreFile()
    {
        $filesystem = \Yii::createObject(["class" => LocalFilesystem::className(), "path" => __DIR__]);
        $classMaps = [];
        foreach ($filesystem->listContents() as $item) {
            if ($item["type"] == "dir") {
                $path = $filesystem->getAdapter()->applyPathPrefix($item["path"]);
                if (!file_exists($path . DIRECTORY_SEPARATOR . 'composer.json')) {
                    continue;
                }
                $reader = new ConfigurationReader();
                $configuration = $reader->read($path . DIRECTORY_SEPARATOR . 'composer.json');
                foreach (array_keys($configuration->autoloadPsr4()) as $namespace) {
                    $classMaps[$namespace] = $path;
                }
            }
        }
        return $classMaps;
    }
}
HassClassLoader::registerPackageAlias();