Search code examples
phpdependenciescontent-management-systempimcore

Pimcore custom classes


I wrote custom classes and want to use them in pimcore application. I took them to /website/lib/Custom directory on server. Afterwards, I wrote recursive script includer for each Class located in the directory and included that script in /index.php file.

It is absolutely not pimcore standard but it works.

In pimcore/config/startup.php exists snippet:

$autoloaderClassMapFiles = [
    PIMCORE_CONFIGURATION_DIRECTORY . "/autoload-classmap.php",
    PIMCORE_CUSTOM_CONFIGURATION_DIRECTORY . "/autoload-classmap.php",
    PIMCORE_PATH . "/config/autoload-classmap.php",
];
$test = PIMCORE_ASSET_DIRECTORY;
foreach ($autoloaderClassMapFiles as $autoloaderClassMapFile) {
    if (file_exists($autoloaderClassMapFile)) {
        $classMapAutoLoader = new \Pimcore\Loader\ClassMapAutoloader([$autoloaderClassMapFile]);
        $classMapAutoLoader->register();
        break;
    }
}

I guess that this provides inclusion of all those classes put into returning array from autoload-classmap.php. Having in mind that /pimcore/config/autoload-classmap.php exists, the mentioned loop would break at first iteration so classes that I would put into custom autoload-classmap are not going to be included in project.

My question is can I change files from /pimcore directory and expect that everything would be fine after system update?


Solution

  • No, you should not overwrite anything in the pimcore directory, since the files in there get overwritten by the update mechanism.

    You can do what you want by using the /website/config/startup.php which will not get overwritten: https://www.pimcore.org/wiki/display/PIMCORE4/Hook+into+the+startup-process

    But instead of loading all your classes as you did, take advantage of the autoloader by adding this to the /website/config/startup.php:

    // The first line is not absolutely necessary, since the $autoloader variable already gets 
    // set in the /pimcore/config/startup.php, but it is a more future-proof option
    $autoloader = \Zend_Loader_Autoloader::getInstance(); 
    $autoloader->registerNamespace('Custom');
    

    If you are properly using namespaces and naming your files correctly that's all you need to do.