Creating a Helper from a PHP Library?

Permalink 1 user found helpful
I'm trying to create a concrete5 helper out of the SmartyPants PHP library.

I've edited the smartypants.php file to lead off with:

<?php
defined('C5_EXECUTE') or die('Access Denied.');
class SmartyPants {

and placed it in my theme's helper folder: /packages/brp/helpers/smartypants.php

When I call to load the helper like this: $sp = Loader::helper('smartypants', 'brp');

I get a big ol' error:

Fatal error: require_once(): Failed opening required '/Volumes/Data/Dropbox/SyncedSites/vhosts/www.concrete.dev/updates/concrete5.6.2.1_updater/concrete/helpers/smartypants.php' (include_path='/Volumes/Data/Dropbox/SyncedSites/vhosts/www.concrete.dev/libraries/3rdparty:/Volumes/Data/Dropbox/SyncedSites/vhosts/www.concrete.dev/updates/concrete5.6.2.1_updater/concrete/libraries/3rdparty:.:/Applications/MAMP/bin/php/php5.4.10/lib/php') in /Volumes/Data/Dropbox/SyncedSites/vhosts/www.concrete.dev/updates/concrete5.6.2.1_updater/concrete/core/libraries/loader.php on line 297


It doesn't appear that concrete5 is even trying to look in my theme/helpers folder.

What have I configured wrong?

Is there some documentation on creating helpers that I've missed?

bobrocke
View Replies: View Best Answer
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
Usually with 3rd party code you put the whole lot unmodified in
/packages/my_package/libraries/3rdparty/foreign_class.php

Concrete5.org and the core will then ignore many of the usual rules that foreign code breaks.

You can then create a helper to wrap the library in the file
/packages/my_packahes/helpers/my_helper_name.php
class MyHelperNameHelper {
  public function whatever(){  
    Loader::library('3rdparty/foreign_class', 'my_package');
    $foreighn_instance = new WhateverForeighClassNamesAreThere();
    ...
    ...
  }
}


In your particular case, it could be because the class is SmartyPants rather than SmartyPantsHelper. Nevertheless, the above structure enables you to leave the 3rd party code untouched.

Bear in mind that helpers are pseudo-static. They only ever get instantiated once. If that does not suit you, better to go for a model or library to wrap the 3rd party code, but with the same basic principle.
bobrocke replied on at Permalink Reply
bobrocke
Thanks for the detailed answer. I'm going to give that a go and report back.

Bob
bobrocke replied on at Permalink Reply
bobrocke
OK, your help has the helper loading without errors when I use this for the helper:

class SmartyPantsHelper {
   public function smartypants() {
      Loader::library('3rdparty/smartypants', 'brp');
       }
}


But when I call helper like this:

$sp = Loader::helper('smartypants', 'brp');
$message = $sp->smartypants($message);


I get no errors but $message comes back null.

Do I need to define any functions within my helper class to map to the functions (there are no classes) in the PHP library?
JohntheFish replied on at Permalink Reply
JohntheFish
The helper loader should be:
$sp = Loader::helper('smarty_pants', 'brp');


The rule is camelcase wherever there are '_' or '/' in the path.

Yes, you will need to write code within the smartypants function to call the actual smarty pants code you are interfacing to. There is no behind the scenes magic that will do it for you.
bobrocke replied on at Permalink Reply
bobrocke
Hmmm.

$sp = Loader::helper('smarty_pants', 'brp');


wants the file name to be smarty_pants.php, so I renamed it so. I had been smartypants.php.

Still no errors, but $message still returns null.

I have something else wrong somewhere.
bobrocke replied on at Permalink Reply
bobrocke
Wait. Wait. Wait!

I need to actually call the function from my helper like this:

class SmartyPantsHelper {
   public function smartypants($text) {
      Loader::library('3rdparty/smartypants', 'brp');
      $text = SmartyPants($text);
      return $text;
   }
}


Now I think I've got it! Thanks for all your help.