Mirgate to PSR12 and other small fixes

This commit is contained in:
andpavlenko committed 2022-04-01 09:32:55 +00:00
1 parent 84ae07b788
commit f4c4f6bac7
184 files changed
+15781 -13117

No files matched your search

+10 -10
View File
@@ -1,6 +1,6 @@
<?php
/**
* BoxBilling
* BoxBilling.
*
* @copyright BoxBilling, Inc (https://www.boxbilling.org)
* @license Apache-2.0
@@ -11,8 +11,8 @@
*/
/**
* Example module Admin API
*
* Example module Admin API.
*
* API can be access only by admins
*/
@@ -21,22 +21,22 @@ namespace Box\Mod\Example\Api;
class Admin extends \Api_Abstract
{
/**
* Return list of example objects
*
* Return list of example objects.
*
* @return string[]
*/
public function get_something($data)
{
$result = array(
$result = [
'apple',
'google',
'facebook',
);
];
if(isset($data['microsoft'])) {
if (isset($data['microsoft'])) {
$result[] = 'microsoft';
}
return $result;
}
}
}
+14 -14
View File
@@ -1,6 +1,6 @@
<?php
/**
* BoxBilling
* BoxBilling.
*
* @copyright BoxBilling, Inc (https://www.boxbilling.org)
* @license Apache-2.0
@@ -12,7 +12,7 @@
/**
* All public methods in this class are exposed to the client using API.
* Always think about the information you are exposing.
* Always think about the information you are exposing.
*/
namespace Box\Mod\Example\Api;
@@ -20,18 +20,18 @@ namespace Box\Mod\Example\Api;
class Client extends \Api_Abstract
{
/**
* From client API you can call any other module API
*
* From client API you can call any other module API.
*
* This method will collect data from all APIs and merge
* into one result.
*
*
* Be careful not to expose sensitive data from the Admin API.
*/
public function get_info($data)
{
// call custom event hook. All active modules will be notified
$this->di['events_manager']->fire(array('event'=>'onAfterClientCalledExampleModule', 'params'=>array('key'=>'value')));
$this->di['events_manager']->fire(['event' => 'onAfterClientCalledExampleModule', 'params' => ['key' => 'value']]);
// Log message
$this->di['logger']->info('Log message to log file');
@@ -40,11 +40,11 @@ class Client extends \Api_Abstract
$type = $this->di['array_get']($data, 'type', 'info');
return array(
'data' => $data,
'version' => $systemService->getVersion(),
'profile' => $clientService->toApiArray($this->di['loggedin_client']),
'messages' => $systemService->getMessages($type)
);
return [
'data' => $data,
'version' => $systemService->getVersion(),
'profile' => $clientService->toApiArray($this->di['loggedin_client']),
'messages' => $systemService->getMessages($type),
];
}
}
}
+12 -11
View File
@@ -1,6 +1,6 @@
<?php
/**
* BoxBilling
* BoxBilling.
*
* @copyright BoxBilling, Inc (https://www.boxbilling.org)
* @license Apache-2.0
@@ -12,21 +12,21 @@
/**
* All public methods in this class are exposed to public. Always think
* what kind of information you are exposing. Emails, passwords and other
* information should NOT be returned by functions in this class
*
* what kind of information you are exposing. Emails, passwords and other
* information should NOT be returned by functions in this class.
*
* This module can be called from API or in template
*
*/
// Change "Example" with your module's name
namespace Box\Mod\Example\Api;
class Guest extends \Api_Abstract
{
/**
* Return extension README.
*
*
* @return string
*/
public function readme($data)
@@ -36,17 +36,18 @@ class Guest extends \Api_Abstract
// Our example admin and client area pages will use this function to fetch the README data
// Then, we'll tell Twig to parse and display the markdown output
$readme = $this->di['tools']->file_get_contents(BB_PATH_MODS . '/Example/README.md');
$readme = $this->di['tools']->file_get_contents(BB_PATH_MODS.'/Example/README.md');
return $readme;
}
/**
* Return a random number between 1 and 100
*
* Return a random number between 1 and 100.
*
* @return int
*/
public function random_number()
{
return random_int(1, 100);
}
}
}
+33 -33
View File
@@ -1,6 +1,6 @@
<?php
/**
* BoxBilling
* BoxBilling.
*
* @copyright BoxBilling, Inc (https://www.boxbilling.org)
* @license Apache-2.0
@@ -12,7 +12,7 @@
/**
* This file connects BoxBilling admin area interface and API
* Class does not extend any other class
* Class does not extend any other class.
*/
namespace Box\Mod\Example\Controller;
@@ -40,53 +40,52 @@ class Admin implements \Box\InjectionAwareInterface
/**
* This method registers menu items in admin area navigation block
* This navigation is cached in bb-data/cache/{hash}. To see changes please
* remove the file
*
* remove the file.
*
* @return array
*/
public function fetchNavigation()
{
return array(
'group' => array(
'index' => 1500, // menu sort order
'location' => 'example', // menu group identificator for subitems
'label' => 'Example module', // menu group title
'class' => 'example', // used for css styling menu item
),
'subpages'=> array(
array(
'location' => 'example', // place this module in extensions group
'label' => 'Example module submenu',
'index' => 1500,
'uri' => $this->di['url']->adminLink('example'),
'class' => '',
),
),
);
return [
'group' => [
'index' => 1500, // menu sort order
'location' => 'example', // menu group identificator for subitems
'label' => 'Example module', // menu group title
'class' => 'example', // used for css styling menu item
],
'subpages' => [
[
'location' => 'example', // place this module in extensions group
'label' => 'Example module submenu',
'index' => 1500,
'uri' => $this->di['url']->adminLink('example'),
'class' => '',
],
],
];
}
/**
* Methods maps admin areas urls to corresponding methods
* Always use your module prefix to avoid conflicts with other modules
* in future
*
* in future.
*
* @example $app->get('/example/test', 'get_test', null, get_class($this)); // calls get_test method on this class
* @example $app->get('/example/:id', 'get_index', array('id'=>'[0-9]+'), get_class($this));
* @param \Box_App $app
*/
public function register(\Box_App &$app)
{
$app->get('/example', 'get_index', array(), get_class($this));
$app->get('/example/test', 'get_test', array(), get_class($this));
$app->get('/example/user/:id', 'get_user', array('id'=>'[0-9]+'), get_class($this));
$app->get('/example/api', 'get_api', array(), get_class($this));
$app->get('/example', 'get_index', [], get_class($this));
$app->get('/example/test', 'get_test', [], get_class($this));
$app->get('/example/user/:id', 'get_user', ['id' => '[0-9]+'], get_class($this));
$app->get('/example/api', 'get_api', [], get_class($this));
}
public function get_index(\Box_App $app)
{
// always call this method to validate if admin is logged in
$this->di['is_admin_logged'];
return $app->render('mod_example_index');
}
@@ -95,7 +94,7 @@ class Admin implements \Box\InjectionAwareInterface
// always call this method to validate if admin is logged in
$this->di['is_admin_logged'];
$params = array();
$params = [];
$params['youparamname'] = 'yourparamvalue';
return $app->render('mod_example_index', $params);
@@ -106,21 +105,22 @@ class Admin implements \Box\InjectionAwareInterface
// always call this method to validate if admin is logged in
$this->di['is_admin_logged'];
$params = array();
$params = [];
$params['userid'] = $id;
return $app->render('mod_example_index', $params);
}
public function get_api(\Box_App $app, $id = null)
{
// always call this method to validate if admin is logged in
$api = $this->di['api_admin'];
$list_from_controller = $api->example_get_something();
$params = array();
$params = [];
$params['api_example'] = true;
$params['list_from_controller'] = $list_from_controller;
return $app->render('mod_example_index', $params);
}
}
}
+8 -7
View File
@@ -1,6 +1,6 @@
<?php
/**
* BoxBilling
* BoxBilling.
*
* @copyright BoxBilling, Inc (https://www.boxbilling.org)
* @license Apache-2.0
@@ -12,7 +12,7 @@
/**
* This file connects BoxBilling client area interface and API
* Class does not extend any other class
* Class does not extend any other class.
*/
namespace Box\Mod\Example\Controller;
@@ -40,14 +40,14 @@ class Client implements \Box\InjectionAwareInterface
/**
* Methods maps client areas urls to corresponding methods
* Always use your module prefix to avoid conflicts with other modules
* in future
* in future.
*
* @param \Box_App $app - returned by reference
*/
public function register(\Box_App &$app)
{
$app->get('/example', 'get_index', array(), get_class($this));
$app->get('/example/protected', 'get_protected', array(), get_class($this));
$app->get('/example', 'get_index', [], get_class($this));
$app->get('/example/protected', 'get_protected', [], get_class($this));
}
public function get_index(\Box_App $app)
@@ -59,6 +59,7 @@ class Client implements \Box\InjectionAwareInterface
{
// call $this->di['is_client_logged'] method to validate if client is logged in
$this->di['is_client_logged'];
return $app->render('mod_example_index', array('show_protected'=>true));
return $app->render('mod_example_index', ['show_protected' => true]);
}
}
}
+52 -51
View File
@@ -1,6 +1,6 @@
<?php
/**
* BoxBilling
* BoxBilling.
*
* @copyright BoxBilling, Inc (https://www.boxbilling.org)
* @license Apache-2.0
@@ -11,11 +11,10 @@
*/
/**
* This file is a delegate for module. Class does not extend any other class
* This file is a delegate for module. Class does not extend any other class.
*
* All methods provided in this example are optional, but function names are
* still reserved.
*
*/
namespace Box\Mod\Example;
@@ -40,15 +39,16 @@ class Service
* database table might be enough.
*
* @return bool
*
* @throws \Box_Exception
*/
public function install()
{
// execute sql script if needed
$db = $this->di['db'];
$db->exec("SELECT NOW()");
$db->exec('SELECT NOW()');
//throw new \Box_Exception("Throw exception to terminate module installation process with a message", array(), 123);
// throw new \Box_Exception("Throw exception to terminate module installation process with a message", array(), 123);
return true;
}
@@ -56,11 +56,12 @@ class Service
* Method to uninstall module.
*
* @return bool
*
* @throws \Box_Exception
*/
public function uninstall()
{
//throw new \Box_Exception("Throw exception to terminate module uninstallation process with a message", array(), 124);
// throw new \Box_Exception("Throw exception to terminate module uninstallation process with a message", array(), 124);
return true;
}
@@ -70,46 +71,50 @@ class Service
* after new files are placed.
*
* @param array $manifest - information about new module version
*
* @return bool
*
* @throws \Box_Exception
*/
public function update($manifest)
{
//throw new \Box_Exception("Throw exception to terminate module update process with a message", array(), 125);
// throw new \Box_Exception("Throw exception to terminate module update process with a message", array(), 125);
return true;
}
/**
* Method is used to create search query for paginated list.
* Usually there is one paginated list per module
* Usually there is one paginated list per module.
*
* @param array $data
*
* @return array() = list of 2 parameters: array($sql, $params)
*/
public function getSearchQuery($data)
{
$params = array();
$sql="SELECT meta_key, meta_value
$params = [];
$sql = "SELECT meta_key, meta_value
FROM extension_meta
WHERE extension = 'example' ";
$client_id = $this->di['array_get']($data, 'client_id', NULL);
$client_id = $this->di['array_get']($data, 'client_id', null);
if(NULL !== $client_id) {
if (null !== $client_id) {
$sql .= ' AND client_id = :client_id';
$params[':client_id'] = $client_id;
}
$sql .= ' ORDER BY created_at DESC';
return array($sql, $params);
return [$sql, $params];
}
/**
* Methods is a delegate for one database row.
*
* @param array $row - array representing one database row
* @param array $row - array representing one database row
* @param string $role - guest|client|admin who is calling this method
* @param bool $deep - true|false deep or light version of result to return to API
* @param bool $deep - true|false deep or light version of result to return to API
*
* @return array
*/
@@ -119,7 +124,7 @@ class Service
}
/**
* Example event hook. Any module can hook to any BoxBilling event and perform actions
* Example event hook. Any module can hook to any BoxBilling event and perform actions.
*
* Make sure extension is enabled before testing this event.
*
@@ -130,44 +135,43 @@ class Service
* In this example we are going to count how many times client failed
* to enter correct login details
*
* @param \Box_Event $event
* @return void
*
* @throws \Box_Exception
* @throws \Box_Exception
*/
public static function onEventClientLoginFailed(\Box_Event $event)
{
//getting Dependency Injector
// getting Dependency Injector
$di = $event->getDi();
//@note almost in all cases you will need Admin API
// @note almost in all cases you will need Admin API
$api = $di['api_admin'];
//sometimes you may need guest API
//$api_guest = $di['api_guest'];
// sometimes you may need guest API
// $api_guest = $di['api_guest'];
$params = $event->getParameters();
//@note To debug parameters by throwing an exception
//throw new Exception(print_r($params, 1));
// @note To debug parameters by throwing an exception
// throw new Exception(print_r($params, 1));
// Use RedBean ORM in any place of BoxBilling where API call is not enough
// First we need to find if we already have a counter for this IP
// We will use extension_meta table to store this data.
$values = array(
'ext' => 'example',
'rel_type' => 'ip',
'rel_id' => $params['ip'],
'meta_key' => 'counter',
);
$values = [
'ext' => 'example',
'rel_type' => 'ip',
'rel_id' => $params['ip'],
'meta_key' => 'counter',
];
$meta = $di['db']->findOne('extension_meta', 'extension = :ext AND rel_type = :rel_type AND rel_id = :rel_id AND meta_key = :meta_key', $values);
if(!$meta) {
if (!$meta) {
$meta = $di['db']->dispense('extension_meta');
//$count->client_id = null; // client id is not known in this situation
$meta->extension = 'mod_example';
$meta->rel_type = 'ip';
$meta->rel_id = $params['ip'];
$meta->meta_key = 'counter';
// $count->client_id = null; // client id is not known in this situation
$meta->extension = 'mod_example';
$meta->rel_type = 'ip';
$meta->rel_id = $params['ip'];
$meta->meta_key = 'counter';
$meta->created_at = date('Y-m-d H:i:s');
}
$meta->meta_value = $meta->meta_value + 1;
@@ -177,30 +181,29 @@ class Service
// Now we can perform task depending on how many times wrong details were entered
// We can log event if it repeats for 2 time
if($meta->meta_value > 2) {
$api->activity_log(array('m' => 'Client failed to enter correct login details ' . $meta->meta_value . ' time(s)'));
if ($meta->meta_value > 2) {
$api->activity_log(['m' => 'Client failed to enter correct login details '.$meta->meta_value.' time(s)']);
}
// if client gets funky, we block him
if($meta->meta_value > 30) {
if ($meta->meta_value > 30) {
throw new \Box_Exception('You have failed to login too many times. Contact support.');
}
}
/**
* This event hook is registered in example module client API call
* @param \Box_Event $event
* This event hook is registered in example module client API call.
*/
public static function onAfterClientCalledExampleModule(\Box_Event $event)
{
//error_log('Called event from example module');
// error_log('Called event from example module');
$di = $event->getDi();
$params = $event->getParameters();
$meta = $di['db']->dispense('extension_meta');
$meta->extension = 'mod_example';
$meta->meta_key = 'event_params';
$meta = $di['db']->dispense('extension_meta');
$meta->extension = 'mod_example';
$meta->meta_key = 'event_params';
$meta->meta_value = json_encode($params);
$meta->created_at = date('Y-m-d H:i:s');
$meta->updated_at = date('Y-m-d H:i:s');
@@ -208,12 +211,11 @@ class Service
}
/**
* Example event hook for public ticket and set event return value
* @param \Box_Event $event
* Example event hook for public ticket and set event return value.
*/
public static function onBeforeGuestPublicTicketOpen(\Box_Event $event)
{
/* Uncomment lines below in order to see this function in action */
/* Uncomment lines below in order to see this function in action */
/*
$data = $event->getParameters();
@@ -225,8 +227,7 @@ class Service
}
/**
* Example email sending
* @param \Box_Event $event
* Example email sending.
*/
public static function onAfterClientOrderCreate(\Box_Event $event)
{
@@ -248,4 +249,4 @@ class Service
$api->email_template_send($email);
*/
}
}
}