1. Go to this page and download the library: Download t-labs-co/config-walker library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
t-labs-co / config-walker example snippets
// Your Model App\Models\Category
class Category extends Model
{
use HasFactory;
use ConfigWalkable;
// omit the rest
// Export book category to config
public function walkable(): array
{
return self::query()
->whereStatus(CategoryStatus::Published)
->whereType(CategoryType::Book)
->get()
->keyBy('slug')
->map(function ($cat) {
return $cat->name;
})
->toArray();
}
public function walkerKey(): string
{
return "category_" . CategoryType::Book->value;
}
}
// Using TLabsCo\ConfigWalker\Facades\ConfigWalker to walk your model
ConfigWalker::walk(Category::class);
// Get the key config with Category
ConfigWalker::get('category_book');
// walk your config Category with Override key and under Tag
ConfigWalker::walk(Category::class, 'book1', 'categories');
// Combine your custom config under Tag
ConfigWalker::walk(['fantasy' => 'Fantasy'], 'book1', 'categories');
// Get config instance under tag categories
$categoriesConfig = ConfigWalker::tag('categories');
// Get custom config key
$categoriesConfig->get('book1');
// Get all
$categoriesConfig->all();
// Enum class
enum CategoryType: string
{
use EnumConfigWalkable;
case Post = 'post';
case Product = 'product';
case Book = 'book';
case Page = 'page';
}
// Using TLabsCo\ConfigWalker\Facades\ConfigWalker to walk your Enum with TLabsCo\ConfigWalker\EnumConfigWalkable
ConfigWalker::walk(CategoryType::class);
// Get data from enum CategoryType, by default the key will be dashed case from enum name and without namespace
// For here '\App\Enums\CategoryType' to 'category_type'
ConfigWalker::get('category_type');
// Get Facade TLabsCo\ConfigWalker\Facades\ConfigWalker
config_walker()
// Get config key
config_walker('your-key');
// Set data when input array
config_walker(['your-key' => 'your-value']);
// Load on fly or from your boot ServiceProvider to auto load from init
ConfigWalker::loadDefault();
// Setting in config-walker.php option
return [
'loadWithAppConfig' => true
]
// Using default laravel config via ConfigWalker
ConfigWalker::tag('default')->get('key');
// Or
ConfigWalker::makeDefault()->get('key');
// By default the Laravel will place under `default` group
// so to get key from config
config('key')
// equal to
config_walker('default.key')