Sage: [Feature] Directives configuration file

Created on 28 Jul 2017  路  7Comments  路  Source: roots/sage

Submit a feature request or bug report


What is the current behavior?

Currently there's no convenient way and place to put custom directives (the @asset is created at the end of the app/setup.php)

What is the expected or desired behavior?

It would be good to have something like config/directives.php. See Example


Feature Request

Please provide use cases for changing the current behavior:

It's easier and clearer to define directives in a dedicated file.

Part of my current directives.php:

<?php

$getBladeArgs = function( $expression ) {
    return explode(', ', str_replace(['(', ')'], '', $expression));
};
$fnEndWhile = function () { return '<?php endwhile; wp_reset_query(); ?>'; };

return [
    /*
    |--------------------------------------------------------------------------
    | Directives
    |--------------------------------------------------------------------------
    |
    | Custom directives
    |
    */

    // Create @asset() Blade directive
    'asset' => function ( $asset ) {
        return "<?php echo " . __NAMESPACE__ . "\\asset_path({$asset}); ?>";
    },

    // Creates @mainquery Blade directive
     'mainquery' => function () {
        return '<?php while(have_posts()) : the_post(); ?>';
    },

    // Creates @mainquery Blade directive
    'endmainquery' => $fnEndWhile,

    // Creates @customquery(\WP_Query $queryObj) Blade directive
    'customquery' => function( $queryObj ) {
        $output = "<?php while ({$queryObj}->have_posts()) : ?>";
        $output .= "<?php {$queryObj}->the_post(); ?>";
        return $output;
    },

    // Creates @endcustomquery Blade directive
    'endcustomquery' => $fnEndWhile,

    // Create @shortcode($shortCodeString) Blade directive
    'shortcode' => function ( $shortcode ) {
        return "<?php echo do_shortcode({$shortcode}); ?>";
    },

    // Create @set($name, value) Blade directive
    'set' => function ( $expression ) {
        list($variable, $value) = $getBladeArgs($expression);

        // Ensure variable has no spaces or apostrophes
        $variable = trim(str_replace('\'', '', $variable));

        // Make sure that the variable starts with $
        if ( !starts_with($variable, '$') ) {
            $variable = '$' . $variable;
        }
        $value = trim($value);
        return "<?php {$variable} = {$value}; ?>";
    },
];

Other relevant information:

If wanted, I can make a pull request 馃槂

feature

Most helpful comment

An application container that can register ServiceProviders would be excellent. It'd be nice to be able to use the various Blade extensions as well as be able to depreciate my shitty blade-svg-sage plugin.

Although I suppose it'd still require renaming sage() to app(), no?

All 7 comments

Maybe at some point I'll create an application container and you can register a proper serviceprovider for things like this, similar to how things are done in Laravel. Until then i'd say to just create a new file if you want a bunch of directives and load that file (from functions.php or whatever) or toss your directives in setup.php if you feel so inclined.

An application container that can register ServiceProviders would be excellent. It'd be nice to be able to use the various Blade extensions as well as be able to depreciate my shitty blade-svg-sage plugin.

Although I suppose it'd still require renaming sage() to app(), no?

+5000 for app container.

Some of our sites have a lot of application code. We abstract as much as possible to private packages but at some point you have to wire up your dependencies and right now the amount of factory classes I have on this one project is just about killing me 馃槀 .

An app container would be amazing and if you're open to it it's something I would be down to take a stab at.

create an application container and you can register a proper serviceprovider

Huge 馃憤 too!
What is required to "convert" the current container into an app container?

@LeoColomb The easiest way would be to include the entire Laravel Framework. The Application container is actually in Illuminate/Foundation and cannot be installed via composer. It is all of the framework or nothing, and that would be more than what Sage really needs.

What Laravel Lumen is doing, is they cherry-picked some components out of Illuminate/Foundation and reworked them. This may be a good starting point for creating a custom Sage Application container, but it is still a lot of work.

@kaisermann @Log1x I have created an extension package for Sage 9 that has built-in Blade directives plus a config file where you can add your own directives. See my GitHub repo Sage Xpress. One special directive @menu is tied into a MenuServiceProvider that renders a menu based on a config file -- pretty trippy and clutter free.

There are other container options out there, such as PHP-DI. No need to go overkill with the Laravel dependencies in order to rework a couple things and wire up something like PHP-DI.

@samrap Sage is already dependent on Illuminate/Container. Any other dependencies are built on that. My Sage Xpress repo already shows how easy it can be done.

My intent for that repo is to extend Sage in its current state.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

williamscotholmes picture williamscotholmes  路  4Comments

eduardoarandah picture eduardoarandah  路  4Comments

collegeman picture collegeman  路  3Comments

jlariza picture jlariza  路  5Comments

swalkinshaw picture swalkinshaw  路  6Comments