PHP code example of brightnucleus / dependencies

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

    

brightnucleus / dependencies example snippets




// Configure all your styles and scripts here.
$dependencies_config = [
	'styles'   => [ <individual style dependencies go here> ],
	'scripts'  => [ <individual script dependencies go here> ],
	'handlers' => [
		'scripts' => 'BrightNucleus\Dependency\ScriptHandler',
		'styles'  => 'BrightNucleus\Dependency\StyleHandler',
	],
];

// Pass the Config to the 'DependencyManager', with a vendor/package prefix.
return 'BrightNucleus' => [
	'Example' => [
		'DependencyManager' => $dependencies_config,
	],
];


namespace BrightNucleus\Example;

use BrightNucleus\Config\ConfigInterface;
use BrightNucleus\Config\ConfigTrait;
use BrightNucleus\Dependency\DependencyManager;

class ExamplePlugin {

	use ConfigTrait;

	/**
	 * Instantiate a Plugin object.
	 *
	 * @param ConfigInterface $config Config to parametrize the object.
	 */
	public function __construct( ConfigInterface $config ) {
		$this->processConfig( $config );
	}

	/**
	 * Launch the initialization process.
	 */
	public function run() {
		add_action( 'plugins_loaded', [ $this, 'init_dependencies' ] );
	}

	/**
	 * Initialize DependencyManager and hook it up to WordPress.
	 */
	public function init_dependencies() {

		// Initialize dependencies.
		$dependencies = new DependencyManager(
			$this->config->getSubConfig( 'DependencyManager' )
		);
		// Register dependencies.
		add_action( 'init', [ $dependencies, 'register' ] );
	}
}


$dependencies_config = [
	'script' => [
		'handle' => 'test_script',
		'is_needed' => function ( $context ) {
			return array_key_exists( 'page_template', $context )
			&& 'test-template' === $context['page_template'];
		},
	],
];


$dependencies_config = [
	'scripts' => [
		[
			'handle'    => 'bn-example-script-handle',
			'src'       => BN_EXAMPLE_PLUGIN_DIR . 'js/bn-example-script.js',
			'deps'      => [ 'jquery' ],
			'ver'       => '1.2.1',
			'in_footer' => true,
			'localize'  => [
				'name' => 'BNExampleData',
				'data' => function( $context ) {
					return [
						'ajaxurl' => admin_url( 'admin-ajax.php' ),
						'context' => $context,
					];
				},
			],
		],
	],
];


$dependencies_config = [
	'scripts' => [
		[
			'handle'     => 'bn-example-script-handle',
			'src'        => BN_EXAMPLE_PLUGIN_DIR . 'js/bn-example-script.js',
			'deps'       => [ 'jquery' ],
			'ver'        => '1.2.1',
			'in_footer'  => true,
			'add_inline' => 'window.initialite_my_script();',
		],
	],
];


/**
 * Register all dependencies.
 *
 * @param mixed $context Optional. The context to pass to the dependencies.
 */
public function register( $context = null );


class ExamplePlugin {

	// <...>

	/** @var BrightNucleus\Dependency\DependencyManager $dependencies */
	protected $dependencies;

	/**
	 * Initialize DependencyManager and hook it up to WordPress.
	 */
	public function init_dependencies() {

		// Initialize dependencies.
		$this->dependencies = new DependencyManager( $this->config );
		// Register dependencies.
		add_action( 'init', [ $this, 'register_with_context' ], 20 );
	}

	/**
	 * Register dependencies and pass collected context into them.
	 */
	public function register_with_context() {
		$context['example_key'] = 'example_value';
		$this->dependencies->register( $context );
	}
}


/**
 * Enqueue a single dependency retrieved by its handle.
 *
 * @param string $handle   The dependency handle to enqueue.
 * @param mixed  $context  Optional. The context to pass to the
 *                         dependencies.
 * @param bool   $fallback Whether to fall back to dependencies registered
 *                         outside of DependencyManager. Defaults to false.
 * @return bool Returns whether the handle was found or not.
 */
public function enqueue_handle( $handle, $context = null, $fallback = false );