Skip to content

Commit 3a5ac07

Browse files
committed
Merge pull request #122 from rwifeng/fop_refactor
rm metaprogramming
2 parents 78b826f + 48fdce0 commit 3a5ac07

File tree

4 files changed

+71
-189
lines changed

4 files changed

+71
-189
lines changed

src/Qiniu/Processing/Operation.php

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,16 @@
11
<?php
2+
23
namespace Qiniu\Processing;
34

45
use Qiniu\Http\Client;
56
use Qiniu\Http\Error;
67

78
final class Operation
89
{
10+
911
private $auth;
1012
private $token_expire;
1113
private $domain;
12-
public static function buildOp($cmd, $mode = null, array $args = array())
13-
{
14-
$op = array($cmd);
15-
if ($mode !== null) {
16-
array_push($op, $mode);
17-
}
18-
foreach ($args as $key => $value) {
19-
array_push($op, "$key/$value");
20-
}
21-
return implode('/', $op);
22-
}
23-
24-
public static function pipeCmd($cmds)
25-
{
26-
return implode('|', $cmds);
27-
}
28-
29-
public static function saveas($op, $bucket, $key)
30-
{
31-
return self::pipeCmd(array($op, 'saveas/' . \Qiniu\entry($bucket, $key)));
32-
}
3314

3415
public function __construct($domain, $auth = null, $token_expire = 3600)
3516
{
@@ -38,56 +19,42 @@ public function __construct($domain, $auth = null, $token_expire = 3600)
3819
$this->token_expire = $token_expire;
3920
}
4021

41-
public function buildUrl($key, $cmd, $mod = null, array $args = array())
22+
23+
/**
24+
* 对资源文件进行处理
25+
*
26+
* @param $key 待处理的资源文件名
27+
* @param $fops string|array fop操作,多次fop操作以array的形式传入。
28+
* eg. imageView2/1/w/200/h/200, imageMogr2/thumbnail/!75px
29+
*
30+
* @return array[] 文件处理后的结果及错误。
31+
*
32+
* @link http://developer.qiniu.com/docs/v6/api/reference/fop/
33+
*/
34+
public function execute($key, $fops)
4235
{
43-
$fop = self::buildOp($cmd, $mod, $args);
44-
$baseUrl = "http://$this->domain/$key?$fop";
45-
$url = $baseUrl;
46-
if ($this->auth != null) {
47-
$url = $this->auth->privateDownloadUrl($baseUrl, $this->token_expire);
36+
$url = $this->buildUrl($key, $fops);
37+
$resp = Client::get($url);
38+
if (!$resp->ok()) {
39+
return array(null, new Error($url, $resp));
4840
}
49-
return $url;
41+
if ($resp->json() != null) {
42+
return array($resp->json(), null);
43+
}
44+
return array($resp->body, null);
5045
}
5146

52-
private static $fops = array(
53-
'imageView2',
54-
'imageMogr2',
55-
'imageInfo',
56-
'exif',
57-
'watermark',
58-
'imageAve',
59-
60-
'avinfo',
61-
'pm3u8',
62-
63-
'qrcode',
64-
'md2html',
65-
);
66-
67-
public function __call($method, $args)
47+
public function buildUrl($key, $fops)
6848
{
69-
70-
if (!in_array($method, self::$fops)) {
71-
throw new \InvalidArgumentException("fop {$method} isn't supported");
72-
}
73-
$key = $args[0];
74-
$mode = null;
75-
if (count($args)>1) {
76-
$mode = $args[1];
49+
if (is_array($fops)) {
50+
$fops = implode('|', $fops);
7751
}
7852

79-
if (count($args)>2) {
80-
$options = $args[2];
81-
}
82-
$options = array();
83-
$url = $this->buildUrl($key, $method, $mode, $options);
84-
$r = Client::get($url);
85-
if (!$r->ok()) {
86-
return array(null, new Error($url, $r));
87-
}
88-
if ($r->json() != null) {
89-
return array($r->json(), null);
53+
$url = "http://$this->domain/$key?$fops";
54+
if ($this->auth !== null) {
55+
$url = $this->auth->privateDownloadUrl($url, $this->token_expire);
9056
}
91-
return array($r->body, null);
57+
58+
return $url;
9259
}
9360
}

src/Qiniu/Processing/PersistentFop.php

Lines changed: 12 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,23 @@ public function __construct($auth, $bucket, $pipeline = null, $notify_url = null
4343
}
4444

4545
/**
46-
* 列取空间的文件列表
46+
* 对资源文件进行异步持久化处理
4747
*
48-
* @param $key 待处理的源文件
49-
* @param $fops 处理详细操作,规格详见 http://developer.qiniu.com/docs/v6/api/reference/fop/
48+
* @param $key 待处理的源文件
49+
* @param $fops string|array 待处理的pfop操作,多个pfop操作以array的形式传入。
50+
* eg. avthumb/mp3/ab/192k, vframe/jpg/offset/7/w/480/h/360
5051
*
5152
* @return array[] 返回持久化处理的persistentId, 和返回的错误。
53+
*
54+
* @link http://developer.qiniu.com/docs/v6/api/reference/fop/
5255
*/
53-
public function execute($key, array $fops)
56+
public function execute($key, $fops)
5457
{
55-
$ops = implode(';', $fops);
56-
$params = array('bucket' => $this->bucket, 'key' => $key, 'fops' => $ops);
58+
if (is_array($fops)) {
59+
$fops = implode(';', $fops);
60+
}
61+
62+
$params = array('bucket' => $this->bucket, 'key' => $key, 'fops' => $fops);
5763
if (!empty($this->pipeline)) {
5864
$params['pipeline'] = $this->pipeline;
5965
}
@@ -85,77 +91,4 @@ public static function status($id)
8591
}
8692
return array($response->json(), null);
8793
}
88-
89-
private static $pfops = array(
90-
'avthumb',
91-
'vframe',
92-
'segtime',
93-
'vsample',
94-
'vwatermark',
95-
'avconcat',
96-
97-
'concat',
98-
);
99-
100-
public function __call($method, $args)
101-
{
102-
103-
if (!in_array($method, self::$pfops)) {
104-
throw new \InvalidArgumentException("pfop {$method} isn't supported");
105-
}
106-
$key = $args[0];
107-
$mod = null;
108-
if (count($args)>1) {
109-
$mod = $args[1];
110-
}
111-
112-
$options = array();
113-
if (count($args)>2) {
114-
$options = $args[2];
115-
}
116-
117-
$target_bucket = null;
118-
if (count($args)>3) {
119-
$target_bucket = $args[3];
120-
}
121-
122-
$target_key = null;
123-
if (count($args)>4) {
124-
$target_key = $args[4];
125-
}
126-
127-
$pfop = Operation::buildOp($method, $mod, $options);
128-
if ($target_bucket != null) {
129-
$pfop = Operation::saveas($pfop, $target_bucket, $target_key);
130-
}
131-
132-
$ops = array();
133-
array_push($ops, $pfop);
134-
return $this->execute($key, $ops);
135-
}
136-
137-
public function mkzip(
138-
$dummy_key,
139-
$urls_and_alias,
140-
$to_bucket = null,
141-
$to_key = null,
142-
$mode = 2
143-
) {
144-
$base = 'mkzip/' . $mode;
145-
$op = array($base);
146-
foreach ($urls_and_alias as $key => $value) {
147-
if (is_int($key)) {
148-
array_push($op, 'url/' . \Qiniu\base64_urlSafeEncode($value));
149-
} else {
150-
array_push($op, 'url/' . \Qiniu\base64_urlSafeEncode($key));
151-
array_push($op, 'alias/' . \Qiniu\base64_urlSafeEncode($key));
152-
}
153-
}
154-
$fop = implode('/', $op);
155-
if ($to_bucket != null) {
156-
$op = Operation::saveas($fop, $to_bucket, $to_key);
157-
}
158-
$ops =array($op);
159-
return $this->execute($dummy_key, $ops);
160-
}
16194
}

tests/Qiniu/Tests/FopTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class FopTest extends \PHPUnit_Framework_TestCase
99
public function testExifPub()
1010
{
1111
$fop = new Operation('testres.qiniudn.com');
12-
list($exif, $error) = $fop->exif('gogopher.jpg');
12+
list($exif, $error) = $fop->execute('gogopher.jpg', 'exif');
1313
$this->assertNull($error);
1414
$this->assertNotNull($exif);
1515
}
@@ -18,8 +18,20 @@ public function testExifPrivate()
1818
{
1919
global $testAuth;
2020
$fop = new Operation('private-res.qiniudn.com', $testAuth);
21-
list($exif, $error) = $fop->exif('noexif.jpg');
21+
list($exif, $error) = $fop->execute('noexif.jpg', 'exif');
2222
$this->assertNotNull($error);
2323
$this->assertNull($exif);
2424
}
25+
26+
public function testbuildUrl()
27+
{
28+
$fops = 'imageView2/2/h/200';
29+
$fop = new Operation('testres.qiniudn.com');
30+
$url = $fop->buildUrl('gogopher.jpg', $fops);
31+
$this->assertEquals($url, 'http://testres.qiniudn.com/gogopher.jpg?imageView2/2/h/200');
32+
33+
$fops = array('imageView2/2/h/200', 'imageInfo');
34+
$url = $fop->buildUrl('gogopher.jpg', $fops);
35+
$this->assertEquals($url, 'http://testres.qiniudn.com/gogopher.jpg?imageView2/2/h/200|imageInfo');
36+
}
2537
}

