0

I'm writing a composer package that needs to execute a list of safety checks when Composer autoload.php is called and before accessing its classes and methods

I have this composer.json file:

{
    "name": "def-studio/logger",
    "description": "Def Studio logger system",
    "type": "library",
    "require": {
        "php": ">=5.3.0"
    },
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "DefStudio\\Logger\\": "src/"
        }
    }
}

After ./vendor/autoload.php is called and the main class DefStudio\Logger\Logger.php is loaded, I need to execute this method: Logger::perform_checks() everytime the class is loaded.

my code is, actually:

include_once './vendor/autoload.php';
use DefStudio\Logger\Logger;
Logger::perform_checks();


try{
   //random messy code here
}catch(Exception $ex){
   Logger::error("error!", $ex);
}

I need my package to call Logger::perform_checks() everytime autoload.php is called and I don't want to rely on the user to remember to call it.. I wish it to be automagically called when autoload.php is invoked

Is there any way to automatically execute a php script when autoload.php is called?

fabio.ivona
  • 577
  • 7
  • 24
  • If you only want to run a self-check on the library upon being loaded, you might as well use the composer build in [package events](https://getcomposer.org/doc/articles/scripts.md#package-events) for that purpose. – Dejv Dec 11 '18 at 09:58
  • Please mind that if composer autoloader is used properly, the file holding the class is only loaded **once** and the include is **triggered by using the class for the first time**. By calling `Logger::perform_checks()` right after autoload.php include, you are also manually triggering the include of `DefStudio\Logger\Logger.php` file. Did you think about triggering the checks upon `Logger` class instantiation? – Dejv Dec 11 '18 at 10:04
  • @Dejv, Probably I explained badly my question: I don't want to call `Logger::perform_checks()` manually, because I don't know if the developer that is using the package will remember to do so... what you see in my code is the actual package way of working, but I'd like to remove `Logger::perform_checks()` after autoload.php and trigger `Logger::perform_checks()` automatically when the class is loaded, in order to be sure that my checks are performed. – fabio.ivona Dec 11 '18 at 10:33
  • In that case, I would put the checks into a private method of the `Logger` class and call this method on the last line of Logger's `__construct()`. That way, your checks will run each time someone instantiates the class and you will save performance as long as the object is immutable, which should be the case for logging class. – Dejv Dec 11 '18 at 10:40
  • Logger class is not instatiable, it is called throught static methods. And, my target is that perform_checks() is called even when Logger methods are not invoked (that's the case in wich no error occours). It would be fine even to extract perform_checks() from Logger class, the important thing is that it is called when autoload.php is invoked – fabio.ivona Dec 11 '18 at 10:46
  • I keep thinking if your requirement is really necessary. What kind of checks does the `perform_checks()` function/method hold, so that you demand running it right after autoloading is initialized? Does it have something to do with autoloading? – Dejv Dec 11 '18 at 13:02
  • My plugin checks and eventually sets up php errors logging through set_error_handler function. As you can see, this kind of error logging is not triggered by the developer, so I can't wait for him to call Logger class – fabio.ivona Dec 11 '18 at 14:19
  • 1
    I see. So in that case, I suggest setting up [your own autoloader](https://stackoverflow.com/a/12788474/1719108), which will include `vendor/composer/autoload_*.php` files with associative arrays of files to go through during class lookup in your [custom autoload function](http://php.net/manual/en/language.oop5.autoload.php). That way, you will be able to incorporate `perform_checks()` function trigger during autoloading. – Dejv Dec 11 '18 at 14:28

1 Answers1

1

Thanks to Dejv comment, I found a useful answer in the question linked from him:

Updated my package composer.json file in order to add a script to be executed on autoload:

{
    "name": "def-studio/logger",
    "description": "Def Studio logger system",
    "type": "library",
    "require": {
        "php": ">=5.3.0"
    },
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "DefStudio\\Logger\\": "src/"
        }
        "files": ["src/utilities/autoloader.php"]
    }
}

and in autoloader.php I executed my checks and startup code

this way, when the developer calls include_once './vendor/autoload.php', my code is executed as well

fabio.ivona
  • 577
  • 7
  • 24