Our blog
Zend Framework Template Renamer Script
Bit hard to describe this one, but if you use Zend Framework along with the MVC functionality, and you also use base classes for your controllers, models etc, you may find that for each controller, model etc the files are pretty much the same apart from the names of the files and the classes.
If this sounds familiar then you might be interested in this little script.
Put the script in a folder that contains a sub folder called TEMPLATE. In the template folder have a skeleton template with controllers, models and views folders. If you wanted to replicate this basic structure x amount of times for each of your controllers, this script will make that procedure a breeze.
There may be other applications for this little utility as well.
(now updated to accepted a # separated list of replacements - create your entire structure in one move
)
-
<?php
-
/* This is a simple script designed to be run in a folder that contains a TEMPLATE folder
-
*
-
* the template folder shoudl contain your folder structure for your zend framework app.
-
*
-
* Normally this will be something like the format:
-
*
-
* controllers/item.php
-
* models/item.php
-
* models/db_table/item.php
-
* views/scripts/
-
* blah.php
-
* yada.php
-
* etc.php
-
*
-
* The script will take this template of folders and files and will find and will create a new folder structure in which all the filenames have been changed and within the files, a batch of finds and replaces have been done.
-
*
-
* The idea is that this then gives you a starting point for your ZF app. This works especially well if each controller, model etc inherits from a custom base_controller or base_model.
-
*
-
* hope it helps someone out
-
*
-
* Joseph
-
*
-
* edmondscommerce.co.uk
-
*
-
* */
-
-
-
?>
-
<form>
-
<h3>Set Find and Replace - Please Input Upper Cased - lower cased will be automatically replaced as well</h3>
-
<b>Find:</b> <input name="findme"> <b>Replace:</b> <input name="replaceme"> <input type="submit" value="go"></form>
-
<?php
-
exit;
-
}
-
$fm = $_GET['findme'];
-
-
-
$debug = true;
-
-
/*
-
$fm = 'findMe';
-
$rm = 'replaceMe';
-
*/
-
-
-
-
/*get root current working directory*/
-
-
/*output folder */
-
$of = 'OUTPUT';
-
delete($of);
-
}
-
-
-
foreach($rms as $k=>$rm){
-
/* First create a sub folder to put our new structure in */
-
-
-
/*template path */
-
$template = $cwd . '/TEMPLATE/';
-
-
/*copy template to output */
-
//if($k === 0){
-
dir_copy('TEMPLATE', $of);
-
//}
-
-
/* Now get output structure */
-
$dirlist = dir_list($of);
-
dbug($dirlist);
-
-
/*now jump into the output folder */
-
-
/*Now loop through, rename the file and then find and replace the contents */
-
replace_rename($dirlist, $fm, $rm);
-
-
/* Jump back to the main cwd */
-
}
-
-
-
######## FUNCTIONS #########
-
-
function replace_rename($array, $fm, $rm){
-
foreach($array as $k=>$v){
-
//its a file
-
h3("Renaming $v to $newfile");
-
done();
-
h3("Replacing Contents of $newfile");
-
err("$newfile empty");
-
exit;
-
}
-
err("$fm not found in this file... should it have been?");
-
}
-
file_put_contents($newfile, $newcontents);
-
done();
-
}else{
-
h3("Renaming $k to $newfile");
-
done();
-
$jumpin = $chdir . '/' . $newfolder;
-
h3("Jumping to $jumpin");
-
done();
-
//its a folder
-
h3("Now Recursing into $k");
-
echo '<blockquote>';
-
replace_rename($v, $fm, $rm);
-
echo '</blockquote>';
-
done();
-
}
-
}
-
}
-
-
-
// http://uk3.php.net/copy
-
function dir_copy($srcdir, $dstdir, $offset = '', $verbose = true)
-
{
-
// A function to copy files from one directory to another one, including subdirectories and
-
// nonexisting or newer files. Function returns number of files copied.
-
// This function is PHP implementation of Windows xcopy A:\dir1\* B:\dir2 /D /E /F /H /R /Y
-
// Syntaxis: [$returnstring =] dircopy($sourcedirectory, $destinationdirectory [, $offset] [, $verbose]);
-
// Example: $num = dircopy('A:\dir1', 'B:\dir2', 1);
-
-
// Original by SkyEye. Remake by AngelKiha.
-
// Linux compatibility by marajax.
-
// ([danbrown AT php DOT net): *NIX-compatibility noted by Belandi.]
-
// Offset count added for the possibilty that it somehow miscounts your files. This is NOT required.
-
// Remake returns an explodable string with comma differentiables, in the order of:
-
// Number copied files, Number of files which failed to copy, Total size (in bytes) of the copied files,
-
// and the files which fail to copy. Example: 5,2,150000,\SOMEPATH\SOMEFILE.EXT|\SOMEPATH\SOMEOTHERFILE.EXT
-
// If you feel adventurous, or have an error reporting system that can log the failed copy files, they can be
-
// exploded using the | differentiable, after exploding the result string.
-
//
-
$num = 0;
-
$fail = 0;
-
$sizetotal = 0;
-
$fifail = '';
-
if($file != '.' && $file != '..') {
-
// $srcfile = $srcdir . '\\' . $file; # deleted by marajax
-
// $dstfile = $dstdir . '\\' . $file; # deleted by marajax
-
$srcfile = $srcdir . '/' . $file; # added by marajax
-
$dstfile = $dstdir . '/' . $file; # added by marajax
-
if($ow> 0) {
-
}
-
else {
-
echo "Error: File '$srcfile' could not be copied!<br />\n";
-
$fail++;
-
$fifail = $fifail.$srcfile.'|';
-
}
-
}
-
}
-
// $ret = dircopy($srcfile, $dstfile, $verbose); # deleted by patrick
-
$ret = dir_copy($srcfile, $dstfile, $verbose); # added by patrick
-
}
-
}
-
}
-
}
-
$ret = ($num + $red[0]).','.(($fail-$offset) + $red[1]).','.($sizetotal + $red[2]).','.$fifail.$red[3];
-
return $ret;
-
}
-
-
-
-
-
function dir_list($directory){
-
//echo $directory;
-
if($file != '.' && $file != '..'){
-
$return[$file]=dir_list($directory . '/' . $file . '/');
-
}else{
-
$return[] = $file;
-
}
-
}
-
}
-
return $return;
-
}else{
-
return false;
-
}
-
}
-
-
//recursive delete file/folder
-
function delete($dirname, $folder_empty = 'delete'){
-
// Sanity check
-
//bottom();
-
return false;
-
}
-
-
// Simple delete for a file
-
}
-
-
// Loop through the folder
-
while (false !== $entry = $dir->read()) {
-
// Skip pointers
-
if ($entry == '.' || $entry == '..') {
-
continue;
-
}
-
// Recurse
-
delete("$dirname/$entry");
-
}// end while looping
-
-
// Clean up
-
$dir->close();
-
if($folder_empty == 'delete'){
-
}else{
-
//just empty the folder, don't actually delete it
-
return true;
-
}
-
}
-
-
//DEbug Functions
-
-
//convert tabs to spaces
-
function tab2space($text, $spaces = 4){
-
return $text;
-
}
-
-
//return name of variable passed by reference
-
function vname(&$var, $scope=false){
-
if($scope) $vals = $scope;
-
else $vals = $GLOBALS;
-
$old = $var;
-
$new = 'THISONE';
-
$var = $new;
-
$vname = false;
-
foreach($vals as $key => $val) {
-
if($val !=='var'){
-
if($val === $new) $vname = $key;
-
}
-
}
-
$var = $old;
-
return $vname;
-
}
-
-
-
function dbug(&$item, $scope=false){
-
global $debug;
-
if(!$debug){
-
return false;
-
}
-
$vname = vname(&$item,$scope);
-
echo '<hr><h3>Debug Info: $' . $vname . '</h3>' . tab2space(nl2br(htmlentities(var_export($item, true)))) . '<hr>';
-
}
-
-
function done(){
-
echo '<h3 style="color: green;">Done..</h3>';
-
}
-
-
function h3($text){
-
}
-
-
function err($text){
-
}
More...
More Reading:
2 Comments
|
I usually resort to global controllers for this problem. i have a controllers dir in my aplication folder where i put them and the controllers from the modules directory extend them. |
|
Hi Gabriel I do something similar I think, however I lets say I have a project with 10 tables and I want to use my baseCrudController on these ten tables I have to go and create a whole bunch of files that are basically empty initially I was either going to spend an hour messing about creating files, or create a script to do it for me - and here it is |
RSS Feed
gabriel solomon
March 6th, 2009