Plugins

Plugins are one of the core components of DatingFramework. Everything inside DatingFramework is made with plugins. So DatingFramework can be extended with your own plugins.

  1. Plugin Hooks
  2. Plugin Directory Structure
  3. Plugin Class
  4. Plugin Interface
  5. Plugin Installation
  6. Custom Hooks
  7. List of Action Hooks

Plugin Hooks

Hooks are the central concept in the DatingFramework Plugin System



/*

The function you pass to this hook will be called whenever a new user is created.

*/

     
Plugin::hook("account_create", function($user){
	
	print_r($user); // prints the newly created User Object. 
	
});   



*note: Whenever a user is deleted our Plugin System fires an event called users_deleted. If your plugin has any database record regarding that user you have to delete that record.



/*

The function you pass to this hook will be called whenever a user is deleted.

*/

/* $array_of_user_ids is an array of all users ids are being deleted */   
Plugin::hook("users_deleted", function($array_of_user_ids){
  
  //do something. You can delete from your plugin database table.
  
});   



Plugin Directory Structure

Every plugin should have its own folder in app/Plugins. Inside that folder the plugin should have a PluginName.php inside which all the hooks and other details of the plugin must be present.

  • app
    • Plugins
      • SamplePlugin
        • SamplePlugin.php
        • routes
        • controllers
        • assets
        • models

Plugin Class

The plugin's folder should have a PluginName.php inside which all the hooks and other details of the plugin must be present.



/*

SamplePlugin.php

*/

use App\Components\PluginAbstract;
use App\Components\Plugin;



class SamplePlugin extends PluginAbstract
{
	public function author()
	{
		return 'Your Name';
	}
	public function description()
	{
		return 'Description about this plugin';
	}
	public function version()
	{
		return '0.0.1';
	}
	public function website()
	{
		return 'www.sampleplugincompany.com';
	}
	public function hooks()
	{
		// register all the hooks here
		
		
		Plugin::hook("account_create", function($user){
	
		print_r($user); // prints the newly created User Object. 
	
		}); 
		
		
	}	
}  

Plugin Abstract

All the plugins must extend the App\Components\PluginAbstract. The following methods should be defined:

method return type description
author string Returns a string containing information about the author. Its either the name of the individual developer or an organization
description string Returns a string containing the functionality of the plugin
website string Returns a string containing the website of the individual developer or an organization
version string Returns the string with the current semantic version number
hooks void Does not return anything. In this function you can define all the hooks.

These are optional methods:

method return type description
routes void Define all the routes for your plugin here
autoload void Directories to autoload the classes can be defined here
isCore boolean Defines whether the plugin will be a Core Plugin or not. Simply override this abstract method and return true or false. If this method is not redefined in your Plugin class by default your plugin will be a normal plugin that can be enabled and disabled by admin.

*note : Core Plugins can't be disabled by admin after installation.

Plugin Installation

The Admin Panel will recognize all the plugins that are in the Plugins folder. But not all the plugins will be activate. The Admin needs to manually install a plugin. If your plugin has its own database tables or assets then it needs to have a class called PluginNameInstall. In which you can specify the necessary actions like creating tables when the Admin instals a plugin. Having a PluginNameInstall is optional. You need it only when you have to create tables. If your plugin folder has a folder called assets, then the ocntents of those will automatically be moved to public/plugins/PluginName/, this will happen automatically, you dont need to specify this. If you have a asset folder, consider it's contents moved to the public folder automatically.



/*

SamplePluginInstall.php

*/


use App\Components\PluginInstallInterface;

class SamplePluginInstall extends PluginInstallInterface
{
	
	public function install($info)
	{
		
		// Code for Table creations or things like sending a request to your server that someone installed your plugin.
		/* 
			$info contains array(
			"domain_name" => "sampleuser.com", 
			"ip_address" => "0.0.0.0", 
			"action" => "install") 
		*/
		
		// You can use Laravel's Schema class to create tables.
		
	}
	public function uninstall($info)
	{
		// Called when the Admin uninstalls the plugin. This is where you drop the tables your plugin created.
		/* 
			$info contains array(
			"domain_name" => "sampleuser.com", 
			"ip_address" => "0.0.0.0", 
			"action" => "uninstall") 
		*/
	}
}  

Custom Hooks

You can use Plugin::add_hook method to create your own hooks for internal use cases or allow other plugins to take advantage of yours. The common practice to name the hook is pluginname_hookname



/*

SamplePlugin.php

*/

use App\Components\PluginAbstract;
use App\Components\Plugin;



class SamplePlugin extends PluginAbstract
{
	public function author()
	{
		return 'Your Name';
	}
	public function description()
	{
		return 'Description about this plugin';
	}
	public function version()
	{
		return '0.0.1';
	}
	public function website()
	{
		return 'www.sampleplugincompany.com';
	}
	public function hooks()
	{
		
		Plugin::add_hook("sampleplugin_newhook", function($args){
			
			// anything to process or modify before passing the $args to all the listeners of this hook
			
			Plugin::apply_hooks("sampleplugin_newhook", $args);
			
		})
		
		
	}	
}  