tests/Qiniu/Tests/PfopTest.php

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,31 @@ class PfopTest extends \PHPUnit_Framework_TestCase
99
public function testExecute1()
1010
{
1111
global $testAuth;
12-
$pfop = new PersistentFop($testAuth, 'testres', 'sdktest', true);
13-
$op = Operation::saveas('avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240', 'phpsdk', 'pfoptest');
14-
$ops = array();
15-
array_push($ops, $op);
16-
list($id, $error) = $pfop->execute('sintel_trailer.mp4', $ops);
12+
$bucket = 'testres';
13+
$key = 'sintel_trailer.mp4';
14+
$pfop = new PersistentFop($testAuth, $bucket);
15+
16+
$fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240';
17+
list($id, $error) = $pfop->execute($key, $fops);
1718
$this->assertNull($error);
1819
list($status, $error) = PersistentFop::status($id);
1920
$this->assertNotNull($status);
2021
$this->assertNull($error);
2122
}
2223

23-
public function testAvthumb()
24-
{
25-
global $testAuth;
26-
$pfop = new PersistentFop($testAuth, 'testres', 'sdktest', true);
27-
$options = array(
28-
'segtime' => 10,
29-
'vcodec' => 'libx264',
30-
's' => '320x240'
31-
);
32-
list($id, $error) = $pfop->avthumb('sintel_trailer.mp4', 'm3u8', $options, 'phpsdk', 'avthumtest');
33-
$this->assertNull($error);
34-
list($status, $error) = PersistentFop::status($id);
35-
$this->assertNotNull($status);
36-
$this->assertNull($error);
37-
}
3824

