Initial commit with Symfony 2.1+Vendors

Signed-off-by: Gergely POLONKAI (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Polonkai Gergely
2012-07-01 09:52:20 +02:00
commit 082a0130c2
5381 changed files with 416709 additions and 0 deletions

View File

@@ -0,0 +1,186 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\DistributionBundle\Configurator;
use Sensio\Bundle\DistributionBundle\Configurator\Step\StepInterface;
use Symfony\Component\Yaml\Yaml;
/**
* Configurator.
*
* @author Marc Weistroff <marc.weistroff@gmail.com>
*/
class Configurator
{
protected $filename;
protected $steps;
protected $parameters;
public function __construct($kernelDir)
{
$this->kernelDir = $kernelDir;
$this->filename = $kernelDir.'/config/parameters.yml';
$this->steps = array();
$this->parameters = $this->read();
}
public function isFileWritable()
{
return is_writable($this->filename);
}
public function clean()
{
if (file_exists($this->getCacheFilename())) {
@unlink($this->getCacheFilename());
}
}
/**
* @param StepInterface $step
*/
public function addStep(StepInterface $step)
{
$this->steps[] = $step;
}
/**
* @param integer $index
*
* @return StepInterface
*/
public function getStep($index)
{
if (isset($this->steps[$index])) {
return $this->steps[$index];
}
}
/**
* @return array
*/
public function getSteps()
{
return $this->steps;
}
/**
* @return array
*/
public function getParameters()
{
return $this->parameters;
}
/**
* @return integer
*/
public function getStepCount()
{
return count($this->steps);
}
/**
* @param array $parameters
*/
public function mergeParameters($parameters)
{
$this->parameters = array_merge($this->parameters, $parameters);
}
/**
* @return array
*/
public function getRequirements()
{
$majors = array();
foreach ($this->steps as $step) {
foreach ($step->checkRequirements() as $major) {
$majors[] = $major;
}
}
return $majors;
}
/**
* @return array
*/
public function getOptionalSettings()
{
$minors = array();
foreach ($this->steps as $step) {
foreach ($step->checkOptionalSettings() as $minor) {
$minors[] = $minor;
}
}
return $minors;
}
/**
* Renders parameters as a string.
*
* @return string
*/
public function render()
{
return Yaml::dump(array('parameters' => $this->parameters));
}
/**
* Writes parameters to parameters.yml or temporary in the cache directory.
*
* @return boolean
*/
public function write()
{
$filename = $this->isFileWritable() ? $this->filename : $this->getCacheFilename();
return file_put_contents($filename, $this->render());
}
/**
* Reads parameters from file.
*
* @return array
*/
protected function read()
{
$filename = $this->filename;
if (!$this->isFileWritable() && file_exists($this->getCacheFilename())) {
$filename = $this->getCacheFilename();
}
$ret = Yaml::parse($filename);
if (false === $ret || array() === $ret) {
throw new \InvalidArgumentException(sprintf('The %s file is not valid.', $filename));
}
if (isset($ret['parameters']) && is_array($ret['parameters'])) {
return $ret['parameters'];
} else {
return array();
}
}
/**
* getCacheFilename
*
* @return string
*/
protected function getCacheFilename()
{
return $this->kernelDir.'/cache/parameters.yml';
}
}

View File

@@ -0,0 +1,47 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\DistributionBundle\Configurator\Form;
use Sensio\Bundle\DistributionBundle\Configurator\Step\DoctrineStep;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
/**
* Doctrine Form Type.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class DoctrineStepType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('driver', 'choice', array('choices' => DoctrineStep::getDrivers()))
->add('name', 'text')
->add('host', 'text')
->add('port', 'text', array('required' => false))
->add('user', 'text')
->add('password', 'repeated', array(
'required' => false,
'type' => 'password',
'first_name' => 'password',
'second_name' => 'password_again',
'invalid_message' => 'The password fields must match.',
))
;
}
public function getName()
{
return 'distributionbundle_doctrine_step';
}
}

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\DistributionBundle\Configurator\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
/**
* Secret Form Type.
*
* @author Marc Weistroff <marc.weistroff@sensio.com>
*/
class SecretStepType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('secret', 'text');
}
public function getName()
{
return 'distributionbundle_secret_step';
}
}

View File

