Welcome Guest, Not a member yet? Register   Sign In
Extending class Config\Autoload
#1

(This post was last modified: 09-07-2024, 09:02 AM by ozornick.)

Why? I would like to automate the loading of modules through setting up a single Config\Components file. It will have paths, names, psr... for components and other parameters that need access from the entire project. He needs to edit Config\Autoload to add new dependencies.
If you put a code call in app/Common.php this won't work - the configs haven't loaded yet. 
The Registrar cannot be used.
Maybe this is implemented using Services
Example:

PHP Code:
    // Call inject() method
    // Edit PSR-4 for Autoload
    $autoloadConfig->ps4 + [
        // APP_NAMESPACE  => APPPATH,
        'Tools\\' => ROOTPATH 'tools/',
        'Ads\\' => ROOTPATH 'ads/',
    ];
    // Edit non-class files 
    $files $autoloadConfig->files + [
        ROOTPATH 'tools/Common.php',
    ];

    // isLoaded($name) method
    // getLoaded() method 
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#2

The Config\Autoload is loaded here:
https://github.com/codeigniter4/CodeIgni...t.php#L233
Reply
#3

I don't get what you want well. I don't understand your sample code.
What is the filename?
What is $autoloadConfig?

Why don't you just edit Config\Autoload?
Reply
#4

Yes, this is the easiest to insert into Autoload. But I wouldn't want to manually add all the names, namespaces...
I see it as composition or other: Components::inject($autoloadConfig)
After the call, it will add paths (or other changes) automatically matching for the tools/, ads/ folders. Imagine that there are ~20 directories with modules. They need to be entered manually. Dynamically scanning, I like it more.
$autoloadConfig = Config\Autoload array
The methods in the example from the Config\Components class
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#5

(This post was last modified: 09-08-2024, 01:55 AM by kenjis.)

If you want to change the values in Config\Autoload,
1. you need to do it before loading Autoloader. That is you need to do without autoloading. Because Config\Autoload is a config object for Autoloader. It must be instantiated before the registration of Autoloader.
2. you must pass the modified Config\Autoload object to the initialize() method in https://github.com/codeigniter4/CodeIgni...t.php#L244

After all, you seem to need to customize the Boot class (and index.php https://github.com/codeigniter4/CodeIgni...hp#L53-L56).
Reply
#6

I overloaded the constructor
PHP Code:
    /**
    * Extend Autoloader for modules
    */
    public function __construct()
    {
        parent::__construct();

        $modulesDirs = new FilesystemIterator(ROOTPATH 'components/');

        foreach ($modulesDirs as $moduleDir) {
            $namespace APP_NAMESPACE '\\' $moduleDir->getFilename();
            $this->psr4[$namespace] = $moduleDir->getRealPath();

            $commonFile $moduleDir '/Common.php';

            if (is_file($commonFile)) {
                $this->files[] = $commonFile;
            }
        }
    
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB