-
Notifications
You must be signed in to change notification settings - Fork 0
/
genClass.php
62 lines (51 loc) · 2.25 KB
/
genClass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* File to generate class files for Project Euler Exercises
*
* @category ProjectEuler
* @package default
* @author Michael Reeves <mike.reeves@gmail.com>
* @link http://projecteuler.net
*
*/
define('PE_BASE_URL', 'http://projecteuler.net/problem=');
define('CLASS_DIR', 'includes/classes/');
define('CLASS_TEMPLATE_FILE', 'ProblemTemplate.php');
define('TOKEN_PROB_NUMBER', '%%PROB_NUMBER%%');
define('TOKEN_PROB_DESC_SHORT', '%%PROB_DESC_SHORT%%');
define('TOKEN_PROB_DESC_LONG', '%%PROB_DESC_LONG%%');
if (isset($argv) && isset($argv[1]) && is_numeric($argv[1])) {
$projectNumber = $argv[1];
$newFile = CLASS_DIR.'Problem'.$projectNumber.'.php';
$templateFile = CLASS_DIR.CLASS_TEMPLATE_FILE;
if (file_exists($newFile)) {
echo "Class file ".$newFile." already exists. Exiting. \n";
} else {
$input = file_get_contents($templateFile);
$peHtml = file_get_contents(PE_BASE_URL.$projectNumber); // TODO: add checks for failures
$dom = new DOMDocument();
$dom->loadHTML($peHtml);
$h2Elements = $dom->getElementsByTagName('h2');
$projectDescShort = $h2Elements->item(0)->nodeValue;
// <div class="problem_content" role="problem">
$divElements = $dom->getElementsByTagName('div');
foreach ($divElements as $divElem) {
if ($divElem->getAttribute('class') == "problem_content" && $divElem->getAttribute('role') == "problem") {
$projectDescLong = trim($divElem->nodeValue);
$projectDescLong = str_replace("\n", "\n * ", $projectDescLong);
}
}
$output = str_replace(TOKEN_PROB_NUMBER, $projectNumber, $input);
$output = str_replace(TOKEN_PROB_DESC_SHORT, $projectDescShort, $output);
$output = str_replace(TOKEN_PROB_DESC_LONG, $projectDescLong, $output);
$writeResult = file_put_contents($newFile, $output);
if ($writeResult === false) {
// echo $output;
echo "Write of Problem # ".$projectNumber ." to " . $newFile . " failed!\n";
} else {
echo "Write of Problem # ".$projectNumber ." to " . $newFile . " succeeded!\n";
}
}
} else {
echo "Please enter which Project Euler project number you wish to generate a class for?\n";
}