Last active
August 29, 2015 14:11
-
-
Save chanmix51/94e2c2efb7d8e76663de to your computer and use it in GitHub Desktop.
Pomm2 Silex project creation script
This file contains hidden or 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
#!/bin/bash | |
echo "Project setup"; | |
echo -n "What is your project name ['Test']:> "; | |
read project_name; | |
echo "Postgresql setup"; | |
echo -n "What is your Postgresql username [$USER]:> "; | |
read db_username; | |
echo -n "What is this user's password (empty if none) [] :> "; | |
read db_password; | |
echo -n "What is the server address (IP address or socket directory ('none' for none) [localhost] :> "; | |
read db_host; | |
db_host=${db_host:-"localhost"}; | |
if [ "$db_host" = "none" ]; | |
then | |
db_host=''; | |
db_port=''; | |
db_hostname=''; | |
else | |
echo -n "What is the server port [5432] :> "; | |
read db_port; | |
if echo "$db_host" | grep "/" > /dev/null; | |
then | |
db_hostname="!${db_host}!"; | |
else | |
db_hostname=$db_host; | |
fi; | |
db_hostname="@${db_hostname}:${db_port}" | |
fi | |
echo -n "What is the database name [$USER] :> "; | |
read db_name; | |
echo -n "What IP address should PHP stand alone be bound on ? (enter 'none' to skip server start) [localhost] :> "; | |
read php_host; | |
if [ "$php_host" != "none" ]; | |
then | |
echo -n "What port should be used for standalone PHP server ? [1025] :> "; | |
read php_port; | |
php_port=${php_port:-"1025"}; | |
php_host=${php_host:-"localhost"}; | |
fi; | |
db_username=${db_username:-$USER}; | |
db_password=${db_password:+":${db_password}"}; | |
db_port=${db_port:-"5432"}; | |
db_name=${db_name:=$USER}; | |
project_name=${project_name:-'Test'} | |
mkdir -p sources/{config,twig,sql,lib/{Model,Controller}} web/{css,images,js} tests documentation log | |
chmod 777 log | |
> web/favicon.ico | |
cat > sources/config/config.php.dist << EOF | |
<?php // sources/config/config.php.dist | |
// This file should be copied as config.php in the config directory and filled | |
// with your configuration parameters. | |
// pomm2 session configuration | |
\$app['config.pomm.dsn'] = [ | |
'prod' => [], | |
'dev' => [ | |
'$project_name' => ['dsn' => 'pgsql://user:pass@host:port/dbname', 'class:session_builder' => '\Model\\${project_name}SessionBuilder'] | |
], | |
]; | |
EOF | |
cat > sources/lib/Model/${project_name}SessionBuilder.php << EOF | |
<?php #sources/lib/Model/${project_name}SessionBuilder | |
namespace Model; | |
use PommProject\ModelManager\SessionBuilder; | |
use PommProject\Foundation\Session\Session; | |
use PommProject\Foundation\Query\LoggerListener; | |
use Psr\Log\LoggerInterface; | |
/** | |
* ${project_name}SessionBuilder | |
* | |
* Project related session builder. | |
*/ | |
class ${project_name}SessionBuilder extends SessionBuilder | |
{ | |
/** | |
* postConfiguration | |
* | |
* Add static session configuration here. | |
* @see SessionBuilder | |
*/ | |
protected function postConfiguration(Session \$session) | |
{ | |
return parent::postConfiguration(\$session); | |
} | |
} | |
EOF | |
cat > sources/bootstrap.php << "EOF" | |
<?php #sources/bootstrap.php | |
use Silex\Provider; | |
// This script sets up the application DI with services. | |
if (!defined('PROJECT_DIR')) | |
{ | |
define('PROJECT_DIR', dirname(__DIR__)); | |
} | |
require PROJECT_DIR.'/sources/config/environment.php'; | |
// autoloader | |
$loader = require PROJECT_DIR.'/vendor/autoload.php'; | |
$loader->add(false, PROJECT_DIR.'/sources/lib'); | |
$app = new Silex\Application(); | |
// configuration parameters | |
if (!file_exists(PROJECT_DIR.'/sources/config/config.php')) { | |
throw new \RunTimeException("No config.php found in config."); | |
} | |
require PROJECT_DIR.'/sources/config/config.php'; | |
// extensions registration | |
$app->register(new Provider\UrlGeneratorServiceProvider()); | |
$app->register(new Provider\SessionServiceProvider()); | |
$app->register(new Provider\TwigServiceProvider(), array( | |
'twig.path' => array(PROJECT_DIR.'/sources/twig'), | |
)); | |
$app->register(new PommProject\Silex\ServiceProvider\PommServiceProvider(), [ | |
'pomm.configuration' => $app['config.pomm.dsn'][ENV] | |
]); | |
// Service container customization. | |
$app['loader'] = $loader; | |
// set DEBUG mode or not | |
if (preg_match('/^dev/', ENV)) { | |
$app['debug'] = true; | |
$app->register(new Provider\MonologServiceProvider(), array( | |
'monolog.logfile' => PROJECT_DIR.'/log/app.log' | |
)); | |
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) { $twig->addExtension(new Twig_Extension_Debug()); return $twig; })); | |
$app->register(new Provider\ServiceControllerServiceProvider()); | |
$app->register(new Provider\WebProfilerServiceProvider(), [ | |
'profiler.cache_dir' => PROJECT_DIR.'/cache/profiler', | |
'profiler.mount_prefix' => '/_profiler', // this is the default | |
]); | |
$app->register(new PommProject\Silex\ProfilerServiceProvider\PommProfilerServiceProvider()); | |
} | |
return $app; | |
EOF | |
cat > .pomm_cli_bootstrap.php << "EOF" | |
<?php | |
$app = require __DIR__."/sources/bootstrap.php"; | |
return $app['pomm']; | |
EOF | |
cat > web/index.php << "EOF" | |
<?php #web/index.php | |
$filename = __DIR__.preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']); | |
if (php_sapi_name() === 'cli-server' && is_file($filename)) { | |
return false; | |
} | |
$app = require dirname(__DIR__).'/sources/application.php'; | |
$app->run(); | |
EOF | |
cat > web/.htaccess << "EOF" | |
<IfModule mod_rewrite.c> | |
Options -MultiViews | |
RewriteEngine On | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteRule ^(.*)$ index.php [QSA,L] | |
</IfModule> | |
EOF | |
cat > sources/application.php << "EOF" | |
<?php // sources/application.php | |
use \Symfony\Component\HttpFoundation\Request; | |
use \Symfony\Component\HttpFoundation\Response; | |
$app = require "bootstrap.php"; | |
$app->mount('/', new \Controller\DumbController()); | |
$app->error(function(Exception $e, $code) use ($app) { | |
if ($app['debug']) { | |
// trigger log if enabled | |
return; | |
} | |
return new Response($app['twig']->render('error.html.twig', array('message' => 'An error occured.')), $code); | |
}); | |
return $app; | |
EOF | |
cat > sources/lib/Controller/DumbController.php << EOF | |
<?php | |
namespace Controller; | |
use Silex\Application; | |
use Silex\ControllerProviderInterface; | |
use \Symfony\Component\HttpFoundation\Request; | |
use \Symfony\Component\HttpFoundation\Response; | |
class DumbController implements ControllerProviderInterface | |
{ | |
protected \$app; | |
public function connect(Application \$app) | |
{ | |
\$this->app = \$app; | |
\$controller_collection = \$app['controllers_factory']; | |
\$controller_collection->get('/', [\$this, 'index'])->bind('index'); | |
return \$controller_collection; | |
} | |
public function index() | |
{ | |
\$result = \$this->app['pomm']['${project_name}'] | |
->getQueryManager() | |
->query('select \$*::text as hello', ['Hello World !']) | |
; | |
return \$this->app['twig']->render('index.html.twig', ['row' => \$result->current()]); | |
} | |
} | |
EOF | |
cat > sources/twig/index.html.twig << "EOF" | |
<html> | |
<body> | |
<h1>{{ row['hello'] }}</h1> | |
<p>Your Silex application is up and running and your database connection is working.</p> | |
</body> | |
</html> | |
EOF | |
cat > sources/twig/error.html.twig << "EOF" | |
<html> | |
<body> | |
<h1>Oooops</h1> | |
<p>{{ message }}</p> | |
</body> | |
</html> | |
EOF | |
cat > sources/config/environment.php << "EOF" | |
<?php // sources/config/environment.php | |
if (!defined('ENV')) | |
{ | |
define('ENV', 'dev'); | |
} | |
EOF | |
cat > composer.json << "EOF" | |
{ | |
"require": { | |
"pomm-project/cli": "2.0.*@dev", | |
"pomm-project/foundation": "2.0.*@dev", | |
"pomm-project/model-manager": "2.0.*@dev", | |
"silex/silex": "~1.0", | |
"silex/web-profiler": "~1.0", | |
"monolog/monolog": "~1.11", | |
"symfony/monolog-bridge": "~2.6", | |
"pomm-project/pomm-service-provider": "dev-silex-1", | |
"pomm-project/pomm-profiler-service-provider": "dev-silex-1" | |
} | |
} | |
EOF | |
cat > .gitignore << "EOF" | |
vendor/* | |
.ctags | |
EOF | |
if which composer.phar >/dev/null; | |
then | |
composer.phar install | |
else | |
wget -O - 'http://getcomposer.org/installer' 2>/dev/null |php; | |
php composer.phar install; | |
fi | |
cat > sources/config/config.php << EOF | |
<?php | |
\$app['config.pomm.dsn'] = ['dev' => ['${project_name}' => ['dsn' => 'pgsql://${db_username}${db_password}${db_hostname}/${db_name}', 'class:session_builder' => '\Model\\${project_name}SessionBuilder']]]; | |
EOF | |
if [ "$php_host" != "none" ]; | |
then | |
php -S ${php_host}:${php_port} -t web web/index.php & | |
fi; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment