4

I am having trouble implementing a particular class in PHP. I want to use Anthony Ferrara's RandomLib library in my Zend Framework application (you can find it here.)

I've been programming in PHP for a few years now, so I know my way around it for the most part. But I have to admit that I'm kind of clueless when it comes to using classes that implement namespaces. Here's what I've got in my code:

public static function generateToken()
{
    require_once 'RandomLib/Factory.php';
    $factory = new \RandomLib\Factory;
    $generator = $factory->getMediumStrengthGenerator();
    return $generator->generate(16);
}

Unfortunately, I'm getting the following error:

Fatal error: Class 'SecurityLib\AbstractFactory' not found in C:\xampp\php\includes\RandomLib\Factory.php on line 30

Like I said, I really have no idea what's going on here. I don't know if I'm supposed to use some kind of use statement in my class or not.

blainarmstrong
  • 1,040
  • 1
  • 13
  • 33
  • 1
    Which version of ZF are you using? You'll need to configure your autoloader to also load RandomLib's classes correctly. – Maerlyn Jun 06 '13 at 10:15
  • 2
    This has nothing to do with the namespace. I expect that the library uses an autoloader or something like that. you'll to make sure this works. check its documentation – hek2mgl Jun 06 '13 at 10:15
  • I'm using version 1.11. And now, for another noob question: how do I go about configuring the autoloader to load the RandomLib classes correctly? – blainarmstrong Jun 06 '13 at 10:16
  • 1
    I see the libary doesn't introduce its own autoloader. You should use the one from zend framework: http://framework.zend.com/manual/1.12/en/zend.loader.autoloader.html or [my one :)](https://github.com/metashock/Jm_Autoloader) – hek2mgl Jun 06 '13 at 10:19

3 Answers3

1

With autoloader with ZF 1.*, asuming you put your factoru into application_name/libs/RandomLibFactory.php as RandomLibFactory class it should look like this:

public static function generateToken() {
    $factory = $locator = new RandomLibFactory();
    $generator = $factory->getMediumStrengthGenerator();
    return $generator->generate(16); }
Amorphous
  • 779
  • 7
  • 27
0

For anyone whom spent lot of time and didn't find a clue, tearing his hairs apart and hitting the wall to death:

  • you need to have an autoloader as in "bootstrap.php" in the folder "test/", to manage every namespace... or you need to link every file in the folder which is not very smart. See spl_autoload_register in php.net
  • you just downloaded RandomLib, which depends on another library (the author didn't mentioned it): so you need SecurityLib (it has the same folder structure: copy what is inside "lib/" into the other "lib/" folder.

example of autoloader for a script calling from the root folder "RandomLib-1.1.0/" (see that 'lib' in the $path ?):

spl_autoload_register(function ($class) {
    $nslen = strlen(__NAMESPACE__);
    if (substr($class, 0, $nslen) != __NAMESPACE__) {
        //Only autoload libraries from this package
        return;
    }
    $path = substr(str_replace('\\', '/', $class), $nslen);
    $path = __DIR__ . '/lib/' . $path . '.php';
    if (file_exists($path)) {
        require_once $path;
    }
});

now you are set and can use classes freely without worrying about including or requiring files.

$factory = new RandomLib\Factory;

$generator = $factory->getLowStrengthGenerator();
//$generator = $factory->getMediumStrengthGenerator();
//$generator = $factory->getHighStrengthGenerator();

$randomStringLength = 16;
$randomStringAlphabet = '0123456789@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/';
$randomString = '';

for ($i=0;$i<10;$i++){
    $randomString = $generator->generateString( $randomStringLength , $randomStringAlphabet);
    echo $randomString.'<br>';
}
Nereo Costacurta
  • 7,693
  • 4
  • 21
  • 27
0

I too tore out many hairs, before I found the solution for my Windows machine.

  1. I downloaded the Windows version of Composer (which I had barely heard of let alone used).
  2. I used it to install RandomLib (as shown in the Install section of the instructions - use a command box in Windows)
  3. This generated a vendor folder containing a composer folder an ircmaxell folder and an autoload.php file; these I uploaded to a suitable place in my website The vendor directory includes both the random-lib and the - required - security-lib libraries.
  4. In the program in which I wanted to generate random text I included that autoload.php (preceded by suitable directory pointers)
  5. Then I was good to go to create a factory, generator and string as shown in the library's instructions
Ron
  • 11
  • 2