Log1x / blade-svg-sage

A simple package to add support for Blade SVG by Adam Wathan to Roots Sage.
MIT License
72 stars 13 forks source link

Followed instructions, but also get "Call to undefined function app()" #17

Closed farmerpaul closed 5 years ago

farmerpaul commented 5 years ago

Similarly to the authors of #9 and #3, I too am getting Call to undefined function app() when I try to install your library and call @svg('some-svg') in my code. Here's the full message:

Fatal error:  Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined function app() in /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/nothingworks/blade-svg/src/helpers.php:15
Stack trace:
#0 /srv/www/bgr/htdocs/wp-content/uploads/cache/f55cf797dc38eb655f771426241b544eed5f52f2.php(9): svg_image('logo')
#1 /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/illuminate/view/Engines/PhpEngine.php(43): include('/srv/www/bgr/ht...')
#2 /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/illuminate/view/Engines/CompilerEngine.php(59): Illuminate\View\Engines\PhpEngine->evaluatePath('/srv/www/bgr/ht...', Array)
#3 /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/illuminate/view/View.php(142): Illuminate\View\Engines\CompilerEngine->get('/srv/www/bgr/ht...', Array)
#4 /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/illuminate/view/View.php(125): Illuminate\View\View->getContents()
#5 /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/illuminate/view/View.php(90): Illuminate\View\View->renderContents()
#6 /srv in /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/nothingworks/blade-svg/src/helpers.php on line 15

I have PHP 7.2, Sage 9.0.5, and Composer 1.7.3. As instructed, I ran composer require log1x/blade-svg-sage, and it installed version 0.3.0 of nothingworks/blade-svg and 2.0.0 of log2x/blade-svg-sage:

$ composer require log1x/blade-svg-sage
Using version ^2.0 for log1x/blade-svg-sage
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
  - Installing nothingworks/blade-svg (v0.3.0): Downloading (100%)         
  - Installing log1x/blade-svg-sage (v2.0.0): Downloading (100%)         
Writing lock file
Generating autoload files

I've even installed the blade-generate WP-CLI tool to make sure the Blade cache was clear and tried recompiling, but it insists on generating code that tries to call the undefined function app().

Any idea what I've done wrong?

farmerpaul commented 5 years ago

Well, I have no clue how Composer's loader is supposed to work, but for whatever reason, when I install your package following your own instructions, the app prefers the svg_image() function defined in nothingworks/blade-svg/src/helpers.php instead of your own log1x/blade-svg-sage/src/helpers.php. Since I'm clueless for how to get Composer to give your function priority instead, my clumsy workaround was to edit nothingworks/blade-svg/src/helpers.php and essentially replace the function body of svg_image() with your version, making a few tweaks:

…

if (! function_exists('svg_image')) {
    function svg_image($name, $class = '', $attrs = [])
    {
        if (empty($name)) {
            return;
        }

        // Add `App\` and `BladeSvgSage\` below to reference the other namespaces properly.
        return App\sage(BladeSvgSage\BladeSvgSage::class)->svg($name, $class, $attrs);
    }
}

…

Hopefully this helps someone else who's trying to get this package to work.

Log1x commented 5 years ago

Working on a fix for this now. For some reason, I didn't run into this issue during initial testing prior to release.

Log1x commented 5 years ago

Should be fixed now in the v2.0.1 release.

farmerpaul commented 5 years ago

Thanks for your quick response, however I'm afraid this hasn't fixed the issue for me:

$ composer remove log1x/blade-svg-sage
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 0 installs, 0 updates, 2 removals
  - Removing nothingworks/blade-svg (v0.3.0)
  - Removing log1x/blade-svg-sage (v2.0.0)
Writing lock file
Generating autoload files
$ composer require log1x/blade-svg-sage
Using version ^2.0 for log1x/blade-svg-sage
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
  - Installing nothingworks/blade-svg (v0.3.0): Loading from cache
  - Installing log1x/blade-svg-sage (v2.0.1): Loading from cache
Writing lock file
Generating autoload files

Reloading my page (which had been working fine with aforementioned hack) again reproduces that same error message:

Fatal error:  Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined function app() in /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/nothingworks/blade-svg/src/helpers.php:15
Stack trace:
#0 /srv/www/bgr/htdocs/wp-content/uploads/cache/7d3bd42e63db143eb70e0949ad11f6f15a6d4e1f.php(5): svg_image('bg-heading-1', 'bg-abs color-re...')
#1 /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/illuminate/view/Engines/PhpEngine.php(43): include('/srv/www/bgr/ht...')
#2 /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/illuminate/view/Engines/CompilerEngine.php(59): Illuminate\View\Engines\PhpEngine->evaluatePath('/srv/www/bgr/ht...', Array)
#3 /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/illuminate/view/View.php(142): Illuminate\View\Engines\CompilerEngine->get('/srv/www/bgr/ht...', Array)
#4 /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/illuminate/view/View.php(125): Illuminate\View\View->getContents()
#5 /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/illuminate/view/View.php(90): Illuminate\View\ in /srv/www/bgr/htdocs/wp-content/themes/bgr/vendor/nothingworks/blade-svg/src/helpers.php on line 15

I've reapplied my hack in order to continue development of my project. Happy to test again later.

Log1x commented 5 years ago

Are you calling the function directly or with the Blade directive?

farmerpaul commented 5 years ago

Just using the Blade directive. For example: @svg('logo')

Log1x commented 5 years ago

Can you make sure the Blade cache is cleared? I see in your stack trace it's calling svg_image('bg-heading-1', 'bg-abs color-re...') but it should now call App/svg_image('bg-heading-1', 'bg-abs color-re...')

farmerpaul commented 5 years ago

Good call, sorry about that. I was clearing my cache before but forgot to this time! Cheers, it works.

Log1x commented 5 years ago

Great! Thanks for testing. Let me know if you find any other issues.