RedBeanPHP
The Power ORM

Plugins:Create Your Own

Plugins are an elegant way to add new functionality to RedBeanPHP. To create a plugin, create a class or function and use:

    R::ext'doSomething', function() {
        return 
MyClass::myMethod();
    } );

to add your new feature to the R-class (required PHP 5.3+ and RedBeanPHP 3.5+). For older versions of PHP use:

    R::ext'doSomething',
        array( 
'MyClass''MyStaticMethod' )
    );

Now you can use your plugin like this:

    R::doSomething();

it's that easy! Here is a list of some interesting 3rd party plugins for RedBeanPHP !

Installing a Plugin

Installing plugins is really easy. Most plugins, the legacy plugins for instance, automatically register their plugin functions with the R-facade for your convenience. So to install the Cooker plugin (one of the legacy plugins) use:

    require 'plugins/Cooker/Cooker.php';

Yes, that's all. Just include the file and you're done. Sometimes the plugin author has additional instructions and might require manual registration with the R-facade, this is mainly because the author is an object 'purist'. To install these plugins correctly consult the corresponding readme files or manuals.

Testing your plugin

You can use the RedBeanPHP unit test facilities to test your plugin. Simply write a test class extending RedUNIT\Base like this:

    class TestMe extends \RedUNIT\Base {
        public function 
testMethod()
        {
            ...
        }
    }

The RedUNIT test facility will simply run all methods starting with 'test'. The tests will run for every driver. If you want your test to run for a specific driver only extend RedUNIT\MySQL or RedUNIT\Postgres etc instead. To perform a test use the asrt($a, $b) command. If $a === $b the test will pass and the number of the test will be printed on the screen, otherwise the program will exit immediately printing an error message. A list of test commands:

    asrt$a$b ); //pass if a === b otherwise fail
    
pass(); //count as passed
    
fail(); //fail (i.e. die with error message)
    
testpack$message ); //print message

To run your tests:

php runtests.php "testing/Mytest.php" "RedUNIT\Base\Mytest"

First argument is the file to run, second argument is the name of the testing class contained in the file. You can not test more than one test file per plugin. Test coverage of your plugin will be calculated and printed. A report containing all missed lines will be saved to the cli folder. You can specify a third parameter to only count code lines of specific files, only lines in matching filenames will be counted in the code coverage statistics.

Writing a new Query Writer

To add a new, custom Query Writer for your favourite RDBMS simply wrap the writer in a plugin. To activate your writer people should issue the following command:

    //for instance to install DB2 database support
    
require 'db2.php';
    
R::setupDB2($dsn$user$pass);

The reason for this is that it is too hard for me to maintain all writers in the core, especially writers for commercial database platform I can't even obtain. Therefore they are separated from the core and hosted in their own repositories by the contributors. Query Writer authors can make the plugins available using the setup-approach described above. Note that this is also more flexible as it allows a database writer plugin to select a different driver (OCI instead of PDO for instance) or maybe even a different toolbox. Query writers should implement all methods defined in the Query Writer interface (see API). Many methods are already implemented in the Abstract Query Writer, these can be reused if they are appropriate for the new Query Writer as well.

Legacy Plugins

RedBeanPHP 4 KS goes back to the roots of RedBeanPHP, on-the-fly ORM. As such many additional functionalities and modules have been removed from this core. However they have been moved to a special plugin repository on github.


back to main menu

Donate to RedBeanPHP using Monero:
47mmY3AVbRu 7zVVd4bxQnzD
2jR7PQtBJ cF93jWHQ
rP7yRED4qr fqu6G9Q8ZNu7
zqwnB28rz76 w7MaExf
mALVg69yFd 9sUmz
(remove spaces and new lines)

Performance monitor: this page has been generated in 0.018281936645508s. Is the performance lacking? Please drop me an e-mail to notify me!

Partners  
Uurboek.nl PapelDigital

 

RedBeanPHP Easy ORM for PHP © 2024 () and the RedBeanPHP community () - Licensed New BSD/GPLv2 - RedBeanPHP Archives
RedBeanPHP, the power ORM for PHP since 2009.

Privacy Statement