make mime_type_plaindetect work in .phar files
authorChristian Weiske <cweiske@cweiske.de>
Fri, 4 Jul 2014 05:39:32 +0000 (07:39 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 4 Jul 2014 05:39:32 +0000 (07:39 +0200)
src/phorkie/Repository/Post.php
src/phorkie/Tool/MIME/Type/PlainDetect.php [new file with mode: 0644]

index c9f6a53..fbfea4b 100644 (file)
@@ -232,7 +232,7 @@ class Repository_Post
     {
         $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
         file_put_contents($tmp, $content);
-        $type = \MIME_Type_PlainDetect::autoDetect($tmp);
+        $type = Tool_MIME_Type_PlainDetect::autoDetect($tmp);
         unlink($tmp);
 
         return $this->findExtForType($type);
diff --git a/src/phorkie/Tool/MIME/Type/PlainDetect.php b/src/phorkie/Tool/MIME/Type/PlainDetect.php
new file mode 100644 (file)
index 0000000..235046e
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+namespace phorkie;
+
+class Tool_MIME_Type_PlainDetect extends \MIME_Type_PlainDetect
+{
+    /**
+     * Try to find the magic file copied by phing's build.xml into
+     * lib/data/.
+     *
+     * @return string path to the magic file
+     */
+    public static function getMagicFile()
+    {
+        $rootdir = __DIR__ . '/../../../../../';
+        if (file_exists($rootdir . '/lib/PEAR.php')) {
+            if (!\Phar::running()) {
+                return $rootdir . '/lib/data/programming.magic';
+            } else {
+                //magic file within a .phar does not work:
+                // https://bugs.php.net/bug.php?id=67556
+                //put it outside
+                $target = '/tmp/phorkie-programming.magic';
+                if (!file_exists($target)) {
+                    copy(
+                        $rootdir . '/lib/data/programming.magic',
+                        $target
+                    );
+                }
+                return $target;
+            }
+        }
+        return parent::getMagicFile();
+    }
+}
+?>