Skip to content

Commit 066a7fd

Browse files
committed
Initial Release
1 parent c83516f commit 066a7fd

File tree

12 files changed

+495
-0
lines changed

12 files changed

+495
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
11
# codeigniter4-handlers
22
Cross-module handler registration, for CodeIgniter 4
3+
4+
## Quick Start
5+
6+
1. Install with Composer: `> composer require codenom/handlers`
7+
2. Search for and register handlers: `> php spark handlers:register`
8+
9+
## Features
10+
11+
The Handlers library allows for defining a config file and then registering any
12+
supported handlers across all namespaces.
13+
14+
## Installation
15+
16+
Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities
17+
and always be up-to-date:
18+
* `> composer require codenom/handlers`
19+
20+
Or, install manually by downloading the source files and adding the directory to
21+
`app/Config/Autoload.php`.
22+
23+
## Configuration (optional)
24+
25+
The library's default behavior can be altered by extending its config file. Copy
26+
**bin/HandlersConfig.php** to **app/Config/** and follow the instructions
27+
in the comments. If no config file is found in **app/Config** the library will use its own.
28+
29+
## Usage
30+
31+
Run the following command to scan for any supported config files and register the defined handlers:
32+
33+
`> php spark handlers:register`
34+
35+
Handlers may be disabled using the `delete()` method from their corresponding Model.
36+
The `register` command may be rerun anytime to add new or update existing handlers.

bin/HandlersConfig.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
/***
6+
*
7+
* This file contains example values to alter default library behavior.
8+
* Recommended usage:
9+
* 1. Copy the file to app/Config/Handlers.php
10+
* 2. Change any values
11+
* 3. Remove any lines to fallback to defaults
12+
*
13+
***/
14+
15+
class HandlersConfig extends \Codenom\Handlers\Config\HandlersConfig
16+
{
17+
// whether to continue instead of throwing exceptions
18+
public $silent = true;
19+
20+
// the session variable to check for a logged-in user ID
21+
public $userSource = 'logged_in';
22+
23+
// Config file in each namespace to check for supported handlers
24+
public $configFile = 'Handlers';
25+
}
26+
27+
/** End of bin/HandlersConfig.php */

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "codenom/handlers",
3+
"description": "Cross-module handler registration, for CodeIgniter 4",
4+
"keywords": [
5+
"codeigniter",
6+
"codeigniter4",
7+
"handlers",
8+
"adapters",
9+
"drivers",
10+
"modules"
11+
],
12+
"homepage": "https://github.com/codenomdev/codeigniter4-handlers",
13+
"license": "MIT",
14+
"authors": [
15+
{
16+
"name": "Codenom Dev",
17+
"email": "dev@codenom.com",
18+
"homepage": "https://codenom.com",
19+
"role": "Developer"
20+
}
21+
],
22+
"require": {
23+
"php": "^7.0"
24+
},
25+
"require-dev": {
26+
"codeigniter4/framework": "dev-master"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"Codenom\\Handlers\\": "src"
31+
}
32+
},
33+
"scripts": {
34+
"post-update-cmd": [
35+
"composer dump-autoload"
36+
]
37+
}
38+
}

src/Commands/HandlersList.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Codenom\Handlers\Commands;
4+
5+
use CodeIgniter\CLI\BaseCommand;
6+
use CodeIgniter\CLI\CLI;
7+
8+
class HandlersList extends BaseCommand
9+
{
10+
protected $group = 'Handlers';
11+
protected $name = 'handlers:list';
12+
protected $description = 'List all supported handlers';
13+
protected $usage = 'handlers:list';
14+
protected $arguments = [];
15+
16+
public function run(array $params = [])
17+
{
18+
// Load the library
19+
$lib = service('handlers');
20+
21+
// Fetch all config classes to search
22+
$configs = $lib->findConfigs();
23+
if (empty($configs)) :
24+
CLI::write('ERROR: No config files detected!', 'red');
25+
return;
26+
endif;
27+
28+
// Process each handler
29+
foreach ($configs as $configClas) :
30+
CLI::write($configClas, 'black', 'light_gray');
31+
32+
// Scan for supported handlers
33+
$handlers = $lib->findHandlers($configClas);
34+
if (empty($handlers)) :
35+
CLI::write('No handlers detected.', 'yellow');
36+
continue;
37+
endif;
38+
39+
// Display each handler
40+
foreach ($handlers as $handlerClass) :
41+
CLI::write($handlerClass);
42+
endforeach;
43+
endforeach;
44+
}
45+
}
46+
47+
/** End of src/Commadns/HandlersList.php */

