summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUrban Müller2017-03-15 15:26:58 +0100
committerUrban Müller2017-03-15 15:26:58 +0100
commita833ef8465c738d117d2f72dab588f04bc0982ce (patch)
tree09eae89bc0a03a08bef3c339d0d623acf7242186
parenta29ada8124f7ea5bbad4387dd908eeaa57b1957c (diff)
downloaditools-a833ef8465c738d117d2f72dab588f04bc0982ce.tar.gz
itools-a833ef8465c738d117d2f72dab588f04bc0982ce.tar.bz2
itools-a833ef8465c738d117d2f72dab588f04bc0982ce.zip
add add_dir(), add mkdir to file_put
-rw-r--r--it.class12
-rwxr-xr-xtests/it.t4
2 files changed, 16 insertions, 0 deletions
diff --git a/it.class b/it.class
index 55d57db..30dbf92 100644
--- a/it.class
+++ b/it.class
@@ -985,6 +985,14 @@ static function mail($p)
}
/**
+ * Add an extra md5 based directory name on bottom of path. foo/bar -> foo/07/bar
+ */
+static function add_dir($path)
+{
+ return dirname($path) . "/" . substr(md5(basename($path)), 0, 2) . "/" . basename($path);
+}
+
+/**
* Reads a file and returns it as string or in one of several formats. Two params: filename and flags
* @param $filename name of file to read or - for stdin
* @param $p['keyval'] each line of the file is one tab-separated key/value pair, return assoc array
@@ -1017,6 +1025,7 @@ static function file_get($filename, $p = array())
* @param $data data to write, string by default
* @param $p['keyval'] $data must be an assoc array and is written as tab-separated lines
* @param $p['lines'] write array of lines, appending newline
+ * @param $p['mkdir'] create parent directory (one level)
*/
static function file_put($filename, $data, $p = array())
{
@@ -1025,6 +1034,9 @@ static function file_put($filename, $data, $p = array())
else if ($p['lines'])
$data = count((array)$data) ? join("\n", (array)$data) ."\n" : "";
+ if ($p['mkdir'] && $filename != "-")
+ @mkdir(dirname($filename));
+
if (($result = file_put_contents($filename == "-" ? "php://stdout" : "$filename.tmp." . getmypid(), $data)) !== false && $filename != "-")
$result = rename("$filename.tmp." . getmypid(), $filename);
diff --git a/tests/it.t b/tests/it.t
index e028f27..0c02106 100755
--- a/tests/it.t
+++ b/tests/it.t
@@ -438,3 +438,7 @@ is(it::map(42, array("aaa", "aa")), array(42, 42));
is(it::map('2*$v', [3, 4, 5], ['keys' => '0,1']), [6, 8, 5]);
is(it::map('2*$v', [3, 4, 5], ['keys' => [0,1]]), [6, 8, 5]);
is(it::map('2*$v', ['foo' => 1, 'bar' => 2], ['keys' => 'foo']), ['foo' => 2, 'bar' => 2]);
+
+is(it::add_dir("foo/bar"), "foo/37/bar");
+is(it::add_dir("baz/bar"), "baz/37/bar");
+is(it::add_dir("bar"), "./37/bar");