PHP code example of kynetcode / wpzylos-core

1. Go to this page and download the library: Download kynetcode/wpzylos-core 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/ */

    

kynetcode / wpzylos-core example snippets


use WPZylos\Framework\Core\Application;
use WPZylos\Framework\Core\PluginContext;

// Create plugin context
$context = PluginContext::create([
    'file'       => __FILE__,
    'slug'       => 'my-plugin',
    'prefix'     => 'myplugin_',
    'textDomain' => 'my-plugin',
    'version'    => '1.0.0',
    'namespace'  => 'MyPlugin',
]);

// Create and boot application
$app = new Application($context);
$app->register(new MyServiceProvider());
$app->boot();

$context = PluginContext::create([
    'file'       => __FILE__,
    'slug'       => 'my-plugin',
    'prefix'     => 'myplugin_',
    'textDomain' => 'my-plugin',
    'version'    => '1.0.0',
    'namespace'  => 'MyPlugin',
]);

// Identity
$context->slug();        // 'my-plugin'
$context->prefix();      // 'myplugin_'
$context->textDomain();  // 'my-plugin'
$context->version();     // '1.0.0'
$context->namespace();   // 'MyPlugin'

// Prefixed keys
$context->optionKey('setting');          // 'myplugin_setting'
$context->hook('init');                  // 'myplugin_init'
$context->transientKey('cache');         // 'myplugin_cache'
$context->cronHook('daily_sync');        // 'myplugin_daily_sync'
$context->metaKey('order_id');           // '_myplugin_order_id'
$context->assetHandle('admin-js');       // 'my-plugin-admin-js'
$context->tableName('orders');           // 'wp_myplugin_orders'

$app = new Application($context);

// Register service providers
$app->register(new DatabaseServiceProvider());
$app->register(new RoutingServiceProvider());

// Boot the application
$app->boot();

// Resolve services from the container
$service = $app->make(MyService::class);

// Check if a service is bound
$app->has(MyService::class); // true/false

use WPZylos\Framework\Core\ServiceProvider;
use WPZylos\Framework\Core\Contracts\ApplicationInterface;

class MyServiceProvider extends ServiceProvider
{
    public function register(ApplicationInterface $app): void
    {
        parent::register($app);

        $this->singleton(MyService::class, function () {
            return new MyService($this->context());
        });
    }

    public function boot(ApplicationInterface $app): void
    {
        // Called after all providers are registered
        $service = $this->make(MyService::class);
    }
}

$paths = $app->paths();

// Use built-in aliases
$paths->path('@views/welcome.php');     // /plugin/resources/views/welcome.php
$paths->url('@assets/css/app.css');     // https://.../resources/assets/css/app.css

// Register custom aliases (chainable)
$paths->alias('templates', 'resources/templates');

// Check existence
$paths->exists('@config/app.php');      // true/false

// Plugin uploads directory
$paths->uploads('invoices/1.pdf');      // .../wp-content/uploads/my-plugin/invoices/1.pdf

use WPZylos\Framework\Core\Cache\CacheRepository;

$cache = new CacheRepository($context);

// Object cache
$cache->put('key', 'value', 3600);
$cache->get('key');               // 'value'
$cache->forget('key');

// Remember pattern
$users = $cache->remember('active_users', 3600, function () {
    return get_users(['role' => 'subscriber']);
});

// Transient fallback (persistent across requests)
$cache->transientPut('license', $data, DAY_IN_SECONDS);
$cache->transientGet('license');
$cache->transientRemember('api_data', HOUR_IN_SECONDS, fn() => fetch_api());