1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Changes the attributes of a file or all files inside specified directories.
22
 * Right now it has effect only under Windows. Each of the 4 possible
23
 * permissions has its own attribute, matching the arguments for the `attrib`
24
 * command.
25
 *
26
 * Example:
27
 * ```
28
 *    <attrib file="${input}" readonly="true" hidden="true" verbose="true"/>
29
 * ```
30
 *
31
 * @author  Siad Ardroumli <siad.ardroumli@gmail.com>
32
 * @package phing.tasks.system
33
 */
34
class AttribTask extends ApplyTask
35
{
36
    private static $ATTR_READONLY = 'R';
37
    private static $ATTR_ARCHIVE = 'A';
38
    private static $ATTR_SYSTEM = 'S';
39
    private static $ATTR_HIDDEN = 'H';
40
    private static $SET = '+';
41
    private static $UNSET = '-';
42

43
    private $attr = false;
44

45 0
    public function init()
46
    {
47 0
        parent::init();
48 0
        parent::setExecutable('attrib');
49 0
        parent::setParallel(false);
50
    }
51

52
    /**
53
     * @throws BuildException
54
     */
55 0
    public function main()
56
    {
57 0
        $this->checkConfiguration();
58 0
        parent::main();
59
    }
60

61
    /**
62
     * @param bool $b
63
     */
64 0
    public function setVerbose($b)
65
    {
66 0
        $this->loglevel = Project::MSG_VERBOSE;
67
    }
68

69
    /**
70
     * A file to be attribed.
71
     *
72
     * @param PhingFile $src a file
73
     */
74 0
    public function setFile(PhingFile $src)
75
    {
76 0
        $fs = new FileSet();
77 0
        $fs->setFile($src);
78 0
        $this->addFileSet($fs);
79
    }
80

81
    /**
82
     * Set the ReadOnly file attribute.
83
     *
84
     * @param boolean $value
85
     */
86 0
    public function setReadonly($value)
87
    {
88 0
        $this->addArg($value, self::$ATTR_READONLY);
89
    }
90

91
    /**
92
     * Set the Archive file attribute.
93
     *
94
     * @param boolean $value
95
     */
96 0
    public function setArchive($value)
97
    {
98 0
        $this->addArg($value, self::$ATTR_ARCHIVE);
99
    }
100

101
    /**
102
     * Set the System file attribute.
103
     *
104
     * @param boolean $value
105
     */
106 0
    public function setSystem($value)
107
    {
108 0
        $this->addArg($value, self::$ATTR_SYSTEM);
109
    }
110

111
    /**
112
     * Set the Hidden file attribute.
113
     *
114
     * @param boolean $value
115
     */
116 0
    public function setHidden($value)
117
    {
118 0
        $this->addArg($value, self::$ATTR_HIDDEN);
119
    }
120

121
    /**
122
     * Check the attributes.
123
     *
124
     * @throws BuildException
125
     */
126 0
    protected function checkConfiguration()
127
    {
128 0
        if (!$this->hasAttr()) {
129 0
            throw new BuildException(
130 0
                'Missing attribute parameter',
131 0
                $this->getLocation()
132
            );
133
        }
134
    }
135

136
    /**
137
     * Set the executable.
138
     * This is not allowed, and it always throws a BuildException.
139
     *
140
     * @param  mixed $e
141
     * @throws BuildException
142
     */
143 0
    public function setExecutable($e): void
144
    {
145 0
        throw new BuildException(
146 0
            $this->getTaskType() . ' doesn\'t support the executable attribute',
147 0
            $this->getLocation()
148
        );
149
    }
150

151
    /**
152
     * Add source file.
153
     * This is not allowed, and it always throws a BuildException.
154
     *
155
     * @param  boolean $b ignored
156
     * @throws BuildException
157
     */
158 0
    public function setAddsourcefile(bool $b)
159
    {
160 0
        throw new BuildException(
161 0
            $this->getTaskType()
162 0
            . ' doesn\'t support the addsourcefile attribute',
163 0
            $this->getLocation()
164
        );
165
    }
166

167
    /**
168
     * Set max parallel.
169
     * This is not allowed, and it always throws a BuildException.
170
     *
171
     * @param  int $max ignored
172
     * @throws BuildException
173
     */
174 0
    public function setMaxParallel($max)
175
    {
176 0
        throw new BuildException(
177 0
            $this->getTaskType()
178 0
            . ' doesn\'t support the maxparallel attribute',
179 0
            $this->getLocation()
180
        );
181
    }
182

183
    /**
184
     * Set parallel.
185
     * This is not allowed, and it always throws a BuildException.
186
     *
187
     * @param  boolean $parallel ignored
188
     * @throws BuildException
189
     */
190 0
    public function setParallel(bool $parallel)
191
    {
192 0
        throw new BuildException(
193 0
            $this->getTaskType()
194 0
            . ' doesn\'t support the parallel attribute',
195 0
            $this->getLocation()
196
        );
197
    }
198

199 0
    private static function getSignString($attr)
200
    {
201 0
        return ($attr ? self::$SET : self::$UNSET);
202
    }
203

204 0
    private function addArg($sign, $attribute)
205
    {
206 0
        $this->createArg()->setValue(self::getSignString($sign) . $attribute);
207 0
        $this->attr = true;
208
    }
209

210
    /**
211
     * @return bool
212
     */
213 0
    private function hasAttr()
214
    {
215 0
        return $this->attr;
216
    }
217
}

Read our documentation on viewing source code .

Loading