To fire a hook:



/*

Add this to your controller or any other files that need to fire the hook

*/


Plugin::fire("sampleplugin_newhook", $args);


*note : Plugin::fire() fires hooks added through Plugin::add_hook() and Plugin::apply_hooks() fires hooks added through Plugin::hook()

Plugin Autoload

If a Plugin has to load it's controllers, models, and other classes just return array of absolute paths of directories from Plugin autoload() method.


 /*

SamplePlugin.php

*/

use App\Components\PluginAbstract;
use App\Components\Plugin;



class SamplePlugin extends PluginAbstract
{
	....
  ...
  
  /**
  * Used to load models, controllers, repositories and other classes
  *
  * @param    void
  * @return  array of absolute paths of directories
  
  */
  
	public function autoload()
	{
  
		return array(
			Plugin::path('SamplePlugin/Controllers'),
			Plugin::path('SamplePlugin/Repositories'),
			Plugin::path('SamplePlugin/Models'),
		);
    
	}
  
}

    

Plugin autoloader does not load directories recursively so mention each directories seperately. Plugin::path() method gives the absolute path to app/Plugins Directory. Plugin::path('SamplePlugin/Controllers') returns absolute path to .../app/Plugins/SamplePlugin/Controllers/ Directory.

Plugin Routes & Controllers

Plugin Routes and Controllers are same as laravel 5.1 spcification. But while registering routes full in Plugin route() method, full Namespace of that controller must be given. For example:


      /* app/Plugins/SamplePlugin/Controllers/SamplePluginController.php */
      <?php
      namespace App\Http\Controllers;
      
      use App\Http\Controllers\Controller;
      
      class SamplePluginController extends Controller {
        
        //code of controller
        public function sample () {
          
          //code of sample method
        }
        
      }
    
    

The Routes for above sample() method


      /* app/Plugins/SamplePlugin/SamplePlugin.php */
      ....
      
      public function routes() 
      {
        Route::get('/SamplePlugin/sample_route', 'App\Http\Controllers\SamplePluginController@sample');
      }
      
      ......
    
    

Plugin Views

Plugins can have it's own views located at /app/Plugins/PluginName/views directory. Plugin::view('PluginName/ViewName') loads that specified view of spcified Plugin name.

Example :


  /*

SamplePlugin.php

*/

use App\Components\PluginAbstract;
use App\Components\Plugin;



class SamplePlugin extends PluginAbstract
{
	....
  ...
  
  /**
  * Used to register routes
  *
  * @param        void
  * @return    array of absolute paths of directories
  
  */
  
	public function routes()
	{
  
		Route::get('/SamplePlugin/test_route', function () {
        
        $data = ['key1' => 'value1', 'key2' => 'value2'];
        
        return Plugin::view('SamplePlugin/test_plugin_view', $data);
      
    });
    
	}
  
}

    

This code Plugin::view('SamplePlugin/test_plugin_view', $data) loads test_plugin_view.blade.php view from app/Plugins/SamplePlugin/views/ path.

Plugin Assets

Plugins can have it's own assets located at /public/plugins/PluginName/ directory. It basically points to your public/plugins/ directory. Plugin::asset('PluginName/AssetName') generate a URL for an asset using the current scheme of the request (HTTP or HTTPS).

Example 1 :


      $url = Plugin::asset('SamplePlugin/js/sampleCustom.js');
    

$url will be http://yourdomain/plugins/SamplePlugiSamplePlugin/js/sampleCustom.jsn. The Plugin::asset() can be used in blade view files also.

Example 2 :


    <!--This is test_plugin_view.blade.php-->
    <?php use App\Components\Plugin; ?>
    <!DOCTYPE html>
    <html>
    <head>
    <title>test_plugin_view
    <link rel="stylesheet" href="{{{Plugin::asset('SamplePlugin/js/sampleCustom.js')}}}">
    </head>
    <body>
    </body>
    </html>
    <!--end test_plugin_view-->
    

*note : use App\Components\Plugin must be included at very top of your view page.

Plugin Language

Our Plugin System also supports multiple language feature. Plugin languages are located at app/Plugins/PluginName/language directory. Each language must contain PuguinName.php file. At the time of plugin installation all the supported languages by the application automatically get copied to resources/lang directory.


All language files simply return an array of keyed strings. For example:


    /* SamplePlugin/language/en/SamplePlugin.php */
    <?php

      return [
          'welcome' => 'Welcome to our plugin'
      ];
    

In view files you have to write like {{{trans('SamplePlugin.welcome')}}} to make language translation. As a plugin developer, you have to provide all the translated language files.

All Plugin Hooks

All the plugins must extend the App\Components\PluginAbstract. The following methods should be defined:

method return type description
author string Returns a string containing information about the author. Its either the name of the individual developer or an organization