PHP code example of themehybrid / hybrid-assets

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

    

themehybrid / hybrid-assets example snippets


$app->register( \Hybrid\Assets\AssetsServiceProvider::class );

use Hybrid\Assets\ParentTheme;

$this->app->singleton( 'my-theme/assets', static function ( $app ) {
    /** @var ParentTheme $theme */
    $theme = $app->make( ParentTheme::class );
    $theme->setAssetsDirectory( '/assets' );
    $theme->setManifestDirectory( '/assets' );

    return $theme;
} );

use Hybrid\Assets\Plugin as AssetsPlugin;

$this->app->singleton( 'my-plugin/assets', static function ( $app ) {
    /** @var AssetsPlugin $plugin */
    $plugin = $app->make( AssetsPlugin::class );
    $plugin->setPluginFile( MY_PLUGIN_FILE );

    // By default, plugin asset overrides are expected under `{theme}/public/my-plugin/...`.
    // Themes can override this slug to match their own asset structure, such as `dist/`,
    // `assets/`, or any other custom directory.
    $plugin->setOverrideAssetsDirectory(
        apply_filters( 'my-plugin/assets/override/path', '/public/my-plugin' )
    );

    return $plugin;
} );

$theme = app( 'my-theme/assets' );

$theme->url( '/js/app.js' );
$theme->path( '/js/app.js' );



namespace MyTheme\Facades;

use Hybrid\Core\Facades\Facade;

/**
 * @method static string url(string $file)
 * @method static string path(string $file)
 * @method static string assetUrl(string $file, bool $inherit)
 * @method static string assetPath(string $file, bool $inherit)
 * @method static \Hybrid\Assets\Asset asset(string $file, bool $inherit, string $overrideManifestDirectory = '')
 * @method static \Hybrid\Assets\Svg svg(string $file, bool $inherit = false)
 */
class Assets extends Facade {

    protected static function getFacadeAccessor() {
        return 'my-theme/assets';
    }

}

/** @var \Hybrid\Assets\Asset $asset */
$asset = Assets::asset( 'js/my-file.js' );

wp_enqueue_script(
    'my-file',
    $asset->url(),
    $asset->dependencies(), // read from js/app.asset.php, if present
    $asset->version(), // from .asset.php, mix-manifest.json, or a content hash
    true
);



namespace MyPlugin\Facades;

use Hybrid\Core\Facades\Facade;

/**
 * @see \Hybrid\Assets\Plugin
 *
 * @method static string url(string $file)
 * @method static string path(string $file)
 * @method static string assetUrl(string $file, bool $inherit)
 * @method static string assetPath(string $file, bool $inherit)
 * @method static \Hybrid\Assets\Asset asset(string $file, bool $inherit, string $overrideManifestDirectory = '')
 * @method static \Hybrid\Assets\Svg svg(string $file, bool $inherit = false)
 */
class Assets extends Facade {

    protected static function getFacadeAccessor() {
        return 'my-plugin/assets';
    }

}

/** @var \Hybrid\Assets\Asset $asset */
$asset = Assets::asset( 'js/my-file.js', true );

wp_register_script(
    'my-file',
    $asset->url(),
    $asset->dependencies(), // read from js/my-file.asset.php, if present
    $asset->version(),      // from .asset.php, mix-manifest.json, or a content hash
    true
);

// Checks the child theme first, then falls back to the parent theme itself.
Assets::asset( 'js/my-file.js', true );

// For a plugin: checks child theme, then parent theme (both under the
// plugin's override directory), before falling back to the plugin's own file.
Assets::asset( 'js/my-file.js', true );

$theme->setManifestDirectory( '/assets' );          // where mix-manifest.json lives
$theme->setManifestFileName( 'mix-manifest.json' );  // default, rarely needs changing

Assets::svg( 'images/icons/arrow.svg' )->display();   // echo
$markup = Assets::svg( 'images/icons/arrow.svg' )->render();  // return

Assets::svg( 'images/icons/arrow.svg', true );  // child theme first

Assets::svg( 'images/icons/arrow.svg' )->sanitize( false )->render();

asset( $file, inherit: true )
  │
  ├─ inherit? ─── walk inheritance chain (child theme → parent theme → plugin)
  │                 └─ first resolver where the file exists & is readable wins
  │
  └─ resolve( $file )
        └─ new Asset( resolver, file, path )
              ├─ url()          → resolver->url( $file )
              ├─ path()         → resolver->path( $file )
              ├─ dependencies() → from .asset.php, else []
              └─ version()      → .asset.php → mix-manifest.json → md5_file() hash