|
| 1 | +# Custom Loaders |
| 2 | + |
| 3 | +- [Introduction](#introduction) |
| 4 | +- [Creating a Loader](#creating-a-loader) |
| 5 | + - [Constructor](#constructor) |
| 6 | + - [Name](#name) |
| 7 | + - [Version](#version) |
| 8 | +- [Use the Loader](#use-the-loader) |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +TweakPHP supports custom loaders. You can create your own loader to boot up your application before tweaking the code. |
| 13 | + |
| 14 | +## Creating a Loader |
| 15 | + |
| 16 | +To create a loader, Go to the settings and click on the `Loaders` tab. Then add a new loader. |
| 17 | + |
| 18 | +A loader is a PHP class in `TweakPHP\Client\Loaders` namespace that extends `TweakPHP\Client\Loaders\BaseLoader` class. |
| 19 | + |
| 20 | +```php |
| 21 | +<?php |
| 22 | + |
| 23 | +namespace TweakPHP\Client\Loaders; |
| 24 | + |
| 25 | +class MyCustomLoader extends BaseLoader |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @param string $path is the root path of your project |
| 29 | + */ |
| 30 | + public function __construct(string $path) |
| 31 | + { |
| 32 | + // your custom loader logic here |
| 33 | + // for example: |
| 34 | + // require $path.'/vendor/autoload.php'; |
| 35 | + } |
| 36 | + |
| 37 | + public function name(): string |
| 38 | + { |
| 39 | + return 'Your app name'; |
| 40 | + } |
| 41 | + |
| 42 | + public function version(): string |
| 43 | + { |
| 44 | + return 'Your app version'; |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +### Constructor |
| 50 | + |
| 51 | +Booting your application up should be done in the constructor of the loader. |
| 52 | + |
| 53 | +You can also extend your loader from existing loaders [here](https://github.com/tweakphp/client/tree/main/src/Loaders). |
| 54 | + |
| 55 | +### Name |
| 56 | + |
| 57 | +The `name` method can return your application's or loader's name. This will be shown in the status bar of the editor at the bottom right. |
| 58 | + |
| 59 | +### Version |
| 60 | + |
| 61 | +The `version` method can return your application's version. This will be shown in the status bar of the editor at the bottom right. |
| 62 | + |
| 63 | +## Use the Loader |
| 64 | + |
| 65 | +After creating the loader, Navigate to your project and from top toolbar select the loader you created. When you select a loader, it will automatically update the status bar to show the name and version that you returned in the loader. |
| 66 | + |
| 67 | +If you run the code, the code inside the constructor of the loader will be executed first. |
0 commit comments