Isolate external php code in Laravel - laravel

I need to integrate Slider Revolution editor into my Laravel (5.5) app.
I've put the editor in public/revslider/ folder to be able to use the visual editor. I also created a helper class to "communicate" with it and be able to use it inside my Blade views:
namespace App\Helpers;
include( public_path('revslider/embed.php') );
class Slider{
/**
* This function is called where you want to put your slider
*/
public static function make($slider){
return \RevSliderEmbedder::putRevSlider( $slider );
}
/**
* This function is called inside <HEAD> tag to include all
* SR assets (js/css/font files)
*/
public static function head(){
return \RevSliderEmbedder::headIncludes(false);
}
}
The SR's PHP code does not use namespaces. In fact it is a strange mix of Code Igniter, Wordpress and vanilla php.
The problem is it is trying to declare a translation function __(...):
if( ! function_exists('__'))
{
function __($string = '')
{
....
}
}
and since there is already such Laravel's helper function, it does not redeclare it and tries to use Laravel's __() function. And that obviously causes errors.
I temporarily managed to fix this problem by changing the name of SR's __() function (and all references to it). But of course it is not a best way to solve this problem, since I will be unable to use SR's automatic updates or will be forced to do these changes after every update.
So my questions are:
Is there any good way of integrating such "bad" code into your project, invoking it safely without conflicts? Is there any way of isolating such code and avoid clashes? By "bad code" I mean code that does not follow strict OOP/PSR rules present in projects like Laravel.
What is the best way to include "external" PHP code? I've just used plain include() inside of my helper class' file, but is there a better/cleaner way? Like, I don't know, loading it through composer?

Related

Laravel Bagisto create custom Core.php

I am trying to customize bagisto, and hit a wall when trying to customize Core.php file.
What I want to do is edit some of the functions inside the Core.php file. I am now editing the file directly, but this is definitely not optimal.
How am I supposed to override some of the functions inside the Core.php?
I couldn't find any complete procedure online and am new to Laravel, so I'm completely lost.
To be precise I want to override the currency function like this:
public function currency($amount = 0)
{
if (is_null($amount)) {
$amount = 0;
}
return intval($amount);
//return $this->formatPrice($this->convertPrice($amount), $this->getCurrentCurrency()->code);
}
Found it (I got helped).
The Core.php is called from Core/src/Http/helpers.php which creates the function core().
This function is then made public to all in package.lock under "autoload".
To customize the Core file you would have to create a custom Core file and helper file (that calls the custom Core file) then change the composer.lock file and make it call your custom helper.

what does it mean in blade #extends('some::thing')

Today I have installed jeroennoten/laravel-adminlte and after following all the installation command I created a view and just wrote the line
#extends('adminlte::page')
and it works fine but I do not understand how it works? specially this :: symbol? I checked the laravel documentation but could not find anything.
Please help me by explaining it or give some article/tutorial link from where I can learn more.
adminlte is the name of the package, which is used for views and configs in Laravel as a namespace in order to avoid conflicts with other other packages.
It is defined in the ServiceProvider class on line 51.
By calling this in your blade files:
#extends('adminlte::page')
you are telling to Laravel, that you want to extend the page.blade.php file.
If you call #extends('page'), without adminlte::, it will look for the page.blade.php in your resources/views directory.
You won't see information in Laravel's Blade documentation section about this, because it's specific for Laravel Packages. And you can learn more from here.
::
symbol is a call of static function or static property in a class, for example if you define a class like this:
class Foo{
public static $a = 1;
public static function test(){};
}
you can use Foo::$a to get the value of $a, and use Foo::test() to call the function test().

How to change the language in Laravel Spark?

How can I change the language of Laravel Spark without editing all the blade-files? I could edit all blade-files and change the labbels/messages with the lang() function, but then I don't get the blade updates anymore (or I have to redo this after every update).
I have this very same problem, not only with translations, but also with small changes made to layout and stuff, and, unfortunately, I don't see anything else we, or they, could do now, for translations it could be fairily easy, but for other changes, not really. Changes are verified using MD5, so if you change a single letter in your view you make it unupdatable.
The day after installing it, and changing some views I was already stuck with Spark not being able to upgrade my views because of those changes, and I basically had to go thru the changed files and see if there was something important I would have to copy back to mines.
Thinking that they might at some point add new features to Spark, we would never get them automatically, if we change those views. That's why my decision was to touch only login and register, and let Spark deal with everything else in the Spark panel, while I build a completely separate system around it using my own template. At some point I know I'll have to add settings, so I'll also have to decide about having a second settings page, using my own template, or just edit Spark views and get back to this impossible-to-auto-upgrade state.
Spark is not yet prepared to be multi-language, but now, with Mohamed Said in Laravel Team, we should probably see some changes in this area.
To replace Spark views with yours, you just have to 'override' the spark:: namespace setting your own directory:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app['view']->addNamespace('spark', resource_path('/views/vendor/'.$namespace'));
}
}
This code adds a new folder to the list of ones Laravel Spark already has, in this case it will be the my resources/view.
You'll also have to move AppServiceProvider to a line above Laravel\Spark\Providers\SparkServiceProvider::class, in config/app.php.
And you don't have to have all Spark views in this folder, Laravel is kind of eager finding views, so if it doesn't find one, it will try to find it in the original spark folders.

