phpspl-autoloader

autoloader when executing php from linux bash


im currently working on some sort of upload with automatic video conversion. At the moment i am executing a php script via php shell command after the upload is finished so the user doesn't have to wait until the conversion is completed. Like so:

protected function _runConversionScript() {
    if (!exec("php -f '" . $this->_conversionScript . "' > /dev/null &"))
        return true;

    return false;
}

Now in my conversion script file i am using functions from another class "UploadFunctions" to update the status in the database (like started, converted, finished...). The problem there is though that this UploadFunctions class inherits from another class "Controller" where for example the database connection gets established. Currently i am using spl_autoloader to search specific directories for the files needed (for example controller.php), but because the conversion script is out of context with the whole autoloader stuff it doesn't recognize the Controller class and throws an fatal php error. Here is some code from the conversion script:

require_once('uploadfunctions.php');

$upload_func = new UploadFunctions();

// we want to make sure we only process videos that haven't already
// been or are being processed
$where = array(
    'status' => 'queued'
);
$videos = $upload_func->getVideos($where);

foreach ($videos as $video) {

    // update database to show that these videos are being processed
    $update = array(
        'id' => $video['id'],
        'status' => 'started'
    );
    // execute update
    $upload_func->updateVideo($update);
.........

Am i doing this completly wrong or is there a better way to accomplish this? If you need more code or information please let me know! Thanks a lot

Here is my spl_autoload code:

<?php
spl_autoload_register('autoloader');

function autoloader($class_name) {
        $class_name = strtolower($class_name);

        $pos = strpos($class_name ,'twig');

        if($pos !== false){
            return false;
        }

        $possibilities = array(
            '..'.DIRECTORY_SEPARATOR.'globals'.DIRECTORY_SEPARATOR.$class_name.'.php',
            'controller'.DIRECTORY_SEPARATOR.$class_name.'.php',
            '..'.DIRECTORY_SEPARATOR.'libs'.DIRECTORY_SEPARATOR.$class_name.'.php',
            'local'.DIRECTORY_SEPARATOR.$class_name.'.php'
        );

        foreach ($possibilities as $file) {

            if(class_exists($class_name) != true) {
                if (file_exists($file)) {

                    include_once($file);
                }
            }
        }
    }
?>

I have my project divided into subfolders wich represent the functionality, for example upload, myaccount and gallery.. in every subfolder there are also 2 other folders: controller and local. Controller is the class controlling this part (upload for example) and local is the folder where i am putting the local classes wich are needed. The controller class gets called from the index.php wich is located in the sub-project folder. "libs" and "global" are just projectwide classes, like database, user and so on. This is an example of my folder structure:

www/index.php // main site

www/upload/index.php // calls the controller for upload and initializes the spl_autoload

www/upload/controller/indexcontroller.php // functionality for the upload

www/upload/local/processVideo.php // this is the conversion script.

I am fairly new to spl_autoload function. In my opinion the spl_autoload is not getting called if my script is calling: "php -f processVideo.php", isn't it?


Solution

  • I was actually able to resolve the issue. I had to include the spl_autoload_register function inside the conversion script so that it was able to locate the files. This was an issue because the conversion script is not build into my framework an so it isn't able to load the classes from the framework autoloader.