aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2012-04-13 19:56:24 +0200
committerChristian Weiske <cweiske@cweiske.de>2012-04-13 19:56:24 +0200
commitd9f6b83ed6d8a4546e4119c64b639adda057d25e (patch)
tree80e8a3077c149929072a70d71d523fd3fce929e2 /src
parent09b7728bd2beca1270922a38d79498e3c4fa0456 (diff)
downloadphorkie-d9f6b83ed6d8a4546e4119c64b639adda057d25e.tar.gz
phorkie-d9f6b83ed6d8a4546e4119c64b639adda057d25e.zip
first tool supported: xmllint
Diffstat (limited to 'src')
-rw-r--r--src/phorkie/File.php16
-rw-r--r--src/phorkie/Tool/Info.php33
-rw-r--r--src/phorkie/Tool/Manager.php46
-rw-r--r--src/phorkie/Tool/Result.php9
-rw-r--r--src/phorkie/Tool/Result/Line.php34
-rw-r--r--src/phorkie/Tool/Xmllint.php44
6 files changed, 180 insertions, 2 deletions
diff --git a/src/phorkie/File.php b/src/phorkie/File.php
index 87c3851..869aa80 100644
--- a/src/phorkie/File.php
+++ b/src/phorkie/File.php
@@ -75,14 +75,17 @@ class File
*
* @param string $type Link type. Supported are:
* - "raw"
- * - "display"
+ * - "tool"
+ * @param string $option
*
* @return string
*/
- public function getLink($type)
+ public function getLink($type, $option = null)
{
if ($type == 'raw') {
return '/' . $this->repo->id . '/raw/' . $this->getFilename();
+ } else if ($type == 'tool') {
+ return '/' . $this->repo->id . '/tool/' . $option . '/' . $this->getFilename();
}
throw new Exception('Unknown type');
}
@@ -95,6 +98,15 @@ class File
}
return $GLOBALS['phorkie']['languages'][$ext]['mime'];
}
+
+ /**
+ * @return array Array of Tool_Info objects
+ */
+ public function getToolInfos()
+ {
+ $tm = new Tool_Manager();
+ return $tm->getSuitable($this);
+ }
}
?> \ No newline at end of file
diff --git a/src/phorkie/Tool/Info.php b/src/phorkie/Tool/Info.php
new file mode 100644
index 0000000..c1c3c69
--- /dev/null
+++ b/src/phorkie/Tool/Info.php
@@ -0,0 +1,33 @@
+<?php
+namespace phorkie;
+
+class Tool_Info
+{
+ public $class;
+
+ public function __construct($class)
+ {
+ $this->class = $class;
+ }
+
+ public function getLink(File $file)
+ {
+ return $file->getLink('tool', $this->stripPrefix($this->class));
+ }
+
+ public function getTitle()
+ {
+ return $this->stripPrefix($this->class);
+ }
+
+ protected function stripPrefix($class)
+ {
+ $prefix = '\\phorkie\\Tool_';
+ if (substr($class, 0, strlen($prefix)) === $prefix) {
+ return substr($class, strlen($prefix));
+ }
+ return $class;
+ }
+}
+
+?>
diff --git a/src/phorkie/Tool/Manager.php b/src/phorkie/Tool/Manager.php
new file mode 100644
index 0000000..3bcf750
--- /dev/null
+++ b/src/phorkie/Tool/Manager.php
@@ -0,0 +1,46 @@
+<?php
+namespace phorkie;
+
+
+class Tool_Manager
+{
+ public function getSuitable(File $file)
+ {
+ $ext = $file->getExt();
+ $suitables = array();
+ foreach ($GLOBALS['phorkie']['tools'] as $class) {
+ if (array_search($ext, $class::$arSupportedExtensions) !== false) {
+ $suitables[] = new Tool_Info($class);
+ }
+ }
+ return $suitables;
+ }
+
+ /**
+ * Returns the class name from a tool name
+ *
+ * @param string $name Full class name or short name without
+ * 'phorkie\\Tool_' prefix
+ *
+ * @return string Class name or NULL if not found
+ */
+ public function getClass($name)
+ {
+ if (strpos($name, '\\') === false && strpos($name, '_') === false) {
+ return '\\phorkie\\Tool_' . $name;
+ }
+ return $name;
+ }
+
+ public function loadTool($name)
+ {
+ $class = $this->getClass($name);
+ if (!class_exists($class, true)) {
+ throw new Exception('Tool does not exist: ' . $class);
+ }
+
+ return new $class();
+ }
+}
+
+?> \ No newline at end of file
diff --git a/src/phorkie/Tool/Result.php b/src/phorkie/Tool/Result.php
new file mode 100644
index 0000000..22ea273
--- /dev/null
+++ b/src/phorkie/Tool/Result.php
@@ -0,0 +1,9 @@
+<?php
+namespace phorkie;
+
+class Tool_Result
+{
+ public $annotations;
+}
+
+?> \ No newline at end of file
diff --git a/src/phorkie/Tool/Result/Line.php b/src/phorkie/Tool/Result/Line.php
new file mode 100644
index 0000000..a788db6
--- /dev/null
+++ b/src/phorkie/Tool/Result/Line.php
@@ -0,0 +1,34 @@
+<?php
+namespace phorkie;
+
+class Tool_Result_Line
+{
+ public $message;
+ public $level;
+
+ public function __construct($message, $level = 'ok')
+ {
+ $this->message = $message;
+ $this->setLevel($level);
+ }
+
+ public function setLevel($level)
+ {
+ if ($level !== 'ok' && $level !== 'error' && $level !== 'warning') {
+ throw new Exception('Invalid result line level: ' . $level);
+ }
+ $this->level = $level;
+ }
+
+ public function getAlertLevel()
+ {
+ static $map = array(
+ 'error' => 'alert-error',
+ 'ok' => 'alert-success',
+ 'warning' => '',
+ );
+ return $map[$this->level];
+ }
+}
+
+?> \ No newline at end of file
diff --git a/src/phorkie/Tool/Xmllint.php b/src/phorkie/Tool/Xmllint.php
new file mode 100644
index 0000000..b9917f5
--- /dev/null
+++ b/src/phorkie/Tool/Xmllint.php
@@ -0,0 +1,44 @@
+<?php
+namespace phorkie;
+
+class Tool_Xmllint
+{
+ public static $arSupportedExtensions = array(
+ 'htm', 'html', 'xml'
+ );
+
+ public function run(File $file)
+ {
+ $fpath = $file->getPath();
+ $fpathlen = strlen($fpath);
+
+ $res = new Tool_Result();
+ $cmd = 'xmllint --noout ' . escapeshellarg($fpath) . ' 2>&1';
+ exec($cmd, $output, $retval);
+ if ($retval == 0) {
+ $res->annotations['general'][] = new Tool_Result_Line(
+ 'XML is well-formed', 'ok'
+ );
+ return $res;
+ }
+
+ for ($i = 0; $i < count($output); $i += 3) {
+ $line = $output[$i];
+ if (substr($line, 0, $fpathlen) != $fpath) {
+ throw new Exception('xmllint does not behave as expected: ' . $line);
+ }
+ list($line, $msg) = explode(':', substr($line, $fpathlen + 1), 2);
+ $res->annotations[$line][] = new Tool_Result_Line(
+ $msg, 'error'
+ );
+ }
+
+ $res->annotations['general'][] = new Tool_Result_Line(
+ 'XML is not well-formed', 'error'
+ );
+
+ return $res;
+ }
+}
+
+?> \ No newline at end of file