How can I render a twig template in a custom controller in Silex?

I'm playing with Silex microframework to build a very simple app.
The Silex documentation briefly illustrates how to keep your code organised using controller as class and I've also found this useful article talking about the same practice:
https://igor.io/2012/11/09/scaling-silex.html
but still can't solve my problem
The issue:
in my app.php I'm using
$app->get('/{artist}', 'MyNamespace\\MyController::getAlbum');
This is working. MyController is a class correctly loaded through composer using psr-4.
At the moment the return method of getAlbum($artist) is return $player;
What I'd like to do instead, is returning a twig view from getAlbum, something like:
return $app['twig']->render('player.twig', $player);
To do so, what I've tried to do in my custom class/controller is:
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
[...]
public function getAlbum(Request $request, Application $app, $artist)
but this is generating the following error when I try to access the routed pages:
ReflectionException in ControllerResolver.php line 43:
Class MyNamespace\Request does not exist
Whic made me think that there's a namespace conflict between myNamespace and the Silex namespaces?!
What am I doing wrong?
Is this the right way to make $app visible in my custom controller in order to use return $app['twig']... ?
Thank you in advance!
EDIT
After several other tries still didn't get to the point (replies still welcome!) but I've found a workaround solution that could be useful to anyone will incur in a similar issue. Directly in my app.php I added this
$app->get('/{artist}', function (Silex\Application $app, $artist) use ($app)
{
$object = new MyNamespace\MyController();
$player = $object->getAlbum($artist);
return $app['twig']->render('player.twig',
array(
//passing my custom method return to twig
'player' => $player,));
});
then in my player.twig I added:
{{player | raw}}
And this basically means that I still need an anonymous function to get use of my custom method which is a working solution I'm not really happy with because:
I'm using 2 functions for 1 purpose.
The return value of getAlbum is dependent from the use of "raw" in twig.
SOLVED
The workflow described works fine. It was a distraction error: I've placed the namespace of my custom class after use Symfony\Component\HttpFoundation\Request;
namespace declaration in PHP needs always to be at the top of the file, Silex wasn't able to injects $app and $request for this reason.

Magento email template: Block template inheritance broken

Problem
When I try to add a block into my transactional email template in the following manner:
{{block type='core/template' area='frontend' template='invent/baskettimer/email_items.phtml' record=$record}}
I get the following error, and nothing is rendered.
CRIT (2): Not valid template file:frontend/base/default/template/invent/baskettimer/email_items.phtml
Troubleshooting
Normally this warning points to a typo which is breaking the inheritance but I have quadruple checked and this should work.
I then copied the file into the base and did a test, it rendered correctly.
Create a custom block and set the template, same error is displayed.
Theory
To me it seems template inheritance is broken / not implemented for emails, so it is always looking in base, I cannot put my templates there so I am not sure how to call them.
Possible workarounds
Render the block to html then send it to as a variable to render, problem with this is I am sending the emails from Model level and am having a hard time pre rendering the block, even with a helper.
Render the data using a method, don't really want to do this as it is message / against MVC.
Any help is much appreciated.
Bounty update
So I have traced down the problem, it is probably an easy solution now.
The problem is that I am calling it from a cronjob does not have the correct store view, it is fairly easy to replicate similar situation by using a shell script, then changing the _appCode to null.
<?php
require_once 'abstract.php';
class Mage_Shell_Shell extends Mage_Shell_Abstract
{
protected $_appCode = ''; // works - remove to not work
/**
* Run script
*
*/
public function run()
{
Mage::getModel('invent_baskettimer/email')->sendJob();
}
}
$shell = new Mage_Shell_Shell();
$shell->run();
So basically the question has become:
How do I call a block->toHtml() regardless of store view?
There is not way of setting a cronjob to be like that. Lucky magento lets you emulate your store views, see the following to emulate the default store.
public function cronjob()
{
$iDefaultStoreId = Mage::app()
->getWebsite()
->getDefaultGroup()
->getDefaultStoreId();
$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($iDefaultStoreId);
.. do your stuff here ..
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
}
For more info see: http://inchoo.net/ecommerce/magento/emulate-store-in-magento/

Resources