@@ -0,0 +1,143 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\DistributionBundle\Configurator\Step;
use Sensio\Bundle\DistributionBundle\Configurator\Form\DoctrineStepType;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Doctrine Step.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class DoctrineStep implements StepInterface
{
/**
* @Assert\Choice(callback="getDriverKeys")
*/
public $driver;
/**
* @Assert\NotBlank
*/
public $host;
/**
* @Assert\Min(0)
*/
public $port;
/**
* @Assert\NotBlank
*/
public $name;
/**
* @Assert\NotBlank
*/
public $user;
public $password;
public function __construct(array $parameters)
{
foreach ($parameters as $key => $value) {
if (0 === strpos($key, 'database_')) {
$parameters[substr($key, 9)] = $value;
$key = substr($key, 9);
$this->$key = $value;
}
}
}
/**
* @see StepInterface
*/
public function getFormType()
{
return new DoctrineStepType();
}
/**
* @see StepInterface
*/
public function checkRequirements()
{
$messages = array();
if (!class_exists('\PDO')) {
$messages[] = 'PDO extension is mandatory.';
} else {
$drivers = \PDO::getAvailableDrivers();
if (0 == count($drivers)) {
$messages[] = 'Please install PDO drivers.';
}
}
return $messages;
}
/**
* @see StepInterface
*/
public function checkOptionalSettings()
{
return array();
}
/**
* @see StepInterface
*/
public function update(StepInterface $data)
{
$parameters = array();
foreach ($data as $key => $value) {
$parameters['database_'.$key] = $value;
}
return $parameters;
}
/**
* @see StepInterface
*/
public function getTemplate()
{
return 'SensioDistributionBundle:Configurator/Step:doctrine.html.twig';
}
/**
* @return array
*/
static public function getDriverKeys()
{
return array_keys(static::getDrivers());
}
/**
* @return array
*/
static public function getDrivers()
{
return array(
'pdo_mysql' => 'MySQL (PDO)',
'pdo_sqlite' => 'SQLite (PDO)',
'pdo_pgsql' => 'PosgreSQL (PDO)',
'oci8' => 'Oracle (native)',
'ibm_db2' => 'IBM DB2 (native)',
'pdo_oci' => 'Oracle (PDO)',
'pdo_ibm' => 'IBM DB2 (PDO)',
'pdo_sqlsrv' => 'SQLServer (PDO)',
);
}
}

View File

@@ -0,0 +1,87 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\DistributionBundle\Configurator\Step;
use Sensio\Bundle\DistributionBundle\Configurator\Form\SecretStepType;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Secret Step.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SecretStep implements StepInterface
{
/**
* @Assert\NotBlank
*/
public $secret;
public function __construct(array $parameters)
{
if (array_key_exists('secret', $parameters)){
$this->secret = $parameters['secret'];
if ('ThisTokenIsNotSoSecretChangeIt' == $this->secret) {
$this->secret = $this->generateRandomSecret();
}
} else {
$this->secret = $this->generateRandomSecret();
}
}
private function generateRandomSecret()
{
return hash('sha1', uniqid(mt_rand()));
}
/**
* @see StepInterface
*/
public function getFormType()
{
return new SecretStepType();
}
/**
* @see StepInterface
*/
public function checkRequirements()
{
return array();
}
/**
* checkOptionalSettings
*/
public function checkOptionalSettings()
{
return array();
}
/**
* @see StepInterface
*/
public function update(StepInterface $data)
{
return array('secret' => $data->secret);
}
/**
* @see StepInterface
*/
public function getTemplate()
{
return 'SensioDistributionBundle:Configurator/Step:secret.html.twig';
}
}

View File

@@ -0,0 +1,65 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\DistributionBundle\Configurator\Step;
use Symfony\Component\Form\Type\FormTypeInterface;
/**
* StepInterface.
*
* @author Marc Weistroff <marc.weistroff@sensio.com>
*/
interface StepInterface
{
/**
* __construct
*
* @param array $parameters
*/
function __construct(array $parameters);
/**
* Returns the form used for configuration.
*
* @return FormTypeInterface
*/
function getFormType();
/**
* Checks for requirements.
*
* @return array
*/
function checkRequirements();
/**
* Checks for optional setting it could be nice to have.
*
* @return array
*/
function checkOptionalSettings();
/**
* Returns the template to be renderer for this step.
*
* @return string
*/
function getTemplate();
/**
* Updates form data parameters.
*
* @param array $parameters
* @return array
*/
function update(StepInterface $data);
}