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;
    }