44use Qiniu \Config ;
55use Qiniu \Http \Client ;
66use Qiniu \Http \Error ;
7+ use Qiniu \Processing \Operation ;
78
89final class PersistentFop
910{
1011 private $ auth ;
11- private $ bucket ;
12- private $ pipeline ;
13- private $ notify_url ;
12+ private $ bucket ;
13+ private $ pipeline ;
14+ private $ notify_url ;
1415
15- public function __construct ($ auth , $ bucket , $ pipeline = null , $ notify_url = null )
16+ public function __construct ($ auth , $ bucket , $ pipeline = null , $ notify_url = null , $ force = false )
1617 {
1718 $ this ->auth = $ auth ;
1819 $ this ->bucket = $ bucket ;
1920 $ this ->pipeline = $ pipeline ;
2021 $ this ->notify_url = $ notify_url ;
22+ $ this ->force = $ force ;
2123 }
2224
23- public function execute ($ key , array $ fops, $ force = false )
25+ public function execute ($ key , array $ fops )
2426 {
2527 $ ops = implode ('; ' , $ fops );
2628 $ params = array ('bucket ' => $ this ->bucket , 'key ' => $ key , 'fops ' => $ ops );
@@ -30,15 +32,15 @@ public function execute($key, array $fops, $force = false)
3032 if (!empty ($ this ->notify_url )) {
3133 $ params ['notifyURL ' ] = $ this ->notify_url ;
3234 }
33- if ($ force ) {
35+ if ($ this -> force ) {
3436 $ params ['force ' ] = 1 ;
3537 }
3638 $ data = http_build_query ($ params );
3739 $ url = Config::API_HOST . '/pfop/ ' ;
3840 $ headers = $ this ->auth ->authorization ($ url , $ data , 'application/x-www-form-urlencoded ' );
3941 $ headers ['Content-Type ' ] = 'application/x-www-form-urlencoded ' ;
4042 $ response = Client::post ($ url , $ data , $ headers );
41- if ($ response ->statusCode != 200 ) {
43+ if (! $ response ->ok () ) {
4244 return array (null , new Error ($ url , $ response ));
4345 }
4446 $ r = $ response ->json ();
@@ -50,9 +52,43 @@ public static function status($id)
5052 {
5153 $ url = Config::API_HOST . "/status/get/prefop?id= $ id " ;
5254 $ response = Client::get ($ url );
53- if ($ response ->statusCode != 200 ) {
55+ if (! $ response ->ok () ) {
5456 return array (null , new Error ($ url , $ response ));
5557 }
5658 return array ($ response ->json (), null );
5759 }
60+
61+ public function __call ($ method , $ args )
62+ {
63+ $ key = $ args [0 ];
64+ $ cmd = $ method ;
65+ $ mod = null ;
66+ if (count ($ args )>1 ) {
67+ $ mod = $ args [1 ];
68+ }
69+
70+ $ options = array ();
71+ if (count ($ args )>2 ) {
72+ $ options = $ args [2 ];
73+ }
74+
75+ $ target_bucket = null ;
76+ if (count ($ args )>3 ) {
77+ $ target_bucket = $ args [3 ];
78+ }
79+
80+ $ target_key = null ;
81+ if (count ($ args )>4 ) {
82+ $ target_key = $ args [4 ];
83+ }
84+
85+ $ pfop = Operation::buildOp ($ cmd , $ mod , $ options );
86+ if ($ target_bucket != null ) {
87+ $ pfop = Operation::saveas ($ pfop , $ target_bucket , $ target_key );
88+ }
89+
90+ $ ops = array ();
91+ array_push ($ ops , $ pfop );
92+ return $ this ->execute ($ key , $ ops );
93+ }
5894}
0 commit comments