-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c83516f
commit 066a7fd
Showing
12 changed files
with
495 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,36 @@ | ||
# codeigniter4-handlers | ||
Cross-module handler registration, for CodeIgniter 4 | ||
|
||
## Quick Start | ||
|
||
1. Install with Composer: `> composer require codenom/handlers` | ||
2. Search for and register handlers: `> php spark handlers:register` | ||
|
||
## Features | ||
|
||
The Handlers library allows for defining a config file and then registering any | ||
supported handlers across all namespaces. | ||
|
||
## Installation | ||
|
||
Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities | ||
and always be up-to-date: | ||
* `> composer require codenom/handlers` | ||
|
||
Or, install manually by downloading the source files and adding the directory to | ||
`app/Config/Autoload.php`. | ||
|
||
## Configuration (optional) | ||
|
||
The library's default behavior can be altered by extending its config file. Copy | ||
**bin/HandlersConfig.php** to **app/Config/** and follow the instructions | ||
in the comments. If no config file is found in **app/Config** the library will use its own. | ||
|
||
## Usage | ||
|
||
Run the following command to scan for any supported config files and register the defined handlers: | ||
|
||
`> php spark handlers:register` | ||
|
||
Handlers may be disabled using the `delete()` method from their corresponding Model. | ||
The `register` command may be rerun anytime to add new or update existing handlers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Config; | ||
|
||
/*** | ||
* | ||
* This file contains example values to alter default library behavior. | ||
* Recommended usage: | ||
* 1. Copy the file to app/Config/Handlers.php | ||
* 2. Change any values | ||
* 3. Remove any lines to fallback to defaults | ||
* | ||
***/ | ||
|
||
class HandlersConfig extends \Codenom\Handlers\Config\HandlersConfig | ||
{ | ||
// whether to continue instead of throwing exceptions | ||
public $silent = true; | ||
|
||
// the session variable to check for a logged-in user ID | ||
public $userSource = 'logged_in'; | ||
|
||
// Config file in each namespace to check for supported handlers | ||
public $configFile = 'Handlers'; | ||
} | ||
|
||
/** End of bin/HandlersConfig.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "codenom/handlers", | ||
"description": "Cross-module handler registration, for CodeIgniter 4", | ||
"keywords": [ | ||
"codeigniter", | ||
"codeigniter4", | ||
"handlers", | ||
"adapters", | ||
"drivers", | ||
"modules" | ||
], | ||
"homepage": "https://github.com/codenomdev/codeigniter4-handlers", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Codenom Dev", | ||
"email": "dev@codenom.com", | ||
"homepage": "https://codenom.com", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.0" | ||
}, | ||
"require-dev": { | ||
"codeigniter4/framework": "dev-master" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Codenom\\Handlers\\": "src" | ||
} | ||
}, | ||
"scripts": { | ||
"post-update-cmd": [ | ||
"composer dump-autoload" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Codenom\Handlers\Commands; | ||
|
||
use CodeIgniter\CLI\BaseCommand; | ||
use CodeIgniter\CLI\CLI; | ||
|
||
class HandlersList extends BaseCommand | ||
{ | ||
protected $group = 'Handlers'; | ||
protected $name = 'handlers:list'; | ||
protected $description = 'List all supported handlers'; | ||
protected $usage = 'handlers:list'; | ||
protected $arguments = []; | ||
|
||
public function run(array $params = []) | ||
{ | ||
// Load the library | ||
$lib = service('handlers'); | ||
|
||
// Fetch all config classes to search | ||
$configs = $lib->findConfigs(); | ||
if (empty($configs)) : | ||
CLI::write('ERROR: No config files detected!', 'red'); | ||
return; | ||
endif; | ||
|
||
// Process each handler | ||
foreach ($configs as $configClas) : | ||
CLI::write($configClas, 'black', 'light_gray'); | ||
|
||
// Scan for supported handlers | ||
$handlers = $lib->findHandlers($configClas); | ||
if (empty($handlers)) : | ||
CLI::write('No handlers detected.', 'yellow'); | ||
continue; | ||
endif; | ||
|
||
// Display each handler | ||
foreach ($handlers as $handlerClass) : | ||
CLI::write($handlerClass); | ||
endforeach; | ||
endforeach; | ||
} | ||
} | ||
|
||
/** End of src/Commadns/HandlersList.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Codenom\Handlers\Commands; | ||
|
||
use CodeIgniter\CLI\BaseCommand; | ||
use CodeIgniter\CLI\CLI; | ||
|
||
class HandlersRegister extends BaseCommand | ||
{ | ||
protected $group = 'Handlers'; | ||
protected $name = 'handlers:register'; | ||
protected $description = 'Locate supported handlers and add them to the database'; | ||
protected $usage = 'handlers:register'; | ||
protected $arguments = []; | ||
|
||
public function run(array $params = []) | ||
{ | ||
// Load the library | ||
$lib = service('handlers'); | ||
|
||
// Fetch all supported configs | ||
$configs = $lib->findConfigs(); | ||
if (empty($configs)) : | ||
CLI::write('WARNING: No handler config files detected!', 'yellow'); | ||
return; | ||
endif; | ||
|
||
// Process each handler | ||
foreach ($configs as $configClass) : | ||
|
||
// Scan for supported handlers | ||
$handlers = $lib->findHandlers($configClass); | ||
if (empty($handlers)) : | ||
// Check for errors | ||
if ($errors = $lib->getErrors()) : | ||
foreach ($errors as $error) : | ||
CLI::write($error, 'red'); | ||
endforeach; | ||
else : | ||
CLI::write('No handlers detected for config file: ' . $configClass, 'yellow'); | ||
endif; | ||
|
||
continue; | ||
endif; | ||
|
||
// Get an instance of the model | ||
$config = new $configClass(); | ||
$model = new $config->model(); | ||
|
||
// Load each handler | ||
foreach ($handlers as $handlerClass) : | ||
|
||
// Get the attributes from the handler itself | ||
$handler = new $handlerClass(); | ||
$row = $handler->attributes; | ||
$row['class'] = $handlerClass; | ||
|
||
// Check for an existing registration | ||
if ($existing = $model->where('uid', $row['uid'])->first()) : | ||
// Update it | ||
$model->update($existing->id, $row); | ||
else : | ||
// Create a new registration | ||
$handlerId = $model->insert($row); | ||
CLI::write("New handler registered for {$configClass}: {$handlerClass}", 'green'); | ||
endif; | ||
|
||
endforeach; | ||
|
||
endforeach; | ||
|
||
$this->call('handlers:list'); | ||
} | ||
} | ||
|
||
/** End of src/Commands/HandlersRegister.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Codenom\Handlers\Config; | ||
|
||
use CodeIgniter\Config\BaseConfig; | ||
|
||
class HandlersConfig extends BaseConfig | ||
{ | ||
// Whether to continue instead of throwing exceptions | ||
public $silent = true; | ||
|
||
// Session variable to check for a logged-in user ID | ||
public $userSource = 'logged_in'; | ||
|
||
// Config file in each namespace to check for supported handlers | ||
public $configFile = 'Handlers'; | ||
} | ||
|
||
/** End of src/Config/HandlersConfig.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Codenom\Handlers\Config; | ||
|
||
use CodeIgniter\Config\BaseService; | ||
use CodeIgniter\Database\ConnectionInterface; | ||
|
||
class Services extends BaseService | ||
{ | ||
public static function handlers(BaseConfig $config = null, bool $getShared = true) | ||
{ | ||
if ($getShared) | ||
return static::getSharedInstance('handlers', $config); | ||
|
||
// If no config was injected then load one | ||
$config = $config ?? config('HandlersConfig'); | ||
return new \Codenom\Handlers\Handlers($config); | ||
} | ||
} | ||
|
||
/** End of src/Config/Services.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Codenom\Exports\Exceptions; | ||
|
||
use CodeIgniter\Exceptions\ExceptionInterface; | ||
use CodeIgniter\Exceptions\FrameworkException; | ||
|
||
class ExportsException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
public static function forLoadFail($file, $error) | ||
{ | ||
return new static(lang('Handlers.loadFail', [$file, $error])); | ||
} | ||
|
||
public static function forMissingClass($file, $class) | ||
{ | ||
return new static(lang('Handlers.missingClass', [$file, $class])); | ||
} | ||
|
||
public static function forInvalidFormat($class) | ||
{ | ||
return new static(lang('Handlers.invalidFormat', [$class])); | ||
} | ||
} | ||
|
||
/** End of file src/Exceptions/HandlersException.php */ |
Oops, something went wrong.