how does the Yii2 replace the @app/views/layouts/main.php with @app/themes/basic/layouts/main.php when I use view component

If I use view component like the document says

return [
    'components' => [
        'view' => [
            'theme' => [
                'basePath' => '@app/themes/basic',
                'baseUrl' => '@web/themes/basic',
                'pathMap' => [
                    '@app/views' => '@app/themes/basic',
                ],
            ],
        ],
    ],
];

does it means that the main.php in the folder @app/themes/basic/layouts/main.php will replace the main.php in the folder @app/views/layouts/main.php ?

and how yii2 achieve this? I couldn’t figure out though I traced the controller to find something which could help me understand the logic.

This happen because when Yii2 perform a render check in config / components the real mapping for views container direrctory.

This can be related to all the views as in your case

    'view' => [
        'theme' => [
            'basePath' => '@app/themes/basic',
            'baseUrl' => '@web/themes/basic',
            'pathMap' => [
                '@app/views' => '@app/themes/basic',
            ],
        ],
    ],

or can be related to some vendor or modulo as

'view' => [
     'theme' => [
        'pathMap' => [
           '@dektrium/user/views' => '@backend/views/my-view-user'  // mapping per overriding s dektrium  views with personal  views 
         ],
     ],
 ],

you can take a look at reference doc for views http://www.yiiframework.com/doc-2.0/yii-base-view.html

and https://github.com/yiisoft/yii2/blob/master/framework/base/View.php