PHP Classes
elePHPant
Icontem

File: src/PHPVideoToolkit/Config.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Oliver Lillie  >  PHP Video Toolkit  >  src/PHPVideoToolkit/Config.php  >  Download  
File: src/PHPVideoToolkit/Config.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Video Toolkit
Manipulate and convert videos with ffmpeg program
Author: By
Last change: bug fix for systems when realpath returns false on some systems and thus the configsetexceptions thrown have no path set in the message.
fixed missing config variable bug. #18. Thanks olfan.
Date: 10 months ago
Size: 6,976 bytes
 

Contents

Class file image Download
<?php
   
   
/**
     * This file is part of the PHP Video Toolkit v2 package.
     *
     * @author Oliver Lillie (aka buggedcom) <publicmail@buggedcom.co.uk>
     * @license Dual licensed under MIT and GPLv2
     * @copyright Copyright (c) 2008-2014 Oliver Lillie <http://www.buggedcom.co.uk>
     * @package PHPVideoToolkit V2
     * @version 2.1.1
     * @uses ffmpeg http://ffmpeg.sourceforge.net/
     */
    
    
namespace PHPVideoToolkit;
    
   
/**
     * @access public
     * @author Oliver Lillie
     * @author Jorrit Schippers
     * @package default
     */
   
class Config
   
{
       
/**
         * PlaceHolder for self (Singlton)
         *
         * @var App_Config
         */
       
public static $instance = null;
       
        protected
$_ffmpeg;
        protected
$_ffprobe;
        protected
$_yamdi;
        protected
$_qtfaststart;
        protected
$_temp_directory;
        protected
$_gif_transcoder;
        protected
$_gifsicle;
        protected
$_convert;
        protected
$_php_exec_infinite_timelimit;
        protected
$_force_enable_qtfaststart;
        protected
$_force_enable_flv_meta;

       
/**
         * Get the Instance of self
         *
         * @return App_Config
         */
       
public static function getInstance()
        {
            if(
self::$instance === null)
            {
               
self::$instance = new self;
            }

            return
self::$instance;
        }

       
/**
         * Class Constructor
         *
         * @param array $config
         * @access private
         */
       
public function __construct(array $options=array())
        {
           
$default_options = array(
               
'ffmpeg' => 'ffmpeg',
               
'ffprobe' => 'ffprobe',
               
'yamdi' => null, //'yamdi', // http://yamdi.sourceforge.net/ for flv meta injection
               
'qtfaststart' => null, //'qt-faststart', // https://ffmpeg.org/trac/ffmpeg/wiki/UbuntuCompilationGuide#qt-faststart for fast streaming of mp4/h264 files.
               
'temp_directory' => sys_get_temp_dir(),
               
'gif_transcoder' => null,
               
'gifsicle' => null,
               
'convert' => null,
               
'php_exec_infinite_timelimit' => true,
               
'force_enable_qtfaststart' => false,
               
'force_enable_flv_meta' => true,
            );
           
$this->setConfig(array_merge($default_options, $options));
        }

       
/**
         * Set config options array
         *
         * @param array $options
         * @access private
         * @return App_Config
         */
       
private function setConfig(array $options=array())
        {
            foreach (
$options as $key => $value)
            {
               
$this->{$key} = $value;
            }
           
            return
$this;
        }

       
/**
         * Magic method get
         *
         * This get's triggerd if there is a call made to an undefined property in
         * the App_Config instance or subInstance, so we throw an Exception
         *
         * @param string $name
         * @throws Exception
         */
       
public function __set($key, $value)
        {
            switch(
$key)
            {
                case
'ffmpeg' :
                case
'ffprobe' :
                case
'yamdi' :
                case
'qtfaststart' :
                case
'gifsicle' :
                case
'convert' :
               
                    if(
$value !== null)
                    {
                        if(
strpos($value, '/') !== 0)
                        {
                            try
                            {
                               
$value = Binary::locate($value);
                            }
                            catch(
BinaryLocateException $e)
                            {
                                throw new
ConfigSetException('Unable to locate the '.$value.' binary. Please specify the full path instead.');
                            }
                        }
                    }
                   
                   
$this->{'_'.$key} = $value;
                   
                    return;
                   
                case
'gif_transcoder' :
                   
                    if(
in_array($value, array('gifsicle', 'convert', 'php', null)) === false)
                    {
                        throw new
ConfigSetException('Unrecognised gif transcoder engine.');
                    }
               
                   
$this->{'_'.$key} = $value;
                   
                    return;
                   
                case
'force_enable_qtfaststart' :
                case
'php_exec_infinite_timelimit' :
                case
'force_enable_flv_meta' :
                   
                    if(
in_array($value, array(true, false)) === false)
                    {
                        throw new
ConfigSetException('Unrecognised '.$key.' value. It must be a boolean value, either true or false.');
                    }
               
                   
$this->{'_'.$key} = $value;
                   
                    return;
                   
                case
'temp_directory' :
               
                   
$original_value = $value;
                   
$value = realpath($value);
                    if(empty(
$value) === true || is_dir($value) === false)
                    {
                        throw new
ConfigSetException('`temp_directory` "'.$original_value.'" does not exist or is not a directory.');
                    }
                    else if(
is_readable($value) === false)
                    {
                        throw new
ConfigSetException('`temp_directory` "'.$original_value.'" is not readable.');
                    }
                    else if(
is_writable($value) === false)
                    {
                        throw new
ConfigSetException('`temp_directory` "'.$original_value.'" is not writeable.');
                    }
                   
                   
$this->{'_'.$key} = $value;
                   
                    return;
            }
           
            throw new
ConfigSetException('Setting undefined configuration property: '.$key);
        }

       
/**
         * Magic method get
         *
         * This get's triggerd if there is a call made to an undefined property in
         * the App_Config instance or subInstance, so we throw an Exception
         *
         * @param string $name
         * @throws Exception
         */
       
public function __get($key)
        {
            if(isset(
$this->{'_'.$key}) === true)
            {
                return
$this->{'_'.$key};
            }
           
// TODO trigger error instead of just returning null
           
return null;
// throw new Exception('Call to undefined property: '.$name);
       
}
    }