Archive_Core::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 2
b 1
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php defined('SYSPATH') or die('No direct access allowed.');
2
/**
3
 * Archive library.
4
 *
5
 * $Id: Archive.php 4367 2009-05-27 21:23:57Z samsoir $
6
 *
7
 * @package    Archive
8
 * @author     Kohana Team
9
 * @copyright  (c) 2007-2008 Kohana Team
10
 * @license    http://kohanaphp.com/license.html
11
 */
12
class Archive_Core
13
{
14
15
    // Files and directories
16
    protected $paths;
17
18
    // Driver instance
19
    protected $driver;
20
21
    /**
22
     * Loads the archive driver.
23
     *
24
     * @throws  Kohana_Exception
25
     * @param   string   type of archive to create
26
     * @return  void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
27
     */
28
    public function __construct($type = null)
29
    {
30
        $type = empty($type) ? 'zip' : $type;
31
32
        // Set driver name
33
        $driver = 'Archive_'.ucfirst($type).'_Driver';
34
35
        // Load the driver
36
        if (! Kohana::auto_load($driver)) {
37
            throw new Kohana_Exception('core.driver_not_found', $type, get_class($this));
38
        }
39
40
        // Initialize the driver
41
        $this->driver = new $driver();
42
43
        // Validate the driver
44
        if (! ($this->driver instanceof Archive_Driver)) {
45
            throw new Kohana_Exception('core.driver_implements', $type, get_class($this), 'Archive_Driver');
46
        }
47
48
        Kohana::log('debug', 'Archive Library initialized');
49
    }
50
51
    /**
52
     * Adds files or directories, recursively, to an archive.
53
     *
54
     * @param   string   file or directory to add
55
     * @param   string   name to use for the given file or directory
56
     * @param   bool     add files recursively, used with directories
57
     * @return  Archive_Core
58
     */
59
    public function add($path, $name = null, $recursive = null)
60
    {
61
        // Normalize to forward slashes
62
        $path = str_replace('\\', '/', $path);
63
64
        // Set the name
65
        empty($name) and $name = $path;
66
67
        if (is_dir($path)) {
68
            // Force directories to end with a slash
69
            $path = rtrim($path, '/').'/';
70
            $name = rtrim($name, '/').'/';
71
72
            // Add the directory to the paths
73
            $this->paths[] = array($path, $name);
74
75
            if ($recursive === true) {
76
                $dir = opendir($path);
77
                while (($file = readdir($dir)) !== false) {
78
                    // Do not add hidden files or directories
79
                    if ($file[0] === '.') {
80
                        continue;
81
                    }
82
83
                    // Add directory contents
84
                    $this->add($path.$file, $name.$file, true);
85
                }
86
                closedir($dir);
87
            }
88
        } else {
89
            $this->paths[] = array($path, $name);
90
        }
91
92
        return $this;
93
    }
94
95
    /**
96
     * Creates an archive and saves it into a file.
97
     *
98
     * @throws  Kohana_Exception
99
     * @param   string   archive filename
100
     * @return  boolean
101
     */
102
    public function save($filename)
103
    {
104
        // Get the directory name
105
        $directory = pathinfo($filename, PATHINFO_DIRNAME);
106
107
        if (! is_writable($directory)) {
108
            throw new Kohana_Exception('archive.directory_unwritable', $directory);
109
        }
110
111
        if (is_file($filename)) {
112
            // Unable to write to the file
113
            if (! is_writable($filename)) {
114
                throw new Kohana_Exception('archive.filename_conflict', $filename);
115
            }
116
117
            // Remove the file
118
            unlink($filename);
119
        }
120
121
        return $this->driver->create($this->paths, $filename);
122
    }
123
124
    /**
125
     * Creates a raw archive file and returns it.
126
     *
127
     * @return  string
128
     */
129
    public function create()
130
    {
131
        return $this->driver->create($this->paths);
132
    }
133
134
    /**
135
     * Forces a download of a created archive.
136
     *
137
     * @param   string   name of the file that will be downloaded
138
     * @return  void
139
     */
140
    public function download($filename)
141
    {
142
        download::force($filename, $this->driver->create($this->paths));
143
    }
144
} // End Archive
145