Collecting database file operations
Issue #208
open
Originally reported on Google Code with ID 208
The code relative to the $database array is spread a bit here and there: main flow,
dir_tree(), checkDbName(), isManagedDb...
I've tried to collect *most* of the relative operations in a class representing a list
of database files. Attached is a partial version, I still need to add read/write checks
and some lists.
The functions above are now methods: addDirectory, canCreate, contains. There are also
more ways to fill the set: a single filename, a directory, and the current configuration
format.
The second file attached is a small example/test:
trunk/classes/DatabaseSet.php
trunk/test-dbs.php
Any feedback or ideas around this part of the code?
Reported by dreadnaut
on 2013-04-04 19:16:17
<hr> * Attachment: DatabaseSet.php * Attachment: test-dbs.php
Comments (3)
-
-
(Maybe I shouldn't start too many discussions like this about 20 LOC methods that do what they are supposed to only because I would have implemented them differently...)
Reported by
crazy4chrissi
on 2013-04-26 20:58:05 -
reporter I like these discussions, usually better code comes out of them :) I'd love to use DirectoryIterator (and RecursiveDirectoryIterator, and RecursiveIteratorIterator to avoid recursion or queues) but I find them actually less readable, and they feel to expensive for what we are doing. Things get a bit better with FilesystemIterator, which however requires 5.3.0+. For example, RecursiveDirectoryIterator allows you to specify the SKIP_DOTS flag, while DirectoryIterator doesn't —so long, code reuse. At that point we need to call ->isDot() anyway to check —at that point, let's stick to opendir(). Plus, a bunch of objects allocated to list a few files, each one opening and closing a file handle —vs opening the directory and calling stat() a few times. Readability, er... starting to look like Java :D try { if ($recursive) { $iter = new RecursiveIteratorIterator(new DirectoryRecursiveIterator($path, FilesystemIterator::SKIP_DOTS)); } else { $iter = new DirectoryIterator($path /* sorry, no flags accepted here */); } } catch (UnexpectedValueException $e) { return false; } foreach ($iter as $file) { $filename = $file->getFilename(); /* same code anyway */ } If glob is a problem, I'd probably use opendir() at that point. The SPL is usually nice, but I'm not sold on these classes. Apart from this, I've changed the interface a bit compared to the initial code, and I can now accept these formats: /* configuration examples */ $databases_example = array ( // a single database 'dir/storage.sqlite', // a single database, with an alias 'ShortName' => 'mystuff/database_with_a_long_name.sqlite', // a single database, with an alias (long form, current configuration style) array( 'path' => 'directory/something.sqlite', 'name' => 'My Something DB', ), // specific files in a directory 'some_directory/*.{sqlite,db}', // all the files in a directory 'some_directory/', // all the files in a directory, recursively array( 'path' => 'just_a_directory/', 'recursive' => true, ), ); But I was wondering: which other operations would we need? Does it make sense to include deleting/renaming files here?
Reported by
dreadnaut
on 2013-05-01 16:28:19 - Log in to comment
Reported by
crazy4chrissi
on 2013-04-26 20:54:01