Installer Smarty automatiquement (Voici une solution)

Modérateur PHPfrance
Modérateur PHPfrance | 2575 Messages

05 sept. 2007, 16:06

Vous travaillez avec Smarty et vous voulez automatiser sa configuration pour votre projet Web. J'ai développé un script php qui le fait trés bien.

Sous forme de formulaire, le script receuille un certain nombre d'informations concernant:
  • 1. le dossier source de smarty que vous aurez téléchargé à partir de son site officiel ( http://smarty.php.net/download.php )
    2. le dossier de base de votre serveur web local (ex. c:/wamp/www)
    3. et le nom de votre projet web smarty que vous voulez créer.
Un bouton "Start" démarre alors l'installation et crée l'arboréscence et la configuration nécessaires pour votre site web.
Vous n'aurez plus qu'à commencer le développement de votre site basé sur Smarty

Nommez le code suivant: smarty_install.php
<pre style="font-size:11">
<h2>Manual setup</h2>
REQUIREMENTS:

Smarty requires PHP 4.0.6 or later.
See the on-line documentation for complete install instructions.

INSTALLATION (quick):
* unzip Smarty package downloded from <a href="http://smarty.php.net/download.php">here</a> on your hard disk e.g. C:/Smarty

* copy the files under the libs/ directory to a directory that is in your PHP
  include_path, or set the SMARTY_DIR constant and put them in this directory.
  (if you upgrade from versions before 2.5.0 be aware that up to Smarty 2.4.2
  all necessary files where in the distribution's root directory, but are now
  in libs/.)

* for each application using Smarty, create a "templates", "configs", and a
  "templates_c" directory, be sure to set the appropriate directory settings in
  Smarty for them. If they are located in the same directory as your
  application, they shouldn't need to be modified. Be sure the "templates_c"
  directory is writable by your web server user (usually nobody). chown
  nobody:nobody templates_c; chmod 700 templates_c You can also chmod 777 this
  directory, but be aware of security issues for multi-user systems. If you are
  using Smarty's built-in caching, create a "cache" directory and also chown
  nobody:nobody.

* setup your php and template files. A good working example is in the on-line
  documentation.

* TECHNICAL NOTE: If you do not have access to the php.ini file, you can change
  non-server settings (such as your include_path) with the ini_set() command.
  example: ini_set("include_path",".:/usr/local/lib/php");

<form>
<h2>Automatic setup</h2>
Smarty source Path <input type="text" name="sourcePath" value="<?php echo $_GET['sourcePath']?$_GET['sourcePath']:''; ?>" /> e.g. <i>C:/Smarty</i><br />
Your Web Document Root <input type="text" name="webDocumentRoot" value="<?php echo $_GET['webDocumentRoot']?$_GET['webDocumentRoot']:$_SERVER["DOCUMENT_ROOT"]; ?>" /> e.g. <i>C:/wamp/www</i><br />
Your Application Name <input type="text" name="appName" value="<?php echo $_GET['appName']?$_GET['appName']:'smarty_project1'; ?>" /> e.g. <i>smarty_project1</i><br />
<input type="submit" name="setup" value="Start" />
</form>
<?php
//Errors
if ($_GET["setup"] && !$_GET["sourcePath"] ){echo "<p>Source path required!</p>"; exit;}
if ($_GET["setup"] && $_GET["sourcePath"] && !file_exists($_GET["sourcePath"])){echo "<p>Source path not exists!</p>"; exit;}

if ($_GET["setup"] && !$_GET["webDocumentRoot"] ){echo "<p>Web document root required!</p>"; exit;}
if ($_GET["setup"] && $_GET["webDocumentRoot"] && !file_exists($_GET["webDocumentRoot"])){echo "<p>Web document root not exists!</p>"; exit;}

if ($_GET["setup"] && !$_GET["appName"] ){echo "<p>Application Name required!</p>"; exit;}
if ($_GET["setup"] && $_GET["webDocumentRoot"] && file_exists($_GET["webDocumentRoot"]) 
&& $_GET["appName"] && file_exists($_GET["webDocumentRoot"]."/".$_GET["appName"])){echo "<p>Project already exists!</p>"; exit;}

//Setup
if ($_GET["setup"] && $_GET["sourcePath"] && file_exists($_GET["sourcePath"])
&& $_GET["webDocumentRoot"] && file_exists($_GET["webDocumentRoot"]) 
&& $_GET["appName"] && !file_exists($_GET["webDocumentRoot"]."/".$_GET["appName"]) ){
    //Get source and application Path
	$sourcePath = $_GET["sourcePath"];
	$webDocumentRoot = $_GET["webDocumentRoot"];
	$appName = $_GET["appName"];
    $appPath = $webDocumentRoot."/".$appName;
	
    //Create required directories on the application path
	if  (!file_exists($appPath))                   mkdir($appPath);
	if  (!file_exists($appPath."/libs"))           mkdir($appPath."/libs");
	if  (!file_exists($appPath."/templates"))      mkdir($appPath."/templates");
	if  (!file_exists($appPath."/configs"))        mkdir($appPath."/configs");
	if  (!file_exists($appPath."/templates_c"))    mkdir($appPath."/templates_c");
	if  (!file_exists($appPath."/cache"))          mkdir($appPath."/cache");
	
    //Copy libs directory to the application directory
    $done = (copyDir ($sourcePath."/libs", $appPath."/libs"));
    
    //Create default smartyObject for includes, main index page  and template page
    $default_smartyObject = <<<CODE
<?php
// put full path to Smarty.class.php
require('$appPath/libs/Smarty.class.php');
\$smarty = new Smarty();
\$smarty->template_dir = '$appPath/templates';
\$smarty->compile_dir = '$appPath/templates_c';
\$smarty->cache_dir = '$appPath/cache';
\$smarty->config_dir = '$appPath/configs';

//Configure access permissions
chown (\$smarty->compile_dir, "nobody");
chown (\$smarty->cache_dir, "nobody");
chmod (\$smarty->compile_dir, 400);
chmod (\$smarty->cache_dir, 400);
?>
CODE;
     $default_index_page = <<<CODE
<?php
//put full path to smartyObject.inc
require('smartyObject.inc');

//to assign your templates using the smartyObject \$smarty do this:
\$smarty->assign('php_field_in_template', 'Hello World!');

//then display your template index page locate at /templates directory
\$smarty->display('index.tpl');

//the index.tpl file should be created and contained the fields you assign. It must seems like this:
//<html>
//<head>
//<title>Smarty</title>
//</head>
//<body>
//{\$php_field_in_template}
//</body>
//</html>
?>
CODE;
     $default_template_page = <<<CODE
<html>
<head>
<title>Smarty</title>
</head>
<body>
{\$php_field_in_template}
</body>
</html>
CODE;
    //Writes default pages
    $done = file_put_contents($appPath."/smartyObject.inc", $default_smartyObject);    
    $done = file_put_contents($appPath."/index.php", $default_index_page);
    $done = file_put_contents($appPath."/templates/index.tpl", $default_template_page);
    
    //End
	if ($done) echo "<p>Installation complete. <a href=".$_SERVER["DOCUMENT URI"]."\"/$appName\">View your application</a></p>"; else "<p>Installation failed!</p>";
}

//function for all files automatic copy of a given directory to a new destination
function copyDir ($source, $destination){
	$lecteur  = opendir($source);
	while ($lecteur && ($entry = readdir($lecteur))) {
    	if ($entry != "." && $entry != "..")
			if (is_dir($source."/".$entry)) {
				if (!file_exists($destination."/".$entry)) mkdir ($destination."/".$entry);
				$done = copyDir ($source."/".$entry, $destination."/".$entry);
			}
			else if (!file_exists($destination."/".$entry)) $done = copy($source."/".$entry, $destination."/".$entry);
	}
	return $done;
}	
?>
</pre>
Modifié en dernier par sadeq le 05 sept. 2007, 18:48, modifié 3 fois.
--------//////----//---//----//////
-------//---//----//---//----//---//
------//////----//////-----//////
-----||--------||--||---||
Prendre le recul n'est pas une perte de temps.


ps: Affrontez moi dans l'arène

ViPHP
ViPHP | 2291 Messages

05 sept. 2007, 17:11

Bonjour,
=D> Merci
ImageCe que l'on apprend par l'effort reste toujours ancré beaucoup plus longtemps.