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
 * Handler class for the <project> XML element This class handles all elements
22
 * under the <project> element.
23
 *
24
 * @author    Andreas Aderhold <andi@binarycloud.com>
25
 * @copyright (c) 2001,2002 THYRELL. All rights reserved
26
 * @package   phing.parser
27
 */
28
class ProjectHandler extends AbstractHandler
29
{
30

31
    /**
32
     * The phing project configurator object.
33
     *
34
     * @var ProjectConfigurator
35
     */
36
    private $configurator;
37

38
    /**
39
     * @var PhingXMLContext
40
     */
41
    private $context;
42

43
    /**
44
     * Constructs a new ProjectHandler
45
     *
46
     * @param ExpatParser $parser the ExpatParser object
47
     * @param AbstractHandler $parentHandler the parent handler that invoked this handler
48
     * @param ProjectConfigurator $configurator the ProjectConfigurator object
49
     * @param PhingXMLContext $context
50
     */
51 1
    public function __construct(
52
        ExpatParser $parser,
53
        AbstractHandler $parentHandler,
54
        ProjectConfigurator $configurator,
55
        PhingXMLContext $context
56
    ) {
57 1
        parent::__construct($parser, $parentHandler);
58

59 1
        $this->configurator = $configurator;
60 1
        $this->context = $context;
61
    }
62

63
    /**
64
     * Executes initialization actions required to setup the project. Usually
65
     * this method handles the attributes of a tag.
66
     *
67
     * @param  string $tag the tag that comes in
68
     * @param  array $attrs attributes the tag carries
69
     * @throws ExpatParseException if attributes are incomplete or invalid
70
     */
71 1
    public function init($tag, $attrs)
72
    {
73 1
        $def = null;
74 1
        $name = null;
75 1
        $id = null;
76 1
        $desc = null;
77 1
        $baseDir = null;
78 1
        $ver = null;
79 1
        $strict = null;
80

81
        // some shorthands
82 1
        $project = $this->configurator->project;
83 1
        $buildFileParent = $this->configurator->buildFileParent;
84

85 1
        foreach ($attrs as $key => $value) {
86 1
            if ($key === "default") {
87 1
                $def = $value;
88 1
            } elseif ($key === "name") {
89 1
                $name = $value;
90 1
            } elseif ($key === "id") {
91 0
                $id = $value;
92 1
            } elseif ($key === "basedir") {
93 1
                $baseDir = $value;
94 0
            } elseif ($key === "description") {
95 0
                $desc = $value;
96 0
            } elseif ($key === "phingVersion") {
97 0
                $ver = $value;
98 0
            } elseif ($key === 'strict') {
99 0
                $strict = $value;
100
            } else {
101 0
                throw new ExpatParseException("Unexpected attribute '$key'");
102
            }
103
        }
104
        // these things get done no matter what
105 1
        if (null == $name) {
106 1
            $name = basename($this->configurator->getBuildFile());
107
        }
108

109 1
        $canonicalName = self::canonicalName($name);
110 1
        $this->configurator->setCurrentProjectName($canonicalName);
111 1
        $path = (string) $this->configurator->getBuildFile();
112 1
        $project->setUserProperty("phing.file.{$canonicalName}", $path);
113 1
        $project->setUserProperty("phing.dir.{$canonicalName}", dirname($path));
114

115 1
        if ($this->configurator->isIgnoringProjectTag()) {
116 1
            return;
117
        }
118

119 1
        if ($def === null) {
120 0
            throw new ExpatParseException(
121 0
                "The default attribute of project is required"
122
            );
123
        }
124

125 1
        $project->setDefaultTarget($def);
126

127 1
        if ($name !== null) {
128 1
            $project->setName($name);
129 1
            $project->addReference($name, $project);
130
        }
131

132 1
        if ($id !== null) {
133 0
            $project->addReference($id, $project);
134
        }
135

136 1
        if ($desc !== null) {
137 0
            $project->setDescription($desc);
138
        }
139

140 1
        if ($ver !== null) {
141 0
            $project->setPhingVersion($ver);
142
        }
143

144 1
        if ($strict !== null) {
145 0
            $project->setStrictMode(StringHelper::booleanValue($strict));
146
        }
147

148 1
        if ($project->getProperty("project.basedir") !== null) {
149 1
            $project->setBasedir($project->getProperty("project.basedir"));
150
        } else {
151 1
            if ($baseDir === null) {
152 1
                $project->setBasedir($buildFileParent->getAbsolutePath());
153
            } else {
154
                // check whether the user has specified an absolute path
155 1
                $f = new PhingFile($baseDir);
156 1
                if ($f->isAbsolute()) {
157 0
                    $project->setBasedir($baseDir);
158
                } else {
159 1
                    $project->setBaseDir($project->resolveFile($baseDir, $buildFileParent));
160
                }
161
            }
162
        }
163

164 1
        $project->addTarget("", $this->context->getImplicitTarget());
165
    }
166

167
    /**
168
     * Handles start elements within the <project> tag by creating and
169
     * calling the required handlers for the detected element.
170
     *
171
     * @param  string $name the tag that comes in
172
     * @param  array $attrs attributes the tag carries
173
     * @throws ExpatParseException if a unxepected element occurs
174
     */
175 1
    public function startElement($name, $attrs)
176
    {
177 1
        $project = $this->configurator->project;
178 1
        $types = $project->getDataTypeDefinitions();
179

180 1
        if ($name === "target" || $name === "extension-point") {
181 1
            $tf = new TargetHandler($this->parser, $this, $this->configurator, $this->context);
182 1
            $tf->init($name, $attrs);
183
        } else {
184 1
            $tf = new ElementHandler(
185 1
                $this->parser,
186
                $this,
187 1
                $this->configurator,
188 1
                null,
189 1
                null,
190 1
                $this->context->getImplicitTarget()
191
            );
192 1
            $tf->init($name, $attrs);
193
        }
194
    }
195

196
    /**
197
     * @param $name
198
     * @return mixed
199
     */
200 1
    public static function canonicalName($name)
201
    {
202 1
        return preg_replace('/\W/', '_', strtolower($name));
203
    }
204
}

Read our documentation on viewing source code .

Loading