示例#1
0
$app['config'] = function ($app) {
    // Get config files
    // Likely not platform agnostic
    $configs = [sprintf('%s/../config.json', realpath(__DIR__))];
    // Unix or Windows home path
    $homePath = strtolower(substr(PHP_OS, 0, 3)) === 'win' ? getenv('USERPROFILE') : getenv('HOME');
    $userConfigFile = sprintf('%s/.rfcdigestor.json', $homePath);
    if (file_exists($userConfigFile)) {
        $configs[] = $userConfigFile;
    }
    $config = new Config($configs);
    // Load configs and get storage path
    $storagePath = realpath(sprintf('%s/%s', __DIR__, $config->get('storagePath')));
    $templatePath = realpath(sprintf('%s/%s', __DIR__, $config->get('templatePath')));
    // Set config paths for future commands
    $config->set('storagePath', $storagePath);
    $config->set('templatePath', $templatePath);
    return $config;
};
$app->register(new Silex\Provider\TranslationServiceProvider(), array('translator.messages' => array()));
$app->register(new Silex\Provider\SessionServiceProvider());
$app->register(new FormServiceProvider());
$app->register(new Silex\Provider\ValidatorServiceProvider());
$app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => $app['config']->get('templatePath')));
$app['subscribe-form'] = $app['form.factory']->createBuilder('form')->add('email', 'text', ['attr' => ['placeholder' => 'Your email'], 'constraints' => [new Assert\Email(), new Assert\Callback(function ($email, ExecutionContextInterface $context) use($app) {
    $repo = $app['orm.em']->getRepository('MikeyMike\\RfcDigestor\\Entity\\Subscriber');
    if ($repo->findBy(['email' => $email])) {
        $context->addViolation("Email already used");
    }
})]])->add('subscribe', 'submit', ['attr' => ['class' => 'btn-default']])->getForm();
$app['slack-subscribe-form'] = $app['form.factory']->createBuilder('form')->add('email', 'text', ['attr' => ['placeholder' => 'Your email'], 'constraints' => new Assert\Email()])->add('token', 'text', ['attr' => ['placeholder' => 'Your slack token'], 'constraints' => new Assert\NotBlank()])->add('channel', 'text', ['attr' => ['placeholder' => 'The slack channel to post to'], 'constraints' => new Assert\NotBlank()])->getForm();
 /**
  * @see \Noodlehaus\Config::set
  * @param string $key
  * @param mixed $value
  */
 public function set($key, $value)
 {
     $this->config->set($key, $value);
 }