Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Build a plugin system into your application

    What I'm offering here is a script to offer a simple plugin architecture to a script or scripts you have written. It is PHP 5 based due to the Object functionality required. It is simple to use, and is designed in such a way so that:

    • If you are integrating it into an application, minimal code changes are needed
    • If you are building with it, you don't even need to think about plugins
    • There is 0 chance of conflict becuase it is completley enclosed

    Firstly, go to http://www.tinyplugin.com/download.html and download the ZIP package, or alternativley browse the source at http://www.tinyplugin.com/docs/source.html . In the download ZIP you will see a PHP file for each class as well as TinyPluginSuite.php which is all the inferfaces/classes bundled into one file.

    Here is an example of how you would use the system in an application. First, here is the simple application without any plugins setup:

    PHP Code:
    <?php

    // Get the TinyPluginSuite
    include_once 'TinyPluginSuite.php';

    $db = new PDO'mysql:host=localhost,dbname=testdb''user''pass' );

    $entries $db->query
       
    'SELECT `id`, `title`, `content` FROM `blog`' 
    );

    while( 
    $row $entries->fetch() ){
       
       
    // By using a 'TinyPluginObjectArray' object, you can
       // still use the variable like an object or array and
       // also know that you can easily add plugins later
       
    $entry = new TinyPluginObjectArray($row);
       
       
    // Also, notice how we can access the variables via
       // array syntax ($array['variable']) or object access
       // syntax ($array->variable)
       
    echo "<h1><a href=\"entry.php?id={$entry->id}\">{$entry['title']}</a></h1>";
       echo 
    "<p>{$entry['content']}</p>";
       

    }

    ?>
    As you can see, we just pull in the library and use one of the constructs (TinyPluginObjectArray). This should barely affect the speed of your application while still leaving it open for plugins.

    Now, here is what we are going to make our plugin do:

    • Escape all the values returned from the DB (this should be done as part of your core application logic, i'm making the plugin do this as an example).
    • Shorten all entries after the first entry
    • Apply advert links to some select words in posts


    As you can see, the list of features isn't the usual, especially for a blog, but they are good examples for a plugin.

    PHP Code:
    <?php

    class BlogPlugin {

       private 
    $entryNumber 0;

       public function 
    shortenEntry$entry ){
          if( 
    $entryNumber )
             return 
    substr$entry0200 ) . ' [...]';
          }
          ++
    $this->entryNumber;
       }

       public function 
    replaceAdWords$entry ){
          return 
    str_ireplace(
             array( 
    'books''media''development' ),
             array
             (
                
    'http://www.amazon.com/'
                
    'http://www.play.com/',
                
    'http://www.codingforums.com/'
             
    ),
             
    $entry
          
    );
       }

       
    // Required, to describe the plugin
       
    public function describe()
       {
          return array
          (
             
    '*' => new TinyPluginFilter('htmlentities'),
             
    'content' => array
             (
                new 
    TinyPluginFilter( array( $this'shortenEntry' ) ),
                new 
    TinyPluginFilter( array( $this'replaceAdWords' ) )
             )
          );
       }

    }

    ?>
    You would most likley include this from another file, but it doesn't really matter how you do it, just providing the class is there. Then, to utilize the plugin you would add this to the top of the original script (or wherever):

    PHP Code:
    TinyPluginController::instance()->registerPlugin( new BlogPlugin ); 
    Once that code is there, the following happens:

    • All of the returned DB records will be passed through htmlentities()
    • All entries except the first will get shortened to 200 characters.
    • The words 'books', 'media' and 'development' will be replaced with the respective links.



    I hope you may find this useful in some way. You can find out more information about TinyPlugin at http://www.tinyplugin.com and by reading the documentation at http://www.tinyplugin.com/docs/ .

  • #2
    Regular Coder
    Join Date
    Dec 2004
    Location
    Manchester, UK
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's a really simple plugin concept I came up with not so long back - it has event handler/observer type stuff in it.

    http://www.w3style.co.uk/?p=9

    (Sorry that blog has s** all on it - I never have time to write to it).

  • #3
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    Hi,

    I'd like to read the source-code but the link to tinyplugin.com.

    Is someone have a good link or the .zip

    Thanx

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Everything connected to missing-score disappeared...
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    did anyone happen to download the zip file as tinyplugin.com is non-existent.
    thanks for any help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •