提交 9aa5eda9 authored 作者: Mitch Capper's avatar Mitch Capper
<?php <?php
/** /**
* @todo add unit tests for all methods in all classes * @todo add unit tests for all methods in all classes
* @author Raymond Chandler (intralanman@gmail.com) * @author Raymond Chandler (intralanman@gmail.com)
...@@ -10,7 +11,8 @@ ...@@ -10,7 +11,8 @@
* @author Raymond Chandler (intralanman@gmail.com) * @author Raymond Chandler (intralanman@gmail.com)
* *
*/ */
class NotImplementedException extends Exception { class NotImplementedException extends Exception {
} }
/** /**
...@@ -19,65 +21,69 @@ ...@@ -19,65 +21,69 @@
* *
*/ */
class NullDataException extends Exception { class NullDataException extends Exception {
} }
class MissingRequiredAttribute extends Exception { class MissingRequiredAttribute extends Exception {
} }
class phttapi { class phttapi {
private $logptr; private $logptr;
public $xml; public $xml;
public function __construct() { public function __construct() {
$this->logptr = fopen( '/tmp/phttapi.log', 'a' ); $this->logptr = fopen('/tmp/phttapi.log', 'a');
$this->dlog( print_r( $_REQUEST, true ) ); $this->dlog(print_r($_REQUEST, true));
$this->xml = new XMLWriter(); $this->xml = new XMLWriter();
$this->xml->openMemory(); $this->xml->openMemory();
$this->xml->setIndent( true ); $this->xml->setIndent(true);
$this->xml->setIndentString( " " ); $this->xml->setIndentString(" ");
$this->open( 'document' ); $this->open('document');
$this->attr( 'type', 'text/freeswitch-httapi' ); $this->attr('type', 'text/freeswitch-httapi');
} }
public function output() { public function output() {
$this->xml->endElement(); // </document> $this->xml->endElement(); // </document>
$xml = $this->xml->outputMemory(); $xml = $this->xml->outputMemory();
$this->dlog( $xml ); $this->dlog($xml);
return $xml; return $xml;
} }
public function open( $tag ) { public function open($tag) {
$this->xml->startElement( $tag ); $this->xml->startElement($tag);
} }
public function text( $text ) { public function text($text) {
$this->xml->text( $text ); $this->xml->text($text);
} }
public function close() { public function close() {
$this->xml->endElement(); $this->xml->endElement();
} }
public function attr( $key, $val ) { public function attr($key, $val) {
$this->xml->writeAttribute( $key, $val ); $this->xml->writeAttribute($key, $val);
} }
public function raw( $xml ) { public function raw($xml) {
$this->xml->writeRaw( $xml ); $this->xml->writeRaw($xml);
} }
public function comment( $comment ) { public function comment($comment) {
$this->xml->writeComment( $comment ); $this->xml->writeComment($comment);
} }
public function dlog( $debug = null ) { public function dlog($debug = null) {
fwrite( $this->logptr, $debug ); fwrite($this->logptr, $debug);
} }
public function start_work() { public function start_work() {
$this->open( 'work' ); $this->open('work');
} }
public function end_work() { public function end_work() {
...@@ -85,12 +91,12 @@ class phttapi { ...@@ -85,12 +91,12 @@ class phttapi {
} }
public function start_variables() { public function start_variables() {
$this->open( 'variables' ); $this->open('variables');
} }
public function add_variable( $var, $val ) { public function add_variable($var, $val) {
$this->open( $var ); $this->open($var);
$this->text( $val ); $this->text($val);
$this->close(); $this->close();
} }
...@@ -99,12 +105,12 @@ class phttapi { ...@@ -99,12 +105,12 @@ class phttapi {
} }
public function start_params() { public function start_params() {
$this->open( 'params' ); $this->open('params');
} }
public function add_param( $param, $value ) { public function add_param($param, $value) {
$this->open( $param ); $this->open($param);
$this->text( $value ); $this->text($value);
$this->close(); $this->close();
} }
...@@ -112,37 +118,38 @@ class phttapi { ...@@ -112,37 +118,38 @@ class phttapi {
$this->close(); $this->close();
} }
public function add_action( $action ) { public function add_action($action) {
if ( is_object( $action ) && $action instanceof phttapi_action ) { if (is_object($action) && $action instanceof phttapi_action) {
if ( property_exists( $action, 'action_name' ) && $action->action_name ) { if (property_exists($action, 'action_name') && $action->action_name) {
$action_name = $action->action_name; $action_name = $action->action_name;
} else { } else {
$action_name = preg_replace( '/^phttapi_/', '', get_class( $action ) ); $action_name = preg_replace('/^phttapi_/', '', get_class($action));
} }
$this->open( $action_name ); $this->open($action_name);
foreach ( $action->defined_attributes as $attr_name => $attr_value ) { foreach ($action->defined_attributes as $attr_name => $attr_value) {
$this->attr( $attr_name, $attr_value ); $this->attr($attr_name, $attr_value);
} }
if ( property_exists( $action, 'bindings' ) ) { if (property_exists($action, 'bindings')) {
foreach ( $action->bindings as $binding ) { foreach ($action->bindings as $binding) {
$this->open( 'bind' ); $this->open('bind');
if ( $binding->strip ) { if ($binding->strip) {
$this->attr( 'strip', $binding->strip ); $this->attr('strip', $binding->strip);
} }
$this->text( $binding->match ); $this->text($binding->match);
$this->close(); $this->close();
} }
} }
if ( $action->action_text ) { if ($action->action_text) {
$this->text( $action->action_text ); $this->text($action->action_text);
} }
$this->close(); $this->close();
} }
} }
} }
/** /**
...@@ -152,62 +159,58 @@ class phttapi { ...@@ -152,62 +159,58 @@ class phttapi {
* *
*/ */
class phttapi_action { class phttapi_action {
public $action_text = null; public $action_text = null;
public $action_name = null; public $action_name = null;
public $attributes = array (); public $attributes = array();
public $defined_attributes = array (); public $defined_attributes = array();
public $required_attributes = array (); public $required_attributes = array();
public function __call( $method, $args ) { public function __call($method, $args) {
$attribute = preg_replace( '/_/', '-', $method ); $attribute = preg_replace('/_/', '-', $method);
if ( property_exists( $this, 'attributes' ) && is_array( $this->attributes ) && array_key_exists( $attribute, $this->attributes ) && ! empty( $this->attributes [$attribute] ) ) { if (property_exists($this, 'attributes') && is_array($this->attributes)
if ( ! count( $args ) ) { && array_key_exists($attribute, $this->attributes) && !empty($this->attributes [$attribute])) {
throw new NullDataException( "$method requires an argument" ); if (!count($args)) {
throw new NullDataException("$method requires an argument");
} }
$value = $args [0]; $value = $args [0];
$this->attr( $attribute, $value ); $this->attr($attribute, $value);
} else { } else {
throw new NotImplementedException( sprintf( "%s doesn't implement %s", get_class( $this ), $method ) ); throw new NotImplementedException(sprintf("%s doesn't implement %s", get_class($this), $method));
} }
} }
public function attr( $attr_name, $attr_value ) { public function attr($attr_name, $attr_value) {
$this->defined_attributes [$attr_name] = $attr_value; $this->defined_attributes [$attr_name] = $attr_value;
} }
public function action( $value ) { public function name($value) {
$this->attr( preg_replace( '/_/', '-', __FUNCTION__ ), $value ); $this->attr(preg_replace('/_/', '-', __FUNCTION__), $value);
}
public function temp_action( $value ) {
$this->attr( preg_replace( '/_/', '-', __FUNCTION__ ), $value );
}
public function name( $value ) {
$this->attr( preg_replace( '/_/', '-', __FUNCTION__ ), $value );
} }
public function output() { public function output() {
if ( $this->action_text ) { if ($this->action_text) {
$this->text( $this->action_text ); $this->text($this->action_text);
} }
} }
} }
class phttapi_action_binding { class phttapi_action_binding {
public $match; public $match;
public $strip; public $strip;
public function __construct( $text = null ) { public function __construct($text = null) {
if ( ! $text ) { if (!$text) {
throw new NullDataException( 'match digits must be passed' ); throw new NullDataException('match digits must be passed');
} }
$this->match = $text; $this->match = $text;
} }
public function strip( $value = NULL ) { public function strip($value = NULL) {
if ( $value ) { if ($value) {
$this->strip = $value; $this->strip = $value;
} else { } else {
return $this->strip; return $this->strip;
...@@ -217,17 +220,20 @@ class phttapi_action_binding { ...@@ -217,17 +220,20 @@ class phttapi_action_binding {
} }
class phttapi_prompt extends phttapi_action { class phttapi_prompt extends phttapi_action {
public $bindings = array ();
public function add_binding( $binding ) { public $bindings = array();
if ( is_object( $binding ) && $binding instanceof phttapi_action_binding ) {
array_push( $this->bindings, &$binding ); public function add_binding($binding) {
if (is_object($binding) && $binding instanceof phttapi_action_binding) {
array_push($this->bindings, $binding);
} }
} }
} }
// ACTIONS // ACTIONS
class phttapi_break extends phttapi_action { class phttapi_break extends phttapi_action {
} }
/** /**
...@@ -237,12 +243,15 @@ class phttapi_break extends phttapi_action { ...@@ -237,12 +243,15 @@ class phttapi_break extends phttapi_action {
* *
*/ */
class phttapi_conference extends phttapi_action { class phttapi_conference extends phttapi_action {
public $attributes = array (
'profile' => true public $attributes = array(
'profile' => true
); );
} }
class phttapi_continue extends phttapi_action { class phttapi_continue extends phttapi_action {
} }
/** /**
...@@ -255,19 +264,21 @@ class phttapi_continue extends phttapi_action { ...@@ -255,19 +264,21 @@ class phttapi_continue extends phttapi_action {
* *
*/ */
class phttapi_dial extends phttapi_action { class phttapi_dial extends phttapi_action {
public $attributes = array (
'context' => true, public $attributes = array(
'dialplan' => true, 'context' => true,
'caller_id_name' => true, 'dialplan' => true,
'caller_id_number' => true 'caller_id_name' => true,
'caller_id_number' => true
); );
public function __construct( $text = null ) { public function __construct($text = null) {
if ( ! $text ) { if (!$text) {
throw new NullDataException( "no data passed" ); throw new NullDataException("no data passed");
} }
$this->action_text = $text; $this->action_text = $text;
} }
} }
/** /**
...@@ -277,11 +288,15 @@ class phttapi_dial extends phttapi_action { ...@@ -277,11 +288,15 @@ class phttapi_dial extends phttapi_action {
* *
*/ */
class phttapi_execute extends phttapi_action { class phttapi_execute extends phttapi_action {
public $attributes = array (
'application' => true public $attributes = array(
'application' => true
); );
} }
if ( class_exists( 'PHPUnit_Framework_TestCase' ) ) {
if (class_exists('PHPUnit_Framework_TestCase')) {
class phttapi_executeTest extends PHPUnit_Framework_TestCase { class phttapi_executeTest extends PHPUnit_Framework_TestCase {
/** /**
...@@ -295,10 +310,12 @@ if ( class_exists( 'PHPUnit_Framework_TestCase' ) ) { ...@@ -295,10 +310,12 @@ if ( class_exists( 'PHPUnit_Framework_TestCase' ) ) {
* @expectedException NotImplementedException * @expectedException NotImplementedException
*/ */
public function testFileThrowsException() { public function testFileThrowsException() {
$e = new phttapi_execute( 'text' ); $e = new phttapi_execute('text');
$e->file(); $e->file();
} }
} }
} }
/** /**
...@@ -307,17 +324,19 @@ if ( class_exists( 'PHPUnit_Framework_TestCase' ) ) { ...@@ -307,17 +324,19 @@ if ( class_exists( 'PHPUnit_Framework_TestCase' ) ) {
* *
*/ */
class phttapi_getVariable extends phttapi_action { class phttapi_getVariable extends phttapi_action {
public $attributes = array (
'permanent' => true public $attributes = array(
'permanent' => true
); );
public function __construct( $var = null ) { public function __construct($var = null) {
if ( ! $var ) { if (!$var) {
throw new NullDataException( "getVariable must be instantiated with a variable name" ); throw new NullDataException("getVariable must be instantiated with a variable name");
} else { } else {
$this->attr( 'name', $var ); $this->attr('name', $var);
} }
} }
} }
/** /**
...@@ -327,9 +346,11 @@ class phttapi_getVariable extends phttapi_action { ...@@ -327,9 +346,11 @@ class phttapi_getVariable extends phttapi_action {
* *
*/ */
class phttapi_hangup extends phttapi_action { class phttapi_hangup extends phttapi_action {
public $attributes = array (
'cause' => true public $attributes = array(
'cause' => true
); );
} }
/** /**
...@@ -340,10 +361,12 @@ class phttapi_hangup extends phttapi_action { ...@@ -340,10 +361,12 @@ class phttapi_hangup extends phttapi_action {
* *
*/ */
class phttapi_log extends phttapi_action { class phttapi_log extends phttapi_action {
public $attributes = array (
'level' => true, public $attributes = array(
'clean' => true 'level' => true,
'clean' => true
); );
} }
/** /**
...@@ -353,9 +376,11 @@ class phttapi_log extends phttapi_action { ...@@ -353,9 +376,11 @@ class phttapi_log extends phttapi_action {
* *
*/ */
class phttapi_pause extends phttapi_action { class phttapi_pause extends phttapi_action {
public $attributes = array (
'milliseconds' => true public $attributes = array(
'milliseconds' => true
); );
} }
/** /**
...@@ -372,16 +397,18 @@ class phttapi_pause extends phttapi_action { ...@@ -372,16 +397,18 @@ class phttapi_pause extends phttapi_action {
* *
*/ */
class phttapi_playback extends phttapi_prompt { class phttapi_playback extends phttapi_prompt {
public $attributes = array (
'file' => true, public $attributes = array(
'error-file' => true, 'file' => true,
'digit-timeout' => true, 'error-file' => true,
'input-timeout' => true, 'digit-timeout' => true,
'loops' => true, 'input-timeout' => true,
'name' => true, 'loops' => true,
'asr-engine' => true, 'name' => true,
'asr-grammar' => true 'asr-engine' => true,
'asr-grammar' => true
); );
} }
/** /**
...@@ -390,18 +417,22 @@ class phttapi_playback extends phttapi_prompt { ...@@ -390,18 +417,22 @@ class phttapi_playback extends phttapi_prompt {
* @method void digit_timeout * @method void digit_timeout
* @method void input_timeout * @method void input_timeout
* @method void loops * @method void loops
* @method void beep_file
* *
* @author Raymond Chandler (intralanman@gmail.com) * @author Raymond Chandler (intralanman@gmail.com)
* *
*/ */
class phttapi_record extends phttapi_prompt { class phttapi_record extends phttapi_prompt {
public $attributes = array (
'file' => true, public $attributes = array(
'error-file' => true, 'beep-file' => true,
'digit-timeout' => true, 'file' => true,
'input-timeout' => true, 'error-file' => true,
'loops' => true 'digit-timeout' => true,
'input-timeout' => true,
'loops' => true
); );
} }
/** /**
...@@ -411,9 +442,11 @@ class phttapi_record extends phttapi_prompt { ...@@ -411,9 +442,11 @@ class phttapi_record extends phttapi_prompt {
* *
*/ */
class phttapi_recordCall extends phttapi_action { class phttapi_recordCall extends phttapi_action {
public $attributes = array (
'limit' => true public $attributes = array(
'limit' => true
); );
} }
/** /**
...@@ -426,12 +459,14 @@ class phttapi_recordCall extends phttapi_action { ...@@ -426,12 +459,14 @@ class phttapi_recordCall extends phttapi_action {
* *
*/ */
class phttapi_say extends phttapi_prompt { class phttapi_say extends phttapi_prompt {
public $attributes = array (
'gender' => true, public $attributes = array(
'method' => true, 'gender' => true,
'type' => true, 'method' => true,
'language' => true 'type' => true,
'language' => true
); );
} }
/** /**
...@@ -445,14 +480,16 @@ class phttapi_say extends phttapi_prompt { ...@@ -445,14 +480,16 @@ class phttapi_say extends phttapi_prompt {
* *
*/ */
class phttapi_speak extends phttapi_prompt { class phttapi_speak extends phttapi_prompt {
public $attributes = array (
'engine' => true, public $attributes = array(
'voice' => true, 'engine' => true,
'digit-timeout' => true, 'voice' => true,
'input-timeout' => true, 'digit-timeout' => true,
'loops' => true, 'input-timeout' => true,
'file' => true, 'loops' => true,
'file' => true,
); );
} }
/** /**
...@@ -462,15 +499,17 @@ class phttapi_speak extends phttapi_prompt { ...@@ -462,15 +499,17 @@ class phttapi_speak extends phttapi_prompt {
* *
*/ */
class phttapi_sms extends phttapi_action { class phttapi_sms extends phttapi_action {
public $attributes = array (
'to' => true public $attributes = array(
'to' => true
); );
public function __construct( $text = null ) { public function __construct($text = null) {
if ( ! $text ) { if (!$text) {
throw new Exception( "no data passed" ); throw new Exception("no data passed");
} }
} }
} }
/** /**
...@@ -484,13 +523,15 @@ class phttapi_sms extends phttapi_action { ...@@ -484,13 +523,15 @@ class phttapi_sms extends phttapi_action {
* *
*/ */
class phttapi_voicemail extends phttapi_action { class phttapi_voicemail extends phttapi_action {
public $attributes = array (
'auth-only' => true, public $attributes = array(
'domain' => true, 'auth-only' => true,
'profile' => true, 'domain' => true,
'check' => true, 'profile' => true,
'id' => true 'check' => true,
'id' => true
); );
} }
/** /**
...@@ -498,8 +539,6 @@ class phttapi_voicemail extends phttapi_action { ...@@ -498,8 +539,6 @@ class phttapi_voicemail extends phttapi_action {
* but prove to be pretty useful * but prove to be pretty useful
*/ */
/** /**
* phttapi_google_tts offers a simple way to use Google's TTS api to pull a sound file to be played * phttapi_google_tts offers a simple way to use Google's TTS api to pull a sound file to be played
* *
...@@ -507,22 +546,24 @@ class phttapi_voicemail extends phttapi_action { ...@@ -507,22 +546,24 @@ class phttapi_voicemail extends phttapi_action {
* *
*/ */
class phttapi_google_tts extends phttapi_playback { class phttapi_google_tts extends phttapi_playback {
private $lang = 'en'; private $lang = 'en';
public function __construct() { public function __construct() {
$this->action_name = 'playback'; $this->action_name = 'playback';
} }
public function lang($lang) { public function lang($lang) {
$this->lang = $lang; $this->lang = $lang;
} }
public function text( $text ) { public function text($text) {
$lang = $this->lang; $lang = $this->lang;
$text = preg_replace( '/\s/', '+', $text ); $text = preg_replace('/\s/', '+', $text);
$url = "http://(ext=mp3,user_agent='Mozilla/5.0 (X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1')translate.google.com/translate_tts?tl=$lang&q=$text"; $url = "http://(ext=mp3,user_agent='Mozilla/5.0 (X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1')translate.google.com/translate_tts?tl=$lang&q=$text";
$this->file( $url ); $this->file($url);
} }
} }
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论