notadd 初始化所有modules

notadd\vendor\notadd\framework\src\Module\ModuleManager.php

function getModules

/**
 * Modules of list.
 *
 * @return \Illuminate\Support\Collection
 */
public function getModules()
{
    if ($this->modules->isEmpty()) {
        if ($this->files->isDirectory($this->getModulePath())) {
            collect($this->files->directories($this->getModulePath()))->each(function ($directory) {
                if ($this->files->exists($file = $directory . DIRECTORY_SEPARATOR . 'composer.json')) {
                    $package = new Collection(json_decode($this->files->get($file), true));
                    $identification = Arr::get($package, 'name');
                    $type = Arr::get($package, 'type');
                    if ($type == 'notadd-module' && $identification) {
                        $provider = '';
                        if ($entries = data_get($package, 'autoload.psr-4')) {
                            foreach ($entries as $namespace => $entry) {
                                $provider = $namespace . 'ModuleServiceProvider';
                            }
                        }
                        if ($this->files->exists($autoload = $directory . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php')) {
                            $this->files->requireOnce($autoload);
                        }
                        $authors = Arr::get($package, 'authors');
                        $description = Arr::get($package, 'description');
                        if (class_exists($provider)) {
                            $module = new Module($identification);
                            $module->setAuthor($authors);
                            $module->setDescription($description);
                            $module->setDirectory($directory);
                            $module->setEnabled($this->container->isInstalled() ? $this->container->make('setting')->get('module.' . $identification . '.enabled', false) : false);
                            $module->setInstalled($this->container->isInstalled() ? $this->container->make('setting')->get('module.' . $identification . '.installed', false) : false);
                            $module->setEntry($provider);
                            if (method_exists($provider, 'alias')) {
                                $module->setAlias(call_user_func([$provider, 'alias']));
                            } else {
                                $module->setAlias([$identification]);
                            }
                            method_exists($provider, 'description') && $module->setDescription(call_user_func([$provider, 'description']));
                            method_exists($provider, 'name') && $module->setName(call_user_func([$provider, 'name']));
                            method_exists($provider, 'script') && $module->setScript(call_user_func([$provider, 'script']));
                            method_exists($provider, 'stylesheet') && $module->setStylesheet(call_user_func([$provider, 'stylesheet']));
                            method_exists($provider, 'version') && $module->setVersion(call_user_func([$provider, 'version']));
                            $this->modules->put($identification, $module);
                        } else {
                            $this->unloaded->put($identification, [
                                'authors'        => $authors,
                                'description'    => $description,
                                'directory'      => $directory,
                                'identification' => $identification,
                                'provider'       => $provider,
                            ]);
                        }
                    }
                }
            });
        }
    }

    return $this->modules;
}

这个function 获取到所有的module

我想一定是系统初始化的时候初始化了每一个module 所以才能注册到module的route

notadd 查找/admin路由绑定的地方

一直想找notadd的后台时如何创建路由的,结果一直没有找到

今天总算是让我找到了

一下记录一下步骤

获取所有路由列表

php notadd route:list > D:route.txt

image.png

发现是在Notadd\Administration\Controllers\AdminController这个文件里定义的 

我打开notadd\modules\administration\src\ModuleServiceProvider.php

看到ModuleServiceProvider的boot function是这样的

/**
 * Boot service provider.
 *
 * @throws \Illuminate\Contracts\Container\BindingResolutionException
 */
public function boot()
{
    $administrator = new Administrator($this->app['events'], $this->app['router']);
    $administrator->registerPath('admin');
    $administrator->registerHandler(AdminController::class . '@handle');
    $this->administration->setAdministrator($administrator);
    $this->app->make(Dispatcher::class)->subscribe(CsrfTokenRegister::class);
    $this->app->make(Dispatcher::class)->subscribe(PermissionGroupRegister::class);
    $this->app->make(Dispatcher::class)->subscribe(PermissionModuleRegister::class);
    $this->app->make(Dispatcher::class)->subscribe(PermissionRegister::class);
    $this->app->make(Dispatcher::class)->subscribe(PermissionTypeRegister::class);
    $this->app->make(Dispatcher::class)->subscribe(RouteRegister::class);
    $this->loadTranslationsFrom(realpath(__DIR__ . '/../resources/translations'), 'administration');
    $this->loadViewsFrom(realpath(__DIR__ . '/../resources/views'), 'admin');
    $this->publishes([
        realpath(__DIR__ . '/../resources/mixes/administration/dist/assets/admin') => public_path('assets/admin'),
        realpath(__DIR__ . '/../resources/mixes/neditor')                          => public_path('assets/neditor'),
    ], 'public');
}

重点是这几行

image.png

我不知道registerPath和registerHandler是做什么用的,那么我向上追溯到了notadd\vendor\notadd\framework\src\Administration\Abstracts\Administrator.php

/**
 * Init administrator.
 *
 * @throws \InvalidArgumentException
 */
final public function init()
{
    if (is_null($this->path) || is_null($this->handler)) {
        throw new InvalidArgumentException('Handler or Path must be Setted!');
    }
    $this->router->group(['middleware' => 'web'], function () {
        $this->router->get($this->path, $this->handler);
    });
}

/**
 * Register administration handler.
 *
 * @param $handler
 */
public function registerHandler($handler)
{
    $this->handler = $handler;
}

/**
 * Register administration route path.
 *
 * @param string $path
 */
public function registerPath($path)
{
    $this->path = $path;
}

发现了这些代码,好吧 原来这个路由是在这里去绑定的······ 怪不得我开始怎么找也找不到呢

所以是吧/admin  绑定到了notadd\modules\administration\src\Controllers\AdminController.php 的 handle function

/**
 * Return index content.
 *
 * @param \Notadd\Foundation\Extension\ExtensionManager $extension
 * @param \Notadd\Foundation\Module\ModuleManager       $module
 *
 * @return \Illuminate\Contracts\View\View
 */
public function handle(ExtensionManager $extension, ModuleManager $module)
{
    $this->share('extensions', $extension->getEnabledExtensions());
    $this->share('modules', $module->getEnabledModules());
    $this->share('translations', json_encode($this->translator->fetch('zh-cn')));
    return $this->view('admin::layout');
}

所以在这些操作之前,一定有一个地方初始化了所有的module

我想找到这个地方