News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Ohara, a micro-framework for mods

Started by Suki, March 04, 2018, 07:17:20 PM

Previous topic - Next topic

Suki

This has been in my to-do list for quite some time.

Ohara is a simple class that allows you to build mods in a clean, organic way. My goal was to offer a helper class for mods to re-use while providing a set of simple guidelines any mod can follow.

By adding their corresponding parameters to a config file (from either some PHP or JSON code), the class can do the following:

- Adds a form generator. You can programatically create forms and use them wherever you want, no template files are required.
- Create simple admin setting pages.
- Can create custom pages (IE action=something)
- Add permissions.
- Adds an abstration layer to both $modSettings and $txt strings, making it easy to access them in a normalized way (the same way is shared by any other mod using the class).
- A very simple DBAL for handling DB stuff. Instead of writting SQL code, just pass some data and let the class save/update it to your table.
- DI (dependency injection) You can lazy-load your classes/services. All Ohara services are available as keys in your $this var: $this['tools'], $this['db'], etc. Your custom services will also be added this way.
- autoload your classes.
- Enable/disable hooks on the fly. You can programatically add/remove hooks or even let the forum admin to disable specific parts of your mod without touching the code.
- Cross-page messages/data. Uses sessions. You can set any data and retrieve it on another request.
- Sanitized $_REQUEST and $modSettings vars. You no longer need to worry about them. You don't have to ever use $_REQUEST, $_GET or $_POST ever again.

Those features can be added to your mod without a single line of code, letting you focus on your code.

Some helper functions

- Parser, a very simple way to replace tokens with something else in a string.
- jsonResponse creates an HTTP json response from an array of data. Useful for API/REST communication.
- redirect with token/session support. Any redirect call will include a token and session var by default.


This is an example of a settings array:


$_config = array(
'_availableHooks' => array(
'memberContext' => 'integrate_member_context',
'generalSettings' => 'integrate_general_mod_settings',
'displayContext' => 'integrate_prepare_display_context',
'profile' => 'integrate_load_custom_profile_fields',
),
);


The _availableHooks tells the class which hooks you want to use, every hook is mapped to a method in your class.

An example of adding an external library to the autoload system


// Update or create $modSettings['OharaAutoload']
if (empty($modSettings['OharaAutoload']))
$pref = array(
'namespaces' => array(
'SimplePie' => array('{$vendorDir}/simplepie/simplepie/library'),
),
'psr4' => array(),
'classmap' => array(),
);
else
{
$pref = smf_json_decode($modSettings['OharaAutoload'], true);
$pref['namespaces']['SimplePie'] = array('{$vendorDir}/simplepie/simplepie/library');
}
// Either way, save it.
updateSettings(array('OharaAutoload' => json_encode($pref)));
]


The class needs SMF 2.1 and PHP 7 to work and it also needs mod authors to be already familiar with OOP concepts and practices.

You can use the class in your procedural functions too by creating a new instance of Ohara everytime you need it. You will lose hook enable/disable capabilities but the rest of the features should work.

As for the reason to create this, well, it was largely to be able to use 2.1's full OOP capabilities and to have a normalized way to handle all my mods, every mod sharing the same structure and practices does make it easy to maintain them. By extracting away tedious tasks (admin pages, forms, permissions) you can develop your mods faster without having to write those features each time you want to create another mod.

So yeah, if you like the idea give it a try and see how it goes. I tried to make it as simple as possible but since this takes the whole concept of hooks to another level it might not be as friendly to mod authors as I think it is, in that case let me know or if you have some ideas to further enhance this class lets hear them too :)
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

-Rock Lee-

I'm just getting familiar with the branch 2.1 yet but this seems very useful to put it into practice to see how it goes ... Thanks for sharing @Suki


Regards!
¡Regresando como cual Fenix! ~ Bomber Code
Ayudas - Aportes - Tutoriales - Y mucho mas!!!

Colin

"If everybody is thinking alike, then somebody is not thinking." - Gen. George S. Patton Jr.

Colin

Diego Andrés

Very nice, I'll definitely take a look at it soon

SMF Tricks - Free & Premium Responsive Themes for SMF.

Suki

Thank you for the comments. I really want to push this further.  Got the opportunity to learn from a bunch of really talented mod authors back in the day and they all left a lot of stuff for the community, just want to do the same.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

Advertisement: