3

I'm trying to get Pheanstalk working for PHP but I can't load it.

I downloaded the source code from https://github.com/pda/pheanstalk, I moved src/Pheanstalk to my project directory, and then did the following in test.php:

use Pheanstalk\Pheanstalk;
$pheanstalk = new Pheanstalk('127.0.0.1');

But this gives me the following error:

Fatal error: Class 'Pheanstalk\Pheanstalk' not found in test.php on line 2

How do I just use Pheanstalk from the cloned git repository?

bodacydo
  • 75,521
  • 93
  • 229
  • 319
  • Maybe this will help: http://stackoverflow.com/questions/20181181/composer-autoloading – jeffjenx Oct 01 '14 at 03:05
  • @Quantastical Thanks, looking into it. I dont know what composer is though... – bodacydo Oct 01 '14 at 03:19
  • Me either, but upon looking at pheanstalk's github, it states `// Hopefully you're using Composer autoloading.` I presume it is some way of autoloading class files based on JSON descriptions of where the source lives, but you could just as easily do an `include( ... );` to the `src/Pheanstalk` code. – jeffjenx Oct 01 '14 at 03:27

2 Answers2

5

I wrote a article on Beanstalk, Beanstalkd and Pheanstalk;

Check it out: How to install Beanstalkd and Pheanstalk on Ubuntu

The solution for your problem is there.

1). Install Ubuntu Desktop or Server using a Virtual Machine.

I used Oracle VM VirtualBox for this example.

Make sure that a Bridged Network Connection has been set.

Set a username and a password when asked to.

I used:

username: william password: 123456

2). Start Ubuntu and Login

3). Login as Super User

use: sudo su and enter the super user password when requested ( mine is: 123456 );

4). Install Open SSH and access the Ubuntu virtual machine via Putty.

docs: https://help.ubuntu.com/10.04/serverguide/openssh-server.html

use: apt-get install openssh-client

to install the client and then

use: apt-get install openssh-server

to install the server;

5). Install Apache2 web server

use: apt-get install apache2

now type the following in a browser:

http://localhost

and your virtual machine ip ( mine was 192.168.1.104 )

6). Install MySQL

use: apt-get install mysql-server php5-mysql

set password for MySQL root user: 123456 repeat password: 123456

7). Install PHP5

use: apt-get install php5 libapache2-mod-php5 php5-mcrypt

8). Install cURL

use: apt-get install curl

9). Install Composer

official website: https://getcomposer.org/ docs: https://getcomposer.org/doc/00-intro.md ( Getting Started )

use: curl -s http://getcomposer.org/installer | php or use: curl -sS https://getcomposer.org/installer | php now, composer.phar must be moved and converted

use: mv composer.phar /usr/bin/composer

10). Install Beanstalkd

use: apt-get install beanstalkd

now, let's make sure that beanstalkd persistent mode is active

ps ax

ps ax | grep beans

locate beanstalkd.conf

updatedb

locate beanstalkd.conf

nano /etc/default/beanstalk

uncomment last line in order to save persistent mode to active

11). Install Pheanstalk

docs: https://github.com/pda/pheanstalk

change directory use: cd /var/www/html

create a new directory: /var/www/html/pheanstalk_test use: mkdir pheanstalk_test

change directory to the new created directory use: cd pheanstalk_test

create a composer.json file under this new directory use: nano composer.json

write the following data in the file: { "require":{ "pda/pheanstalk":"v3.0.0" } }

and save the file ( push Ctrl+X keys, push Y key, push ENTER key )

use: composer update

now, the vendor folder data should begin to download

or use: git clone https://github.com/pda/pheanstalk.git

examples: how to put data on tube/s

<?php include 'vendor/autoload.php'; use Pheanstalk\Pheanstalk; $pheanstalk = new Pheanstalk('127.0.0.1'); while(true){ $tube_id=rand(1,9); $r1=rand(1,10000000); $r2=rand(1,10000000); $pheanstalk->useTube('testtube'.$tube_id)->put('{'.$r1.':'.$r2.'}'); } ?>

use: php put.php and a put process will start info! the more instances open, the more data gets put on the tube/s

how to get data from tube/s

<?php include 'vendor/autoload.php'; use Pheanstalk\Pheanstalk; $pheanstalk = new Pheanstalk('127.0.0.1'); while(true){ $tube_id=rand(1,9); $job=$pheanstalk->watch('testtube'.$tube_id)->ignore('default')->reserve(); if($job){ echo $job->getdata(); $pheanstalk->delete($job); } } ?>

use: php get.php and a get process will start info! the more instances open, the more data gets processed;

12). Install Beanstalk Console

docs: https://github.com/ptrofimov/beanstalk_console

change directory

use: cd /var/www/html

create a new folder

use: mkdir beanstalk_console

use: git clone https://github.com/ptrofimov/beanstalk_console.git

change the rights for the file: storage.json chmod 777 storage.json

now, to access beanstalk console, write in the browser: http://localhost/beanstalk_console/public

13). This is it!

Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100
0

While the answer by Ionut is viable for getting Pheanstalk working, it doesn't actually answer the question posed: How do you get Pheanstalk working from a git clone of the project repo? (Implicit here is that you don't want to use Composer for the installation, which is the assumption I'll be using for this answer.)

The main issue is that you can't just require('src/Pheanstalk.php'); and then start using the Pheanstalk class. The Pheanstalk library relies heavily on PHP class autoloading, so without that setup, it will error out as described in the original question.

If you have a PSR-4 compliant autoloader for your project already, great! Point it at the psr-4 section of the composer.json file and watch the magic work.

For everyone else, however, you'll want to use the function provided by Thibault in his answer to a similar question: https://stackoverflow.com/a/39774973

Assuming you have the Pheanstalk git repo cloned into your project at pheanstalk, you can then just use loadPackage('pheanstalk') and it should all work properly.

Full example:

function loadPackage($dir)
{
    $composer = json_decode(file_get_contents("$dir/composer.json"), 1);
    $namespaces = $composer['autoload']['psr-4'];

    // Foreach namespace specified in the composer, load the given classes
    foreach ($namespaces as $namespace => $classpath) {
        spl_autoload_register(function ($classname) use ($namespace, $classpath, $dir) {
            // Check if the namespace matches the class we are looking for
            if (preg_match("#^".preg_quote($namespace)."#", $classname)) {
                // Remove the namespace from the file path since it's psr4
                $classname = str_replace($namespace, "", $classname);
                $filename = preg_replace("#\\\\#", "/", $classname).".php";
                include_once $dir."/".$classpath."/$filename";
            }
        });
    }
} 

loadPackage(__DIR__."/pheanstalk");

use Pheanstalk\Pheanstalk;

$pheanstalk = new Pheanstalk('127.0.0.1');

echo $pheanstalk->getConnection()->isServiceListening();
Joe C.
  • 1,538
  • 11
  • 14