1 | <?php |
||
9 | class ar_store_files extends arBase { |
||
10 | |||
11 | protected static function parseName( $fname ) { |
||
12 | list( $nls, $name ) = explode('_', $fname, 2); |
||
13 | return array( |
||
14 | 'nls' => $nls, |
||
15 | 'name' => $name |
||
16 | ); |
||
17 | } |
||
18 | |||
19 | protected static function compileName( $name, $nls ) { |
||
20 | if ( !$nls ) { |
||
21 | $ob = ar::context()->getObject(); |
||
22 | $nls = $ob->nls; |
||
23 | } |
||
24 | return $nls.'_'.$name; |
||
25 | } |
||
26 | |||
27 | protected static function getStore() { |
||
28 | $ob = ar::context()->getObject(); |
||
29 | return array( $ob, $ob->store->get_filestore("files") ); |
||
30 | } |
||
31 | |||
32 | public static function ls( $nls=null ) { |
||
33 | list($ob, $fstore) = static::getStore(); |
||
34 | $files = $fstore->ls($ob->id); |
||
35 | if ( !$files ) { |
||
36 | $files = array(); |
||
37 | } |
||
38 | $files = array_map( array('self','parseName'), $files ); |
||
39 | if ( isset($nls) ) { |
||
40 | $files = array_filter( $files, function($f) use($nls) { |
||
41 | return ( $f['nls'] == $nls ); |
||
42 | } ); |
||
43 | } |
||
44 | return $files; |
||
45 | } |
||
46 | |||
47 | public static function get( $name, $nls=null ) { |
||
48 | $info = static::exists($name, $nls); |
||
49 | if ( !$info ) { |
||
50 | return ar_error::raiseError('File not found: '.$name.' - '.$nls, 404); |
||
51 | } |
||
52 | list( $ob, $fstore ) = static::getStore(); |
||
53 | $fname = static::compileName($info['name'], $info['nls']); |
||
54 | $stream = $fstore->get_stream($ob->id, $fname); |
||
55 | return new ar_content_filesFile( $stream ); |
||
56 | } |
||
57 | |||
58 | public static function save( $name, $contents, $nls=null ) { |
||
59 | list( $ob, $fstore ) = static::getStore(); |
||
60 | $fname = static::compileName($name, $nls); |
||
61 | if( $contents instanceof ar_content_filesFile ){ |
||
62 | // FIXME: this should be more efficient then coping the whole contents of the file in memory |
||
63 | // should be fixed with a copyFrom/copyTo function call on ar_content_filesFile |
||
64 | $contents = $contents->getContents(); |
||
65 | } |
||
66 | if ( is_resource($contents) && get_resource_type($contents)==='stream' ) { |
||
67 | return $fstore->copy_stream_to_store( $contents, $ob->id, $fname ); |
||
68 | } else { |
||
69 | return $fstore->write( (string) $contents, $ob->id, $fname ); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | public static function delete( $name, $nls=null ) { |
||
74 | list( $ob, $fstore ) = static::getStore(); |
||
75 | $fname = static::compileName($name, $nls); |
||
76 | if ( $fstore->exists( $ob->id, $fname ) ) { |
||
77 | return $fstore->remove( $ob->id, $fname ); |
||
78 | } |
||
79 | return false; |
||
80 | } |
||
81 | |||
82 | public static function touch( $name, $time=null, $nls=null ) { |
||
87 | |||
88 | public static function temp( $contents=null ) { |
||
89 | list( $ob, $fstore ) = static::getStore(); |
||
90 | $tmpsrc = tempnam($ob->store->get_config("files")."temp", "tmp"); |
||
91 | if ( !$tmpsrc ) { |
||
92 | // FIXME: nlsstrings |
||
93 | return ar_error::raiseError( 'Could not create temp file', 501 ); |
||
94 | } |
||
117 | |||
118 | public static function exists( $name, $nls=null ) { |
||
130 | |||
131 | } |
||
132 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.