Child pages
  • cAutoload
Skip to end of metadata
Go to start of metadata

CONTENIDO provides autoloading for source files of classes/interfaces which are delivered by a CONTENIDO package. The main goal of autoloading is to reduce the list of needed includes which is usually at the beginning of scripts. By implementing an autoloader, the PHP engine has the possibility to load the file while trying to use a class/interface.

How it works?

The CONTENIDO autoloader is initialized during the application startup process. The autoloading solution in CONTENIDO uses the class map strategy. It uses a generated class map configuration file, which is available inside data/config/{environment}/ folder.

data/config/{environment}/config.autoloader.php

Each class type name is mapped to a file which contains the implementation of the required class type. By referencing a class type, the autoloader will load the needed file if not done before.

Example

Usually you have to ensure that a class file is already loaded by using include/require statements or by using CONTENIDO's cInclude function:

cInclude('classes', 'contenido/class.articlelanguage.php'); 
$oArt = new cApiArticleLanguage();

With an autoloader the manually loading is not required anymore.

$oArt = new cApiArticleLanguage();

Classes/Interfaces available by the autoloader

At the moment all available classes/interfaces inside following directories of a CONTENIDO installation: contenido/classes/

Note

The autoloader doesn't handle loading of classes which don't belong to the CONTENIDO package. This means, custom classes (i.e. classes provided by plugins or modules) aren't automatically available for the autoloader. Read the section below, if you want to use autoloading for custom classes.

 

Extending the class map configuration

Don't edit the class map configuration manually, the next update could overwrite your changes. The autoloader is extendable by adding an additional user defined class map file inside the "data/config/{environment}/" folder, which could contain further class map settings or could overwrite settings of main class map file.

data/config/{environment}/config.autoloader.local.php

This file will not be overwritten during an update.

The content of the user defined file should have the following structure:

return array(
    '{classname_1}' => '{path_to_classfile_1}',
    '{classname_2}' => '{path_to_classfile_2}',
    '{classname_3}' => '{path_to_classfile_3}'
);

Where {classname_X} is the name of the class/interface and {path_to_classfile_X} is the path (from CONTENIDO installation folder) to the file which contains the implementation of the class/interface.

Example

Let's assume that CONTENIDO is installed in folder /var/www/ which contains an additional library "myLib" (full path: /var/www/myLib/) with a class "myFoobarClass" in file "class.myfoobarclass.php" (full path: /var/www/myLib/class.myfoobarclass.php). Then the user defined class map file should contain an entry for this like:

return array(
    ...
    'myFoobarClass' => 'myLib/class.myfoobarclass.php',
    ...
);

 

Auto generation of user defined class map configuration

If you don't want to maintain the user defined class map configuration manually, then you may let a copy of the command line script contenido/tools/create_autoloader_cfg.php (which is adapted to your requirements) do the job.

Do following steps to achieve this:

  • Create a copy of create_autoloader_cfg.php and name it e.g. create_local_autoloader_cfg.php
  • Open create_local_autoloader_cfg.php and adapt it to your requirements (see Initialization/Settings)
    • Set setting $context->destinationFile to

      $context->destinationFile = $context->contenidoInstallPath . '/contenido/includes/config.autoloader.local.php';

      Note

      Don't use another file name, it's predefined and has to be "config.autoloader.local.php"

    • Define paths which should be parsed recursively, e.g.

      $context->pathsToParse = array(
          $context->contenidoInstallPath . '/my_path/',
          $context->contenidoInstallPath . '/my_other_path/',
      );
    • Change class type finder options (if required), e.g.

      // class type finder options
      $context->options = array(
          // list of directories which are to exclude from parsing (case insensitive)
          'excludeDirs'       => array('.svn'),
          // list of files which are to exclude from parsing (case insensitive),
      	// also possible regex patterns like /^~*.\.php$/
          'excludeFiles'      => array(),
          // list of file extensions to parse (case insensitive)
          'extensionsToParse' => '.php',
          'enableDebug'       => false,
      );
  • Run the class map creator by typing following to the command line:

    php create_local_autoloader_cfg.php

    Note

    PHP needs write permissions for folder "contenido/includes/"

  • Check the generated/upated file "config.autoloader.local.php" inside "contenido/includes/" folder

Adding class map configuration manually

As an addition to the user defined class map configuration, the CONTENIDO autoloader provides the feature to add class map configurations manually by using following methods:

  • cAutoload::addClassmapConfig(array $config)
    Adding additional autoloader classmap configuration.

    // Structure is: "Classname" => "Path to classfile from CONTENIDO installation folder"
    $config = array(
        'myPluginsClass' => 'contenido/plugins/myplugin/classes/class.myPluginClass.php',
        'myPluginsOtherClass' => 'contenido/plugins/myplugin/classes/class.myPluginsOtherClass.php',
    );
    cAutoload::addClassmapConfig(array $config);
  • cAutoload::addClassmapConfigFile($configFile)
    Adding additional autoloader class map configuration file.

    $configFile = '/path/to/additional/classmap/config.php';
    cAutoload::addClassmapConfigFile($configFile);


    The provided file must return a class map configuration array as follows:

    <?php
    // Structure is: "Classname" => "Path to classfile from CONTENIDO installation folder"
    return array(
        'myPluginsClass' => 'contenido/plugins/myplugin/classes/class.myPluginClass.php',
        'myPluginsOtherClass' => 'contenido/plugins/myplugin/classes/class.myPluginsOtherClass.php',
        'myCmsClass' => 'cms/includes/class.myCmsClass.php',
    );

Note

Since the autoloader is implemented for CONTENIDO, it doesn't support to load classfiles being located outside of the CONTENIDO installation folder.

 

Extending CONTENIDO core with autoload mechanism

By using the CONTENIDO autoloader it's possible to extend/overwrite CONTENIDO core classes (except classes inside conlib directory) without changing the core files.

Let's assume, you want to use your own Template class in Modules, but everything should still be downwards compatible.

Do following steps to achieve this:

  • Create a user defined folder (if required) which should contain your own Template class file.
  • Create a class file (e. g. class.mytemplate.php), which should contain the implementation of new class Template.
  • Implement your own Template class. Ensure that the interface of your Template class is identical to the CONTENIDO Template class. This means, each public accessible property, method should have the same interface as in the original CONTENIDO Template class (Same names, properties, parameters, etc.).
  • Modify the functions to your requirements.
  • Add the class map configuration of your new Template class to the user defined file config.autoloader.local.php or regenerate the user defined class map file (see extending class map, auto generation of class map and adding manual class map configuration).

 

Note

There is one main disadvantage by using this way of extending the CONTENIDO core. Each time after an update of your CONTENIDO installation it's strongly recommend to check your user defined implementations against changes in original CONTENIDO core files and, if applicable, to adapt your files to those changes.

1 Comment

  1. Overloading CONTENIDO core classes (see "Extending CONTENIDO core with autoload mechanism") cannot be achieved by plugins whose config.plugin.php can also extend the mapping array used for autoloading. This is probably due to the fact, that plugins are loaded later ...