Completed
Pull Request — master (#7)
by Markus
06:25
created

OlsonCompileTask::setOlsonDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
// +---------------------------------------------------------------------------+
4
// | This file is part of the Agavi package.                                   |
5
// | Copyright (c) 2005-2011 the Agavi Project.                                |
6
// |                                                                           |
7
// | For the full copyright and license information, please view the LICENSE   |
8
// | file that was distributed with this source code. You can also view the    |
9
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
10
// |   vi: set noexpandtab:                                                    |
11
// |   Local Variables:                                                        |
12
// |   indent-tabs-mode: t                                                     |
13
// |   End:                                                                    |
14
// +---------------------------------------------------------------------------+
15
16
class OlsonCompileTask extends Task
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
	private $olsonDir = '';
19
	private $outputDir = '';
20
21
	public function setOlsonDir($olsonDir)
22
	{
23
		$this->olsonDir = (string) $olsonDir;
24
	}
25
26
	public function setOutputDir($outputDir)
27
	{
28
		$this->outputDir = (string) $outputDir;
29
	}
30
31
	public function main()
32
	{
33
		set_time_limit(0);
34
35
		require_once('src/agavi.php');
36
37
		$this->olsonDir = realpath($this->olsonDir);
38
		$this->outputDir = realpath($this->outputDir);
39
40
		Config::set('olson.dir', $this->olsonDir);
41
42
		Config::set('core.app_dir', getcwd() . '/etc/olson/agavi/app');
43
		Agavi::bootstrap('');
44
45
		$context = AgaviContext::getInstance('');
46
47
		if(!$this->olsonDir || !file_exists($this->olsonDir)) {
48
			throw new BuildException('Olson data directory is not defined or does not exist.');
49
		}
50
51
		if(!$this->outputDir || !file_exists($this->outputDir)) {
52
			throw new BuildException('Timezone data output directory is not defined or does not exist.');
53
		}
54
55
		$this->log("Building compiling olson files in {$this->olsonDir} to {$this->outputDir}", PROJECT_MSG_INFO);
56
57
		$links = array();
58
		$zones = array();
59
60
		$di = new DirectoryIterator($this->olsonDir);
61
		foreach($di as $file) {
62
			if($file->isFile()) {
63
				// the file doesn't contain an extension so we parse it
64
				// and we don't want the factory time zone
65
				if(strpos($file->getFilename(), '.') === false && $file->getFilename() != 'factory') {
66
					$this->log(sprintf('compiling %s', $file->getPathname()), PROJECT_MSG_INFO);
67
					$parser = new TimeZoneDataParser();
68
					$parser->initialize(AgaviContext::getInstance($context));
69
					$rules = $parser->parse($file->getPathname());
70
					$zones = $rules['zones'] + $zones;
71
					$links = $rules['links'] + $links;
72
				}
73
			}
74
		}
75
76
		$baseCode = '<?php
77
78
/**
79
 * %s
80
 * %s
81
 *
82
 * @package    agavi
83
 * @subpackage translation
84
 *
85
 * @copyright  Authors
86
 * @copyright  The Agavi Project
87
 *
88
 * @since      0.11.0
89
 *
90
 * @version    $Id' . '$
91
 */
92
93
return %s;
94
95
?>';
96
97
		$zoneList = array();
98
99
		foreach($zones as $name => $zone) {
100
			$fname = preg_replace('#([^a-z0-9_])#ie', "'_'.ord('\\1').'_'", $name) . '.php';
101
			$pathname = $this->outputDir . '/' . $fname;
102
			$zone['name'] = $name;
103
104
			$zoneList[$name] = array('type' => 'zone', 'filename' => $fname);
105
			$this->log('Writing zone ' . $name . ' to: ' . $pathname);
106
			file_put_contents(
107
				$pathname,
108
				sprintf(
109
					$baseCode,
110
					sprintf(
111
						'Data file for timezone "%s".',
112
						$name
113
					),
114
					sprintf(
115
						'Compiled from olson file "%s", version %s.',
116
						$zone['source'],
117
						$zone['version']
118
					),
119
					var_export($zone, true)
120
				)
121
			);
122
		}
123
124
		foreach($links as $from => $to) {
125
			$zoneList[$from] = array('type' => 'link', 'to' => $to);
126
		}
127
128
		$this->log('Writing zone listing to: ' . $this->outputDir . '/zonelist.php');
129
		file_put_contents(
130
			$this->outputDir . '/zonelist.php',
131
			sprintf(
132
				$baseCode,
133
				'Zone list file.',
134
				sprintf('Generated on %s.', gmdate('c')),
135
				var_export($zoneList, true)
136
			)
137
		);
138
	}
139
}
140
141
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...