PHP Classes

Add Module PHP Trait: Trait to add pluggable modules to classes

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 64%Total: 311 All time: 7,307 This week: 455Up
Version License PHP version Categories
add-module-trait 1.1Public Domain5.4PHP 5, Language, Traits
Description 

Author

This package provides a trait to add pluggable modules to classes.

The trait can register one or more modules by name that the container classes can call when necessary to implement functionality provided by the external modules.

Multiple registered modules can be called by name all at once or using generators to call each registered module one by one iterating over all the modules.

Innovation Award
PHP Programming Innovation award nominee
June 2015
Number 2


Prize: One downloadable e-book of choice by O'Reilly
Many packages can be extended using plugin modules.

This package implements a modular plugin functionality using a trait, so you can add plugin module support to any package without having to alter the package too much.

Manuel Lemos
Picture of Matthew Knowlton
  Performance   Level  
Name: Matthew Knowlton is available for providing paid consulting. Contact Matthew Knowlton .
Classes: 6 packages by
Country: United States United States
Age: 28
All time rank: 2036290 in United States United States
Week rank: 360 Up38 in United States United States Up
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Example

<?php

/* In the Shopping Cart Definition Page */

require_once('addModule.trait.php');

class
shoppingCart{
    use
addModules;
    private
$products;
   
    public function
__construct(){
       
$this->defineModuleType('getProductFromDB', 'single');
       
$this->defineModuleType('checkVoucherCode', 'multiple');
       
       
//initialize shopping cart
   
}
   
    public function
addVoucherDiscountModule($module){
       
$this->addModule('checkVoucherCode', $module);
    }
   
    public function
setProductDataModule($module){
       
$this->addModule('getProductFromDB', $module);
    }
   
    public function
addItem($partNo){
        if(!isset(
$this->products[$partNo])) {
           
$this->products[$partNo] = $this->runModules('getProductFromDB', $partNo)[0];
        }
       
//run add item code
   
}
   
   
//...
   
   
public function checkVoucherCode($voucherCode){
        foreach(
$this->yieldModuleResults($voucherCode) as $results){
            if(
$results['validCode'] != true) continue;
            else {
               
//calculate voucher discount
           
}
        }
    }
}


/* In the Shopping Cart Setup Page */

$cart = new shoppingCart();

$cart->setProductDataModule('getProductFromDatabase');

$cart->addVoucherDiscountModule(function($inputCode){
   
$voucherCodes = getVoucherCodesFromDB();
    foreach(
$voucherCodes as $code){
        if(
$code == strtolower($inputCode)){
            return array(
               
'validCode' => true,
               
'discount' => getVoucherCodeDiscount($code),
            );
        }
    }
    return array(
'validCode' => false);

});


  Files folder image Files  
File Role Description
Plain text file addModule.trait.php Class Trait Source
Accessible without login Plain text file addModule.example.php Example Example Class Implementing addModule

 Version Control Reuses Unique User Downloads Download Rankings  
 0%1
Total:311
This week:0
All time:7,307
This week:455Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:-
Examples:91%StarStarStarStarStar
Tests:-
Videos:-
Overall:64%StarStarStarStar
Rank:743