Welcome Guest, Not a member yet? Register   Sign In
How to disable Shield on demand?
#1

Hi, 
I want to disable shield on specific conditions to maximize performance, so that no shield class is loaded

I have a condition in BaseController.php
Code:
if (CONDITION))
{           
   $this->helpers = array_merge($this->helpers, ['auth', 'setting']);
}
But the classes are still get loaded. I see them in the files list of the toolbar. The first class which I do not want to load is VENDORPATH\codeigniter4\shield\src\Auth.php

How can I avoid that?
Best regards
Wolfgang
Reply
#2

Sorry, you can't.
auth and email helper are loaded by Composer automatically.

If you are concerned about the time it takes to load a few files, I recommend using Preload.
See https://www.php.net/manual/en/opcache.preloading.php
Reply
#3

When I remove the vendor directory in my example project codeigniter loads 139 files instead of 198. 
So around 60 files would be loaded if Shield is not used, thats to much.
Is there a way to use Shield without composer?
It is not a good idea to change system files but is there a way to avoid the autoloading when I can manipulate the codeigniter system files?
Reply
#4

(11-08-2022, 03:58 AM)wolfgangpue Wrote: Is there a way to use Shield without composer?

We do not support Shield without Composer.
But you could do it by yourself.
Download the source code and install in your repository and configure autoloader for the Shield class files,
and add code to load the helpers when you need.

(11-08-2022, 03:58 AM)wolfgangpue Wrote: It is not a good idea to change system files but is there a way to avoid the autoloading when I can manipulate the codeigniter system files?

No. Composer autoloads the helpers, so CodeIgniter system does not matter.
Reply
#5

I installed Shield without composer and added following constructor to Autoload-class.
PHP Code:
function __construct() {
            $this->psr4['CodeIgniter\\Settings'] = APPPATH 'ThirdParty/settings-2.1.0/src';
            $this->psr4['CodeIgniter\\Shield'] = APPPATH 'ThirdParty/shield/src';
            
            $this
->files[] = APPPATH 'ThirdParty/shield/src/Helpers/auth_helper.php';
            $this->files[] = APPPATH 'ThirdParty/shield/src/Helpers/email_helper.php';
       
            parent
::__construct();      
        
}  
Shield is working like expected. I also tried to load shield later on demand in the BaseController, instead of Autoload-class but without success.
My code in the initController function, but not all shield classes get loaded.

PHP Code:
  $autoloader service('autoloader');       
  $nameSpaces 
= array();
  $nameSpaces['CodeIgniter\\Settings'] = APPPATH 'ThirdParty/settings-2.1.0/src';
  $nameSpaces['CodeIgniter\\Shield'] = APPPATH 'ThirdParty/shield/src';
  $autoloader->addNamespace($nameSpaces);   
   $autoloader
->loadClass('CodeIgniter\\Settings\Config\Services');
  $autoloader->loadClass('CodeIgniter\\Shield\Config\Services');

  include_once APPPATH 'ThirdParty/shield/src/Helpers/auth_helper.php';
  include_once APPPATH 'ThirdParty/shield/src/Helpers/email_helper.php'
What do I need to achive a correct lazy load of the auth service in BaseController?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB