Source code raw phps
<?php
/*
+----------------------------------------------------------------------+
| PHP Version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 2005 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Etienne Kneuss <colder@php.net> |
+----------------------------------------------------------------------+
$Id:$
*/
/**
* Generate .phpt files out of doc examples
*/
if (PHP_SAPI !== 'cli') {
echo "This script is ment to be run under CLI\n";
exit(1);
}
if ($_SERVER['argc'] == 2 &&
in_array($_SERVER['argv'][1], array('--help', '-help', '-h', '-?'))) {
echo "Generate .phpt files out of documentation examples\n\n";
echo "Usage: {$_SERVER['argv'][0]} [--quiet] <reference> [<reference> [...]]\n";
echo " --help, -help, -h, -? - to get this help\n";
die;
}
/**
* Configuration
*/
define('EXAMPLE_REGEX', '#<example>\s+<title>(.+)</title>\s+<programlisting role="php">\s+<!\[CDATA\[\s+(.+)\s+]]>\s+</programlisting>\s+</example>#isU');
define('TEST_FORMAT', "--TEST--\r\n".
"%s (generated)\r\n".
"--FILE--\r\n".
"%s\r\n".
"--EXPECT--\r\n");
define('TEST_PATH', 'tests/%s-%s-%d.phpt');
if (isset($_SERVER['argv'][1]) && in_array($_SERVER['argv'][1], array('-q', '--quiet'))) {
unset($_SERVER['argv'][1]);
define('VERBOSE', false);
} else {
define('VERBOSE', true);
}
$docLocation = 'en/reference/';
$genExamples = 0;
/**
* Generating examples
*/
if (!is_dir($docLocation)) {
echo "Can't file documentation files, be sure you're in phpdoc/\n";
exit(1);
}
function listFiles($path, $reference) {
if (is_dir($path) && is_resource($handle = @opendir($path))) {
while ($name = readdir($handle)) {
if (strpos($name, ".xml") !== false) {
generateTests($path.$name, $reference);
} else if(is_dir($path.$name) && $name !== 'CVS' && $name !== '.' && $name !== '..') {
listFiles($path.$name.DIRECTORY_SEPARATOR, $reference);
}
}
closedir($handle);
return true;
} else {
printf(" Unable to open %s\n", $path);
return false;
}
}
function generateTests($path, $reference) {
if (!is_file($path)) {
return false;
}
$content = file_get_contents($path);
if ($number = preg_match_all(EXAMPLE_REGEX, $content, $matches)) {
// found an example
foreach ($matches[2] as $key => $example) {
$title = preg_replace('#<function>([\w_-]+)</function>#', '\1()', $matches[1][$key]);
$testContent = sprintf(TEST_FORMAT, $title, $example);
if (!file_put_contents(sprintf(TEST_PATH, $reference, basename($path), $key+1), $testContent)) {
$number--;
}
}
$GLOBALS['genExamples'] += $number;
if ($number && VERBOSE) {
printf(" %2d example%s found in %-70s\n", $number, $number>1?'s':' ', $path);
}
}
}
// Begin the listing
echo "Searching for examples:\n";
foreach($_SERVER['argv'] as $key => $reference) {
if ($key) {
echo ' Looking in '.$docLocation.$reference."\n";
listFiles($docLocation.$reference.'/', $reference);
}
}
echo "Results:\n";
if ($genExamples) {
echo " Generated $genExamples examples\n";
} else {
echo " Failed to generate examples\n";
}
Comments
There is currently no comment here.