Class luya\web\Bootstrap

Inheritanceluya\web\Bootstrap » luya\base\BaseBootstrap
Implementsyii\base\BootstrapInterface
Available since version1.0.0

LUYA base bootstrap class which will be called during the bootstraping process.

Public Methods

Hide inherited methods

Method Description Defined By
beforeRun() Before bootstrap run process. luya\web\Bootstrap
bootstrap() Boostrap method will be invoken by Yii Application bootrapping proccess containing the Application ($app) Object to get/set data. luya\base\BaseBootstrap
extractModules() Extract and load all modules from the Application-Object. luya\base\BaseBootstrap
getModules() Return all modules prepared by extractModules() method. luya\base\BaseBootstrap
hasModule() Check if a Module exists in the module list getModules(). luya\base\BaseBootstrap
run() Invokes the bootstraping process. luya\web\Bootstrap

Protected Methods

Hide inherited methods

Method Description Defined By
generateApiRuleDefintions() Generate the rest rule defintions for {{luya\admin\Module::$apiDefintions}}. luya\web\Bootstrap

Method Details

Hide inherited methods

beforeRun() public method

Before bootstrap run process.

See also luya\base\BaseBootstrap::beforeRun().

public void beforeRun ( $app )
$app luya\web\Application

                public function beforeRun($app)
{
    foreach ($app->tags as $name => $config) {
        TagParser::inject($name, $config);
    }
    
    foreach ($this->getModules() as $id => $module) {
        foreach ($module->urlRules as $key => $rule) {
            if (is_string($key)) {
                $this->_urlRules[$key] = $rule;
            } else {
                $this->_urlRules[] = $rule;
            }
        }
        
        // get all api rules (since 1.0.10)
        foreach ($module->apiRules as $endpoint => $rule) {
            $this->_apiRules[$endpoint] = $rule;
        }
        
        /**
         * 'api-admin-user' => 'admin\apis\UserController',
         * 'api-cms-navcontainer' => 'admin\apis\NavContainerController'
         */
        foreach ($module->apis as $alias => $class) {
            $this->_apis[$alias] = ['class' => $class, 'module' => $module];
        }
        
        foreach ($module->tags as $name => $config) {
            TagParser::inject($name, $config);
        }
    }
}

            
bootstrap() public method

Defined in: luya\base\BaseBootstrap::bootstrap()

Boostrap method will be invoken by Yii Application bootrapping proccess containing the Application ($app) Object to get/set data.

public void bootstrap ( $app )
$app object

Luya Application luya\base\Application.

                public function bootstrap($app)
{
    // add trace
    Yii::beginProfile('LUYA Boostrap process profiling', __METHOD__);
    
    // register luya core translation message source
    if (!isset($app->i18n->translations['luya'])) {
        $app->i18n->translations['luya'] = [
            'class' => 'yii\i18n\PhpMessageSource',
            'basePath' => '@luya/messages',
        ];
    }
    
    $this->extractModules($app);
    $this->beforeRun($app);
    $this->startModules($app);
    $this->run($app);
    
    // end trace
    Yii::debug('End of the LUYA bootstraping process', __METHOD__);
    Yii::endProfile('LUYA Boostrap process profiling');
}

            
extractModules() public method

Defined in: luya\base\BaseBootstrap::extractModules()

Extract and load all modules from the Application-Object.

public void extractModules ( $app )
$app object

Luya Application luya\base\Application.

                public function extractModules($app)
{
    if ($this->_modules === null) {
        foreach ($app->getModules() as $id => $obj) {
            // create module object
            $moduleObject = Yii::$app->getModule($id);
            // see if the module is a luya base module, otherwise ignore
            if ($moduleObject instanceof \luya\base\Module) {
                $this->_modules[$id] = $moduleObject;
            }
        }
        // when no luya modules are registered an empty array will be returned.
        if ($this->_modules === null) {
            $this->_modules = [];
        }
    }
}

            
generateApiRuleDefintions() protected method

Generate the rest rule defintions for {{luya\admin\Module::$apiDefintions}}.

protected array generateApiRuleDefintions ( array $apis, array $rules )
$apis array

The array of apis where key is the api name ['api-admin-user' => 'admin\apis\UserController', ...].

$rules array

The new {{luya\base\Module::$apiRules}} defintion ['api-admin-user' => [...], 'api-admin-group' => []].

                protected function generateApiRuleDefintions(array $apis, array $rules)
{
    // generate the url rules which are collected as ONE with an array of controllers:
    $collection = [];
    foreach ($apis as $alias => $array) {
        if (!isset($rules[$alias])) {
            $collection[] = 'admin/'.$alias;
        }
    }
    
    $result = [];
    $result[] = ['controller' => $collection];
    
    // generate the rules from apiRules defintions as they are own entries:
    foreach ($rules as $api => $rule) {
        $rule['controller'] = 'admin/' . $api;
        $result[] = $rule;
    }
    
    return $result;
}

            
getModules() public method

Defined in: luya\base\BaseBootstrap::getModules()

Return all modules prepared by extractModules() method.

public array getModules ( )
return array

An array containg all modules where the key is the module name and the value is the Module Object luya\base\Module.

                public function getModules()
{
    return $this->_modules;
}

            
hasModule() public method

Defined in: luya\base\BaseBootstrap::hasModule()

Check if a Module exists in the module list getModules().

public boolean hasModule ( $module )
$module string

The name of the Module

                public function hasModule($module)
{
    return array_key_exists($module, $this->_modules);
}

            
run() public method

Invokes the bootstraping process.

See also luya\base\BaseBootstrap::run().

public void run ( $app )
$app luya\web\Application

                public function run($app)
{
    if (!$app->request->getIsConsoleRequest()) {
        if ($this->hasModule('admin') && $app->request->isAdmin) {
            // When admin context, change csrf token, this will not terminate the frontend csrf token:
            // @see https://github.com/luyadev/luya/issues/1778
            $app->request->csrfParam = '_csrf_admin';
            
            $app->getModule('admin')->controllerMap = $this->_apis;
            
            // calculate api defintions
            if ($app->getModule('admin')->hasProperty('apiDefintions')) { // ensure backwards compatibility
                $app->getModule('admin')->apiDefintions = $this->generateApiRuleDefintions($this->_apis, $this->_apiRules);
            }
            // as the admin module needs to listen for $apiDefintions we have to get the urlRules from the admin and merge with the existing rules:
            // in admin context, admin url rules have always precedence over frontend rules.
            $this->_urlRules = array_merge($app->getModule('admin')->urlRules, $this->_urlRules);
        } else {
            // Frontend context
            $app->themeManager->setup();
            
            if ($app->themeManager->hasActiveTheme) {
                $app->layout = $app->themeManager->activeTheme->layout;
            }
        }
    }
    
    $app->getUrlManager()->addRules($this->_urlRules);
}