/**
  * This method should never be used unless you know what are you doing.
  *
  * Populates the "acos" DB with information of every installed plugin, or
  * for the given plugin. It will automatically extracts plugin's controllers
  * and actions for creating a tree structure as follow:
  *
  * - PluginName
  *   - Admin
  *     - PrivateController
  *       - index
  *       - some_action
  *   - ControllerName
  *     - index
  *     - another_action
  *
  * After tree is created you should be able to change permissions using
  * User's permissions section in backend.
  *
  * @param string $for Optional, build ACOs for the given plugin, or all plugins
  *  if not given
  * @param bool $sync Whether to sync the tree or not. When syncing all invalid
  *  ACO entries will be removed from the tree, also new ones will be added. When
  *  syn is set to false only new ACO entries will be added, any invalid entry
  *  will remain in the tree. Defaults to false
  * @return bool True on success, false otherwise
  */
 public static function buildAcos($for = null, $sync = false)
 {
     if (function_exists('ini_set')) {
         ini_set('max_execution_time', 300);
     } elseif (function_exists('set_time_limit')) {
         set_time_limit(300);
     }
     if ($for === null) {
         $plugins = plugin()->toArray();
     } else {
         try {
             $plugins = [plugin($for)];
         } catch (\Exception $e) {
             return false;
         }
     }
     $added = [];
     foreach ($plugins as $plugin) {
         if (!Plugin::exists($plugin->name)) {
             continue;
         }
         $aco = new AcoManager($plugin->name);
         $controllerDir = normalizePath("{$plugin->path}/src/Controller/");
         $folder = new Folder($controllerDir);
         $controllers = $folder->findRecursive('.*Controller\\.php');
         foreach ($controllers as $controller) {
             $controller = str_replace([$controllerDir, '.php'], '', $controller);
             $className = $plugin->name . '\\' . 'Controller\\' . str_replace(DS, '\\', $controller);
             $methods = static::_controllerMethods($className);
             if (!empty($methods)) {
                 $path = explode('Controller\\', $className)[1];
                 $path = str_replace_last('Controller', '', $path);
                 $path = str_replace('\\', '/', $path);
                 foreach ($methods as $method) {
                     if ($aco->add("{$path}/{$method}")) {
                         $added[] = "{$plugin->name}/{$path}/{$method}";
                     }
                 }
             }
         }
     }
     if ($sync && isset($aco)) {
         $aco->Acos->recover();
         $existingPaths = static::paths($for);
         foreach ($existingPaths as $exists) {
             if (!in_array($exists, $added)) {
                 $aco->remove($exists);
             }
         }
         $validLeafs = $aco->Acos->find()->select(['id'])->where(['id NOT IN' => $aco->Acos->find()->select(['parent_id'])->where(['parent_id IS NOT' => null])]);
         $aco->Acos->Permissions->deleteAll(['aco_id NOT IN' => $validLeafs]);
     }
     return true;
 }