This repository was archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Added template map generator as bin script #65
Merged
weierophinney
merged 2 commits into
zendframework:develop
from
RalfEggert:feature/templatemap-generator
Jun 21, 2016
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,255 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zendframework for the canonical source repository | ||
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
use Zend\Console; | ||
|
||
/** | ||
* Generate template maps. | ||
* | ||
* Usage: | ||
* --help|-h Get usage message | ||
* --library|-l [ <string> ] Library to parse; if none provided, assumes | ||
* current directory | ||
* --view|-v [ <string> ] View path to parse; if none provided, assumes | ||
* view as template directory | ||
* --extensions|-e [ <string> ] List of accepted file extensions (regex alternation | ||
* without parenthesis); default: * | ||
* --output|-o [ <string> ] Where to write map file; if not provided, | ||
* assumes "template_map.php" in library directory | ||
* --append|-a Append to map file if it exists | ||
* --overwrite|-w Whether or not to overwrite existing map file | ||
*/ | ||
|
||
// Setup/verify autoloading | ||
if (file_exists(__DIR__ . '/../vendor/autoload.php')) { | ||
// Local install | ||
require __DIR__ . '/../vendor/autoload.php'; | ||
} elseif (file_exists(getcwd() . '/vendor/autoload.php')) { | ||
// Root project is current working directory | ||
require getcwd() . '/vendor/autoload.php'; | ||
} elseif (file_exists(__DIR__ . '/../../../autoload.php')) { | ||
// Relative to composer install | ||
require __DIR__ . '/../../../autoload.php'; | ||
} else { | ||
fwrite(STDERR, "Unable to setup autoloading; aborting\n"); | ||
exit(2); | ||
} | ||
|
||
$libraryPath = getcwd(); | ||
$viewPath = getcwd() . '/view'; | ||
|
||
$rules = array( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use short array syntax |
||
'help|h' => 'Get usage message', | ||
'library|l-s' => 'Library to parse; if none provided, assumes current directory', | ||
'view|v-s' => 'View path to parse; if none provided, assumes view as template directory', | ||
'extensions|e-s' => 'List of accepted file extensions (regex alternation: *html, phtml|tpl); default: *', | ||
'output|o-s' => 'Where to write map file; if not provided, assumes "template_map.php" in library directory', | ||
'append|a' => 'Append to map file if it exists', | ||
'overwrite|w' => 'Whether or not to overwrite existing map file', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can remove 3 spaces in each line before |
||
); | ||
|
||
try { | ||
$opts = new Console\Getopt($rules); | ||
$opts->parse(); | ||
} catch (Console\Exception\RuntimeException $e) { | ||
echo $e->getUsageMessage(); | ||
exit(2); | ||
} | ||
|
||
if ($opts->getOption('h')) { | ||
echo $opts->getUsageMessage(); | ||
exit(0); | ||
} | ||
|
||
$fileExtensions = '*'; | ||
if (isset($opts->e) && $opts->e != '*') { | ||
if (!preg_match('/^(\*?[[:alnum:]]\*?+\|?)+$/', $opts->e)) { | ||
echo 'Invalid extensions list specified. Expecting wildcard or alternation: *, *html, phtml|tpl' . PHP_EOL | ||
. PHP_EOL; | ||
echo $opts->getUsageMessage(); | ||
exit(2); | ||
} | ||
$fileExtensions = '(' . $opts->e . ')'; | ||
} | ||
$fileExtensions = str_replace('*', '.*', $fileExtensions); | ||
|
||
$relativePathForMap = ''; | ||
if (isset($opts->l)) { | ||
if (!is_dir($opts->l)) { | ||
echo 'Invalid library directory provided' . PHP_EOL | ||
. PHP_EOL; | ||
echo $opts->getUsageMessage(); | ||
exit(2); | ||
} | ||
$libraryPath = $opts->l; | ||
} | ||
$libraryPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($libraryPath)); | ||
|
||
if (isset($opts->v)) { | ||
if (!is_dir($opts->v)) { | ||
echo 'Invalid view template directory provided' . PHP_EOL | ||
. PHP_EOL; | ||
echo $opts->getUsageMessage(); | ||
exit(2); | ||
} | ||
$viewPath = $opts->v; | ||
} | ||
|
||
if (!is_dir($viewPath)) { | ||
printf('Invalid view path provided (%s)', $viewPath); | ||
echo PHP_EOL . PHP_EOL; | ||
echo $opts->getUsageMessage(); | ||
exit(2); | ||
} | ||
|
||
$viewPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($viewPath)); | ||
|
||
$usingStdout = false; | ||
$appending = $opts->getOption('a'); | ||
$output = $libraryPath . '/template_map.php'; | ||
if (isset($opts->o)) { | ||
$output = $opts->o; | ||
if ('-' == $output) { | ||
$output = STDOUT; | ||
$usingStdout = true; | ||
} elseif (is_dir($output)) { | ||
echo 'Invalid output file provided' . PHP_EOL | ||
. PHP_EOL; | ||
echo $opts->getUsageMessage(); | ||
exit(2); | ||
} elseif (!is_writeable(dirname($output))) { | ||
echo "Cannot write to '$output'; aborting." . PHP_EOL | ||
. PHP_EOL | ||
. $opts->getUsageMessage(); | ||
exit(2); | ||
} elseif (file_exists($output) && !$opts->getOption('w') && !$appending) { | ||
echo "Template map file already exists at '$output'," . PHP_EOL | ||
. "but 'overwrite' or 'appending' flag was not specified; aborting." . PHP_EOL | ||
. PHP_EOL | ||
. $opts->getUsageMessage(); | ||
exit(2); | ||
} else { | ||
// We need to add the $libraryPath into the relative path that is created in the template map file. | ||
$mapPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath(dirname($output))); | ||
|
||
// Simple case: $libraryPathCompare is in $mapPathCompare | ||
if (strpos($libraryPath, $mapPath) === 0) { | ||
$relativePathForMap = substr($libraryPath, strlen($mapPath) + 1) . '/'; | ||
} else { | ||
$libraryPathParts = explode('/', $libraryPath); | ||
$mapPathParts = explode('/', $mapPath); | ||
|
||
// Find the common part | ||
$count = count($mapPathParts); | ||
for ($i = 0; $i < $count; $i++) { | ||
if (!isset($libraryPathParts[$i]) || $libraryPathParts[$i] != $mapPathParts[$i]) { | ||
// Common part end | ||
break; | ||
} | ||
} | ||
|
||
// Add parent dirs for the subdirs of map | ||
$relativePathForMap = str_repeat('../', $count - $i); | ||
|
||
// Add library subdirs | ||
$count = count($libraryPathParts); | ||
for (; $i < $count; $i++) { | ||
$relativePathForMap .= $libraryPathParts[$i] . '/'; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!$usingStdout) { | ||
if ($appending) { | ||
echo "Appending to template file map '$output' for library in '$libraryPath'..." . PHP_EOL; | ||
} else { | ||
echo "Creating template file map for library in '$libraryPath'..." . PHP_EOL; | ||
} | ||
} | ||
|
||
$dirOrIterator = new RecursiveDirectoryIterator($viewPath, RecursiveDirectoryIterator::FOLLOW_SYMLINKS); | ||
$l = new RecursiveIteratorIterator($dirOrIterator); | ||
|
||
// Iterate over each element in the path, and create a map of | ||
// template name => filename, where the filename is relative to the view path | ||
$map = new stdClass; | ||
foreach ($l as $file) { | ||
/* @var $file SplFileInfo */ | ||
if (!$file->isFile() || !preg_match('/^' . $fileExtensions . '$/', $file->getExtension())) { | ||
continue; | ||
} | ||
$filename = str_replace($libraryPath . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $file->getFilename()); | ||
|
||
// Add in relative path to library | ||
$filename = $relativePathForMap . $filename; | ||
$baseName = $file->getBasename('.' . pathinfo($file->getFilename(), PATHINFO_EXTENSION)); | ||
$mapName = str_replace(str_replace(DIRECTORY_SEPARATOR, '/', realpath($viewPath)) . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $baseName); | ||
$map->{$mapName} = $filename; | ||
} | ||
|
||
// Create a file with the map. | ||
|
||
if ($appending && file_exists($output) && is_array(include $output)) { | ||
// Append mode and the output file already exists: retrieve its | ||
// content and merges with the new map | ||
// Remove the last line as it is the end of the array, and we want to | ||
// append our new templates | ||
$content = file($output, FILE_IGNORE_NEW_LINES); | ||
array_pop($content); | ||
$content = implode(PHP_EOL, $content) . PHP_EOL; | ||
} else { | ||
// Write mode or the file does not exists: create a new file | ||
// Stupid syntax highlighters make separating < from PHP declaration necessary | ||
$content = '<' . "?php" . PHP_EOL | ||
. '// Generated by ZF2\'s ./bin/templatemap_generator.php' . PHP_EOL | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it gonna be still ZF2? ;) |
||
. 'return array(' . PHP_EOL; | ||
} | ||
|
||
// Process the template map as a string before inserting it to the output file | ||
|
||
$mapExport = var_export((array) $map, true); | ||
|
||
// Prefix with __DIR__ | ||
$mapExport = preg_replace("#(=> ')#", "=> __DIR__ . '/", $mapExport); | ||
|
||
// Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op | ||
$mapExport = str_replace("\\'", "'", $mapExport); | ||
|
||
// Remove unnecessary double-backslashes | ||
$mapExport = str_replace('\\\\', '\\', $mapExport); | ||
|
||
// Remove "array (" | ||
$mapExport = str_replace('array (', '', $mapExport); | ||
|
||
// Align "=>" operators to match coding standard | ||
preg_match_all('(\n\s+([^=]+)=>)', $mapExport, $matches, PREG_SET_ORDER); | ||
$maxWidth = 0; | ||
|
||
foreach ($matches as $match) { | ||
$maxWidth = max($maxWidth, strlen($match[1])); | ||
} | ||
|
||
$mapExport = preg_replace_callback('(\n\s+([^=]+)=>)', function ($matches) use ($maxWidth) { | ||
return PHP_EOL . ' ' . $matches[1] . str_repeat(' ', $maxWidth - strlen($matches[1])) . '=>'; | ||
}, $mapExport); | ||
|
||
// Trim the content | ||
$mapExport = trim($mapExport, "\n"); | ||
|
||
// Append the map to the file, close the array and write a new line | ||
$content .= $mapExport . ';' . PHP_EOL; | ||
|
||
// Write the contents to disk | ||
file_put_contents($output, $content); | ||
|
||
if (!$usingStdout) { | ||
echo "Wrote templatemap file to '" . realpath($output) . "'" . PHP_EOL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,5 +70,8 @@ | |
"psr-4": { | ||
"ZendTest\\View\\": "test/" | ||
} | ||
} | ||
}, | ||
"bin": [ | ||
"bin/templatemap_generator.php" | ||
] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be now
zendframework/zend-view