missing-score
04-22-2006, 09:59 PM
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
// 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
class BlogPlugin {
private $entryNumber = 0;
public function shortenEntry( $entry ){
if( $entryNumber > 0 )
return substr( $entry, 0, 200 ) . ' [...]';
}
++$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):
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/ .
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
// 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
class BlogPlugin {
private $entryNumber = 0;
public function shortenEntry( $entry ){
if( $entryNumber > 0 )
return substr( $entry, 0, 200 ) . ' [...]';
}
++$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):
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/ .