Pink-Crab / Perique-BladeOne-Provider

A BladeOne Provider for the PinkCrab Renderable Interface.
0 stars 0 forks source link

Helper function to setup DI rules #7

Closed gin0115 closed 3 years ago

gin0115 commented 3 years ago

Due to the way we use the general PHP_Engine as a default, it causes problems fully defining Blade to be more suitable for theme creation.

To get around this, the following will unset the php engine and fully configure blade for themes

( function( $template_path = null, ?string $compiled_path = null, int $mode = 0, ?callable $config = null ) {
    add_filter(
        Hooks::APP_INIT_SET_DI_RULES,
        function( $rules ) use ( $template_path, $compiled_path, $mode, $config ) {
            // Unset the global PHP_Engine useage.
            if ( array_key_exists( '*', $rules )
            && array_key_exists( 'substitutions', $rules['*'] )
            && array_key_exists( Renderable::class, $rules['*']['substitutions'] )
            && is_a( $rules['*']['substitutions'][ Renderable::class ], PHP_Engine::class ) ) {
                unset( $rules['*']['substitutions'][ Renderable::class ] );
            }

            // Setup BladeOne
            $rules[ BladeOne_Provider::class ] = array(
                'substitutions' => array(
                    BladeOne::class => array(
                        Dice::INSTANCE => function() use ( $template_path, $compiled_path, $mode, $config ) {
                            $blade = new BladeOne(
                                $template_path,
                                $compiled_path,
                                $mode
                            );

                            return is_callable( $config ) ? $config( $blade ) : $blade;
                        },
                    ),
                ),
                'call'          => array(
                    array( 'allow_pipe', array() ),
                ),
            );

            $rules[ Renderable::class ] = array(
                'instanceOf' => BladeOne_Provider::class,
            );

            $rules[ Abstract_BladeOne_Config::class ] = array(
                'call' => array(
                    array( 'set_renderable', array( array( Dice::INSTANCE => Renderable::class ) ) ),
                ),
            );
            return $rules;
        }
    );
} )(
    get_stylesheet_directory() . '/views',
    WP_CONTENT_DIR . '/blade-cache',
    BladeOne::MODE_DEBUG
);