Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Silex project bootstrap with Pomm
#!/bin/bash
mkdir -p bin sources/{config,twig,sql,lib/{Model,Controller}} web/{css,images,js} tests documentation log
chmod 777 log
> web/favicon.ico
cat > bin/generate_model.php <<"EOF"
<?php // bin/generate_model.php
$app = require(__DIR__."/../sources/bootstrap.php");
$scan = new Pomm\Tools\ScanSchemaTool(array(
'schema' => $argv[1],
'database' => $app['pomm']->getDatabase(),
'prefix_dir' => PROJECT_DIR."/sources/lib",
'namespace' => 'Model\%dbname%\%schema%',
));
$scan->execute();
$scan->getOutputStack()->setLevel(254);
foreach ( $scan->getOutputStack() as $line )
{
printf("%s\n", $line);
}
EOF
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.
// pomm database configuration
$app['config.pomm.dsn'] = array('prod' => array(), 'dev' => array('my_db' => array('dsn' => 'pgsql://user:pass@host:port/dbname', 'name' => 'MyProject', 'class' => '\Model\MyDatabase')));
// put your configuration here
EOF
cat > sources/lib/Model/MyDatabase.php << "EOF"
<?php #sources/lib/Model/MyDatabase.php
namespace Model;
use \Pomm\Connection\Database;
class MyDatabase extends Database
{
protected function initialize()
{
parent::initialize();
// register configuration or converters here
}
}
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 \Pomm\Silex\PommServiceProvider(), array(
'pomm.databases' => $app['config.pomm.dsn'][ENV]
));
// Service container customization.
$app['loader'] = $loader;
$app['pomm.connection'] = $app->share(function() use ($app) { return $app['pomm']
->getDatabase()
->createConnection();
});
// 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['pomm.connection'] = $app->share($app->extend('pomm.connection',
function($connection, $app) { $connection->setLogger($app['monolog']); return $connection; }
));
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) { $twig->addExtension(new Twig_Extension_Debug()); return $twig; }));
}
return $app;
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('/', array($this, 'index'))->bind('index');
return $controller_collection;
}
public function index()
{
$this->app['pomm.connection']
->executeAnonymousQuery('SELECT true');
return $this->app['twig']->render('index.html.twig');
}
}
EOF
cat > sources/twig/index.html.twig << "EOF"
<html>
<body>
<h1>Hello world</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"
{
"minimum-stability": "dev",
"require": {
"pomm/pomm-service-provider": "dev-master",
"monolog/monolog": "1.6.*@dev",
"twig/twig": "dev-master"
}
}
EOF
cat > .gitignore << "EOF"
vendor/*
.ctags
EOF
echo "You can now edit the 'composer.json' to add dependencies you need."
echo "Set up a web server with document root in the 'web/' directory."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.