-
Notifications
You must be signed in to change notification settings - Fork 25
feat: add FileHandler implementation
#153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
68af8af
add support for FileHandler
michalsn 081bb2c
add publish command
michalsn 0de68b3
update flush method to work with context folders
michalsn b971015
update docs
michalsn d96fd5b
apply changes from the code review
michalsn c647cb4
apply docs changes from the review
michalsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,118 @@ | ||
| # Configuration | ||
|
|
||
| To make changes to the config file, you need to have your own copy in `app/Config/Settings.php`. The easiest way to do it is by using the publish command. | ||
|
|
||
| When you run: | ||
|
|
||
| php spark settings:publish | ||
|
|
||
| You will get your copy ready for modifications. | ||
|
|
||
| --- | ||
|
|
||
| ## Handlers | ||
|
|
||
| An array of handler aliases to use for storing and retrieving settings. Handlers are checked in order, with the first handler that has a value returning it. | ||
|
|
||
| **Type:** `array` | ||
|
|
||
| **Default:** `['database']` | ||
|
|
||
| **Available handlers:** `database`, `file`, `array` | ||
|
|
||
| Example: | ||
|
|
||
| ```php | ||
| public $handlers = ['database']; | ||
| ``` | ||
| ### Multiple handlers | ||
|
|
||
| When multiple handlers are configured, they are checked in the order specified in $handlers. The first handler that has a value for the requested setting will return it. | ||
|
|
||
| Example with fallback: | ||
|
|
||
| ```php | ||
| public $handlers = ['file', 'database']; | ||
| ``` | ||
| This configuration will: | ||
|
|
||
| 1. Check the file handler first | ||
| 2. If not found, check the database handler | ||
| 3. If not found in any handler, return the default value from the config file | ||
|
|
||
| ### Writeable Handlers | ||
|
|
||
| Only handlers marked as `writeable => true` will be used when calling `set()`, `forget()`, or `flush()` methods. | ||
|
|
||
| ## DatabaseHandler | ||
|
|
||
| This handler stores settings in a database table and is production-ready for high-traffic applications. | ||
|
|
||
| **Available options:** | ||
|
|
||
| * `class` - The handler class. Default: `DatabaseHandler::class` | ||
| * `table` - The database table name for storing settings. Default: `'settings'` | ||
| * `group` - The database connection group to use. Default: `null` (uses default connection) | ||
| * `writeable` - Whether this handler supports write operations. Default: `true` | ||
|
|
||
| Example: | ||
|
|
||
| ```php | ||
| public $database = [ | ||
| 'class' => DatabaseHandler::class, | ||
| 'table' => 'settings', | ||
| 'group' => null, | ||
| 'writeable' => true, | ||
| ]; | ||
| ``` | ||
|
|
||
| !!! note | ||
| You need to run migrations to create the settings table: `php spark migrate -n CodeIgniter\\Settings` | ||
|
|
||
| --- | ||
|
|
||
| ## FileHandler | ||
|
|
||
| This handler stores settings as PHP files and is optimized for production use with built-in race condition protection. | ||
|
|
||
| **Available options:** | ||
|
|
||
| * `class` - The handler class. Default: `FileHandler::class` | ||
| * `path` - The directory path where settings files are stored. Default: `WRITEPATH . 'settings'` | ||
| * `writeable` - Whether this handler supports write operations. Default: `true` | ||
|
|
||
| Example: | ||
|
|
||
| ```php | ||
| public $file = [ | ||
| 'class' => FileHandler::class, | ||
| 'path' => WRITEPATH . 'settings', | ||
| 'writeable' => true, | ||
| ]; | ||
| ``` | ||
|
|
||
| !!! note | ||
| The `FileHandler` automatically creates the directory if it doesn't exist and checks write permissions on instantiation. | ||
|
|
||
| --- | ||
|
|
||
| ## ArrayHandler | ||
|
|
||
| This handler stores settings in memory only and is primarily useful for testing or as a parent class for other handlers. | ||
|
|
||
| **Available options:** | ||
|
|
||
| * `class` - The handler class. Default: `ArrayHandler::class` | ||
| * `writeable` - Whether this handler supports write operations. Default: `true` | ||
|
|
||
| Example: | ||
|
|
||
| ```php | ||
| public $array = [ | ||
| 'class' => ArrayHandler::class, | ||
| 'writeable' => true, | ||
| ]; | ||
| ``` | ||
|
|
||
| !!! note | ||
| `ArrayHandler` does not persist data between requests. It's mainly used for testing or extended by other handlers. | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,53 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter Queue. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Settings\Commands; | ||
|
|
||
| use CodeIgniter\CLI\BaseCommand; | ||
| use CodeIgniter\CLI\CLI; | ||
| use CodeIgniter\Publisher\Publisher; | ||
| use Throwable; | ||
|
|
||
| class PublishSettings extends BaseCommand | ||
| { | ||
| protected $group = 'Settings'; | ||
| protected $name = 'settings:publish'; | ||
| protected $description = 'Publish Settings config file into the current application.'; | ||
|
|
||
| public function run(array $params): void | ||
| { | ||
| $source = service('autoloader')->getNamespace('CodeIgniter\\Settings')[0]; | ||
|
|
||
| $publisher = new Publisher($source, APPPATH); | ||
|
|
||
| try { | ||
| $publisher->addPaths([ | ||
| 'Config/Settings.php', | ||
| ])->merge(false); | ||
| } catch (Throwable $e) { | ||
| $this->showError($e); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| foreach ($publisher->getPublished() as $file) { | ||
| $contents = file_get_contents($file); | ||
| $contents = str_replace('namespace CodeIgniter\\Settings\\Config', 'namespace Config', $contents); | ||
| $contents = str_replace('use CodeIgniter\\Config\\BaseConfig', 'use CodeIgniter\\Settings\\Config\\Settings as BaseSettings', $contents); | ||
| $contents = str_replace('class Settings extends BaseConfig', 'class Settings extends BaseSettings', $contents); | ||
| file_put_contents($file, $contents); | ||
| } | ||
|
|
||
| CLI::write(CLI::color(' Published! ', 'green') . 'You can customize the configuration by editing the "app/Config/Settings.php" file.'); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.