PHP code example of webshr / core

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

    

webshr / core example snippets



// functions.php or your theme's bootstrap file

use Webshr\Core\Bootloader;

// Initialize the framework
$bootloader = Bootloader::get_instance();
$bootloader->boot();

use function Webshr\Core\app;

// Get the application instance
$app = app();

// Resolve services from the container
$encryption = app('encryption');
$assets = app('assets');

use Webshr\Core\Application;

$app = new Application();

// Bind services
$app->instance('my-service', new MyService());

// Resolve services
$service = $app->make('my-service');

// Register singletons
$app->singleton('database', Database::class);

use Webshr\Core\Module;

class MyModule extends Module
{
    public function register(): void
    {
        // Register WordPress hooks, services, etc.
        add_action('init', [$this, 'init']);
    }

    public function boot(): void
    {
        // Boot logic after registration
    }

    public function can_register(): bool
    {
        return true; // Conditional registration
    }
}

use Webshr\Core\asset;
use Webshr\Core\bundle;

// Get individual assets
$css = asset('styles/main.css');
$js = asset('scripts/app.js');

// Get asset bundles
$mainBundle = bundle('main');

// Enqueue CSS files
$mainBundle->css(function($handle, $src) {
    wp_enqueue_style($handle, $src);
});

// Enqueue JS files
$mainBundle->js(function($handle, $src, $deps) {
    wp_enqueue_script($handle, $src, $deps);
});

use Webshr\Core\encrypt;
use Webshr\Core\decrypt;

// Encrypt data
$encrypted = encrypt('sensitive data');

// Decrypt data
$decrypted = decrypt($encrypted);

// String-specific encryption (no serialization)
$encrypted = encrypt_string('plain text');
$decrypted = decrypt_string($encrypted);

use Webshr\Core\Support\Str;

// String operations
Str::contains('Hello World', 'World'); // true
Str::starts_with('Hello World', 'Hello'); // true
Str::ends_with('Hello World', 'World'); // true

// String extraction
Str::before('[email protected]', '@'); // 'user'
Str::after('[email protected]', '@'); // 'example.com'
Str::between_first('Hello [World]', '[', ']'); // 'World'

// Pattern matching
Str::is('admin/*', 'admin/users'); // true

// config/app.php
return [
    'paths' => [
        'base' => get_template_directory(),
        'config' => get_template_directory() . '/config',
        'resources' => get_template_directory() . '/resources',
        'storage' => get_template_directory() . '/storage',
    ],
    'modules' => [
        'theme' => ThemeModule::class,
        'assets' => AssetModule::class,
    ],
    'cipher' => 'aes-256-cbc',
    'key' => env('APP_KEY'),
];

// config/assets.php
return [
    'default' => 'main',
    'manifests' => [
        'main' => [
            'path' => get_template_directory() . '/dist',
            'url' => get_template_directory_uri() . '/dist',
            'assets' => get_template_directory() . '/dist/manifest.json',
            'bundles' => get_template_directory() . '/dist/bundles.json',
        ],
    ],
];

// Application helpers
app(); // Get application instance
bootloader(); // Get bootloader instance

// Asset helpers
asset('path/to/file.css');
bundle('main');
meta('path/to/file.json');

// Module helpers
module('theme');

// Security helpers
encrypt($data);
decrypt($payload);
hash($data);
check($data, $hash);

// WordPress helpers
add_actions(['init', 'wp_enqueue_scripts'], $callback);
add_filters(['the_content', 'the_title'], $callback);