src/Commands/HandlersRegister.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Codenom\Handlers\Commands;
4+
5+
use CodeIgniter\CLI\BaseCommand;
6+
use CodeIgniter\CLI\CLI;
7+
8+
class HandlersRegister extends BaseCommand
9+
{
10+
protected $group = 'Handlers';
11+
protected $name = 'handlers:register';
12+
protected $description = 'Locate supported handlers and add them to the database';
13+
protected $usage = 'handlers:register';
14+
protected $arguments = [];
15+
16+
public function run(array $params = [])
17+
{
18+
// Load the library
19+
$lib = service('handlers');
20+
21+
// Fetch all supported configs
22+
$configs = $lib->findConfigs();
23+
if (empty($configs)) :
24+
CLI::write('WARNING: No handler config files detected!', 'yellow');
25+
return;
26+
endif;
27+
28+
// Process each handler
29+
foreach ($configs as $configClass) :
30+
31+
// Scan for supported handlers
32+
$handlers = $lib->findHandlers($configClass);
33+
if (empty($handlers)) :
34+
// Check for errors
35+
if ($errors = $lib->getErrors()) :
36+
foreach ($errors as $error) :
37+
CLI::write($error, 'red');
38+
endforeach;
39+
else :
40+
CLI::write('No handlers detected for config file: ' . $configClass, 'yellow');
41+
endif;
42+
43+
continue;
44+
endif;
45+
46+
// Get an instance of the model
47+
$config = new $configClass();
48+
$model = new $config->model();
49+
50+
// Load each handler
51+
foreach ($handlers as $handlerClass) :
52+
53+
// Get the attributes from the handler itself
54+
$handler = new $handlerClass();
55+
$row = $handler->attributes;
56+
$row['class'] = $handlerClass;
57+
58+
// Check for an existing registration
59+
if ($existing = $model->where('uid', $row['uid'])->first()) :
60+
// Update it
61+
$model->update($existing->id, $row);
62+
else :
63+
// Create a new registration
64+
$handlerId = $model->insert($row);
65+
CLI::write("New handler registered for {$configClass}: {$handlerClass}", 'green');
66+
endif;
67+
68+
endforeach;
69+
70+
endforeach;
71+
72+
$this->call('handlers:list');
73+
}
74+
}
75+
76+
/** End of src/Commands/HandlersRegister.php */

src/Config/HandlersConfig.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Codenom\Handlers\Config;
4+
5+
use CodeIgniter\Config\BaseConfig;
6+
7+
class HandlersConfig extends BaseConfig
8+
{
9+
// Whether to continue instead of throwing exceptions
10+
public $silent = true;
11+
12+
// Session variable to check for a logged-in user ID
13+
public $userSource = 'logged_in';
14+
15+
// Config file in each namespace to check for supported handlers
16+
public $configFile = 'Handlers';
17+
}
18+
19+
/** End of src/Config/HandlersConfig.php */

src/Config/Services.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Codenom\Handlers\Config;
4+
5+
use CodeIgniter\Config\BaseService;
6+
use CodeIgniter\Database\ConnectionInterface;
7+
8+
class Services extends BaseService
9+
{
10+
public static function handlers(BaseConfig $config = null, bool $getShared = true)
11+
{
12+
if ($getShared)
13+
return static::getSharedInstance('handlers', $config);
14+
15+
// If no config was injected then load one
16+
$config = $config ?? config('HandlersConfig');
17+
return new \Codenom\Handlers\Handlers($config);
18+
}
19+
}
20+
21+
/** End of src/Config/Services.php */

src/Exceptions/HandlersException.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Codenom\Exports\Exceptions;
4+
5+
use CodeIgniter\Exceptions\ExceptionInterface;
6+
use CodeIgniter\Exceptions\FrameworkException;
7+
8+
class ExportsException extends \RuntimeException implements ExceptionInterface
9+
{
10+
public static function forLoadFail($file, $error)
11+
{
12+
return new static(lang('Handlers.loadFail', [$file, $error]));
13+
}
14+
15+
public static function forMissingClass($file, $class)
16+
{
17+
return new static(lang('Handlers.missingClass', [$file, $class]));
18+
}
19+
20+
public static function forInvalidFormat($class)
21+
{
22+
return new static(lang('Handlers.invalidFormat', [$class]));
23+
}
24+
}
25+
26+
/** End of file src/Exceptions/HandlersException.php */

0 commit comments

Comments
 (0)