5 * Abstract command class that handles generic properties
7 class Command_AbstractProps
9 public static function optsGeneric(\Console_CommandLine_Command $cmd)
15 'long_name' => '--category',
16 'description' => 'Category names',
18 'action' => 'StoreArray',
26 'long_name' => '--files',
27 'description' => 'Files or URLs to upload',
28 'help_name' => 'PATH',
29 'action' => 'StoreArray',
36 'long_name' => '--published',
37 'description' => 'Publish date',
38 'help_name' => 'DATE',
39 'action' => 'StoreString',
47 'long_name' => '--slug',
48 'description' => 'URL path',
49 'help_name' => 'PATH',
50 'action' => 'StoreString',
58 'long_name' => '--syndication',
59 'description' => 'Syndication URL(s)',
61 'action' => 'StoreArray',
69 'long_name' => '--xprop',
70 'description' => 'Additional property',
71 'help_name' => 'key=value',
72 'action' => 'StoreArray',
78 protected function handleGenericOptions(
79 \Console_CommandLine_Result $cmdRes, Request $req
81 if ($cmdRes->options['published'] !== null) {
82 $req->req->addPostParameter(
83 'published', $cmdRes->options['published']
86 if (count($cmdRes->options['categories'])) {
87 $req->addPostParameter(
88 'category', $cmdRes->options['categories']
91 if ($cmdRes->options['slug'] !== null) {
92 $req->req->addPostParameter(
93 'slug', $cmdRes->options['slug']
96 if (count($cmdRes->options['syndication'])) {
97 $req->addPostParameter(
98 'syndication', $cmdRes->options['syndication']
102 $this->handleFiles($cmdRes, $req);
104 if (count($cmdRes->options['x'])) {
106 foreach ($cmdRes->options['x'] as $xproperty) {
107 list($propkey, $propval) = explode('=', $xproperty, 2);
108 if (!isset($postParams[$propkey] )) {
109 $postParams[$propkey] = [];
111 $postParams[$propkey][] = $propval;
113 foreach ($postParams as $propkey => $propvals) {
114 $req->addPostParameter($propkey, $propvals);
119 protected function handleFiles(
120 \Console_CommandLine_Result $cmdRes, Request $req
122 $files = $cmdRes->options['files'];
123 $fileList = $urlList = [
129 foreach ($files as $filePath) {
130 if (strpos($filePath, '://') !== false) {
132 $mte = new \MIME_Type_Extension();
133 $mimetype = $mte->getMIMEType($filePath);
134 $media = \MIME_Type::getMedia($mimetype);
135 if (!isset($urlList[$media])) {
136 Log::err('File type not allowed: ' . $mimetype);
139 $urlList[$media][] = $filePath;
140 } else if (file_exists($filePath)) {
142 $mimetype = \MIME_Type::autoDetect($filePath);
143 $media = \MIME_Type::getMedia($mimetype);
144 if (!isset($urlList[$media])) {
145 Log::err('File type not allowed: ' . $mimetype);
148 $fileList[$media][] = $filePath;
150 Log::err('File does not exist: ' . $filePath);
154 foreach ($urlList as $type => $urls) {
155 if ($type == 'image') {
158 if (count($urls) == 1) {
159 $req->req->addPostParameter($type, reset($urls));
160 } else if (count($urls) > 1) {
162 foreach ($urls as $url) {
163 $req->req->addPostParameter(
164 $type . '[' . $n++ . ']', $url
169 foreach ($fileList as $type => $filePaths) {
170 if ($type == 'image') {
173 if (count($filePaths) == 1) {
174 $req->addUpload($type, reset($filePaths));
175 } else if (count($filePaths) > 0) {
176 $req->addUpload($type, $filePaths);