3925
public function testExecute2()
4026
{
4127
global $testAuth;
42-
$pfop = new PersistentFop($testAuth, 'testres', 'sdktest', true);
43-
$url_src1 = 'http://testres.qiniudn.com/gogopher.jpg';
44-
$url_en1 = \Qiniu\base64_urlSafeEncode($url_src1);
45-
$url_alias_en1 = \Qiniu\base64_urlSafeEncode('g.jpg');
46-
$url_en2 = $url_en1;
47-
$fop = "mkzip/2/url/$url_en1/alias/$url_alias_en1/url/$url_en2";
48-
$op = Operation::saveas($fop, 'phpsdk', 'mkziptest');
49-
$ops = array();
50-
array_push($ops, $op);
51-
list($id, $error) = $pfop->execute('sintel_trailer.mp4', $ops);
52-
$this->assertNull($error);
53-
list($status, $error) = PersistentFop::status($id);
54-
$this->assertNotNull($status);
55-
$this->assertNull($error);
56-
}
28+
$bucket = 'testres';
29+
$key = 'sintel_trailer.mp4';
30+
$fops = array(
31+
'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240',
32+
'vframe/jpg/offset/7/w/480/h/360',
33+
);
34+
$pfop = new PersistentFop($testAuth, $bucket);
5735

58-
public function testMkzip()
59-
{
60-
global $testAuth;
61-
$pfop = new PersistentFop($testAuth, 'testres', 'sdktest', true);
62-
$urls = array(
63-
'http://testres.qiniudn.com/gogopher.jpg' => 'g.jpg',
64-
'http://testres.qiniudn.com/gogopher.jpg'
65-
);
66-
list($id, $error) = $pfop->mkzip('sintel_trailer.mp4', $urls, 'phpsdk', 'mkziptest2.zip');
36+
list($id, $error) = $pfop->execute($key, $fops);
6737
$this->assertNull($error);
6838
list($status, $error) = PersistentFop::status($id);
6939
$this->assertNotNull($status);

0 commit comments

Comments
 (0)