-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoload.php
76 lines (59 loc) · 2.37 KB
/
autoload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* This file is part of the php-resque package.
*
* (c) Michael Haynes <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Resque\Job;
use Resque\Event;
use Resque\Logger;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/*
This is an example for a basic autoloader for the example jobs.
You probably want to have your own autoloader (or use composer)
that autoloads your job class files when they're called from php-resque
There is access to the following variables from here:
- $config array An array of configuration options parsed from config file and options
- $input InputInterface Console input interface for retreiving user option
- $output OutputInterface Console output interface for sending messages to user
- $logger Logger The logger instance, you should log messages to this with the `log()` method
*/
class YiiResqueAutoloader
{
protected $logger;
protected $job = null;
public function __construct(Logger $logger)
{
$this->logger = $logger;
$this->registerEvents();
$this->registerAutoload();
}
// If you're wondering what this is about run `worker:start` with `-vvv` (very verbose)
// which outputs all the debugging logs
public function registerEvents()
{
$job =& $this->job;
Event::listen(Event::JOB_PERFORM, function ($event, $_job) use (&$job) {
$job = $_job;
});
}
public function registerAutoload()
{
$job =& $this->job;
$logger = $this->logger;
spl_autoload_register(function ($class) use (&$job, $logger) {
$isJobClass = $job instanceof Job and $job->getClass() == trim($class, ' \\');
if (file_exists($file = __DIR__ . '/../../../' . str_replace('\\', '/', $class) . '.php')) {
$isJobClass and $logger->log('Including job ' . $job . ' class ' . $class . ' file ' . $file . ' in pid:' . getmypid(), Logger::DEBUG);
require_once $file;
} else {
$isJobClass and $logger->log('Job ' . $job . ' class ' . $class . ' file was not found, tried: ' . $file . ' in pid:' . getmypid(), Logger::DEBUG);
}
});
}
}
new YiiResqueAutoloader($logger);