Met behulp van een eigen bibliotheek in uw extensies

From Joomla! Documentation

Revision as of 12:41, 4 November 2016 by AboutTime (talk | contribs)

Er zijn verschillende manieren hoe u uw eigen bibliotheek met Joomla ' s autoloader en gebruik het in uw toestel. Al deze benaderingen zijn gebaseerd op de JLoader klasse. In deze tutorial gaan we er van uit dat uw bibliotheek zich bevindt in de "'/bibliotheken/mylib"' map.

Het ontdekken van Klassen

Klassen in een map die een naamgevingsconventie volgen kunnen worden geregistreerd, samen met JLoader ontdek () - methode. De ontdekmethode lijkt op de bestandsnamen in een map en registreerd klassen op basis van die namen. Aanvullende argumenten kunnen worden gebruikt voor het bijwerken van de klasse registratie en genest in sub-mappen.

// Register all files in the /libraries/mylib folder as classes with a name like:  MyLib<Filename>
JLoader::discover('Mylib', JPATH_LIBRARIES . '/mylib');

Zoals je kunt zien werkt deze methode als u speciale naamgeving volgt:

  • /mylib/user.php zal gelijk zijn aan MylibUser
  • /mylib/userhelper.php zal gelijk zijn aan MylibUserHelper

De voorvoegsel (Prefix) Loader

Sinds Joomla Platform 12.1 (Joomla! CMS 3+), is er de mogelijkheid om in te achterhalen waar de auto-loader kijkt gebaseerd op basis van een class prefix (voorheen alleen de "J" prefix werd ondersteund, gebonden aan de /libraries/joomla-map). Dit wordt gedaan met de registerPrefix() methode en zorgt voor een aantal scenario's:

  • Een ontwikkelaar kan het registreren van de prefix van aangepaste klassen, en een root pad om de auto-loader om ze te vinden.
  • Een ontwikkelaar kan een extra pad registreren voor een bestaand voorvoegsel (b.v. dit laat het Joomla CMS toe op maat gemaakte bibliotheken te hebben, maar nog steeds met de "J" - prefix).
  • A developer can register a force override for a prefix. This could be used to completely override the core classes with a custom replacement.

Convention

The class name must be in camel case and each segment of the name will represent a folder path where the last segment of the name is the name of the class file. If there is only one part to the class name, the auto-loader will look for the file in a folder of the same name. Folder names must be in lower case.

Examples:

  • PrefixUserModel class should be located in PATH_TO_PREFIX/user/model.php
  • PrefixUser class should be located in PATH_TO_PREFIX/user/user.php

Our scenario examples:

  • MylibUserHelper class should be located in /libraries/mylib/user/helper.php
  • MylibUser class should be located in /libraries/mylib/user/user.php


There is no limit to the depth to which the auto-loader will search, providing it forms a valid path based on the camel case natural of the class name. Note that while acronyms and names such as HTML, XML and MySQL have a standard presention in text, such terms should observe camel case rules programmatically ("HTML" becomes "Html", "XML" becomes "Xml" and so on).

Usage

// Tell the auto-loader to look for classes starting with "Mylib" in a specific folder.
JLoader::registerPrefix('Mylib', JPATH_LIBRARIES . '/mylib');
// Tell the auto-loader to also look in the /libraries/cms folder for "J" prefixed classes.
JLoader::registerPrefix('J', JPATH_PLATFORM . '/cms');
// Tell the auto-loader to reset the "J" prefix and point it to a custom fork of the platform.
JLoader::registerPrefix('J', '/my/platform/fork', true);

Note on registerPrefix

For Joomla CMS 2.5, prefix cannot start with the same letter, otherwise the first matched will be tried to load. For example if we register prefix 'JM', as 'J' prefix exists so JLoader will find 'J' prefix. When it tries to search for class in the registered path of 'J', no class found.

Namespace Loader

As of Joomla CMS v3.1.2, the core autoloader has had support for autoloading PSR-0 style namespaced libraries. (You can read up on PHP namespaces here. If you only support Joomla on PHP 5.3+ (which you do if you only support Joomla CMS v3.x or greater), then you can utilize code that uses native PHP namespaces. In accordance with PSR-0 rules, the directory and file casing must match that of the PHP namespace and class.

Example

  • MyLib\User\UserHelper class must be located in JPATH_LIBRARIES/src/MyLib/User/UserHelper.php

Usage

// Tell the auto-loader to look for namespaced classes starting with MyLib in the JPATH_LIBRARIES/src directory
JLoader::registerNamespace('MyLib', JPATH_LIBRARIES . '/src');

Accessing library from any place

To access our library from any place of an application we should create a system plugin which will register our library with the JLoader. This plugin should fire on onAfterInitialise event:

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.0" type="plugin" group="system" method="upgrade">
    <name>System - Mylib</name>
    <author>Joomla! Project</author>
    <creationDate>March 2013</creationDate>
    <copyright>Copyright (C) 2005 - 2013 Open Source Matters. All rights reserved.</copyright>
    <license>GNU General Public License version 2 or later.</license>
    <authorEmail>admin@joomla.org</authorEmail>
    <authorUrl>www.joomla.org</authorUrl>
    <version>1.0.0</version>
    <description>Simple example plugin to register custom library.</description>

    <files>
        <filename plugin="mylib">mylib.php</filename>
        <filename>index.html</filename>
    </files>
</extension>

Plugin file

<?php
/**
 * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later.
 */

defined('_JEXEC') or die;

/**
 * Mylib plugin class.
 *
 * @package     Joomla.plugin
 * @subpackage  System.mylib
 */
class plgSystemMylib extends JPlugin
{
    /**
     * Method to register custom library.
     *
     * return  void
     */
    public function onAfterInitialise()
    {
        JLoader::registerPrefix('Mylib', JPATH_LIBRARIES . '/mylib');
    }
}

Now you will be able to call classes from your library (located in /libraries/mylib) from any component, module or plugin.