Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit c84de0aa6e3f1411d2f681f0ea01711d31dfbe0f 1 parent f23bd8a
Mike van Riel mvriel authored
4 .gitignore
View
@@ -0,0 +1,4 @@
+vendor
+phpunit.xml
+cilex.phar
+.idea
19 LICENSE
View
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Mike van Riel
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
0  README
View
No changes.
37 README.md
View
@@ -0,0 +1,37 @@
+Cilex, a simple Command Line Interface framework
+================================================
+
+Cilex is a simple command line application framework to develop simple tools
+based on [Symfony2][1] components:
+
+```php
+<?php
+require_once __DIR__.'/cilex.phar';
+
+$app = new \Cilex\Application('Cilex');
+$app->command(new \Cilex\Command\DemoGreetCommand());
+$app->run();
+```
+
+Cilex works with PHP 5.3.2 or later and is heavily inspired on the [Silex][2]
+web micro-framework by Fabien Potencier.
+
+<!--
+## Installation
+
+Installing Cilex is as easy as it can get. Download the [`cilex.phar`][3] file
+and you're done!
+
+## More Information
+
+Read the [documentation][4] for more information.
+-->
+
+## License
+
+Cilex is licensed under the MIT license.
+
+[1]: http://symfony.com
+[2]: http://silex.sensiolabs.org
+[3]: http://cilex.github.com/get/cilex.phar
+[4]: http://cilex.github.com/documentation
8 compile
View
@@ -0,0 +1,8 @@
+#!/usr/bin/env php
+<?php
+require_once __DIR__.'/vendor/.composer/autoload.php';
+
+use Cilex\Compiler;
+
+$compiler = new Compiler();
+$compiler->compile();
37 composer.json
View
@@ -0,0 +1,37 @@
+{
+ "name": "cilex/cilex",
+ "description": "The PHP micro-framework for Command line tools based on the Symfony2 Components",
+ "keywords": ["microframework","cli"],
+ "homepage": "http://cilex.github.com",
+ "license": "MIT",
+ "authors":[
+ {
+ "name": "Mike van Riel",
+ "email": "mike.vanriel@naenius.com"
+ }
+ ],
+ "require":{
+ "php": ">=5.3.2",
+ "pimple/pimple": "dev-master",
+ "symfony/class-loader": "2.1.*",
+ "symfony/console": "2.1.*",
+
+ "symfony/process": "2.1.*",
+ "symfony/finder": "2.1.*"
+ },
+ "suggest":{
+ "monolog/monolog": ">=1.0.0",
+ "symfony/yaml": ">=1.0.0",
+ "symfony/validator": ">=1.0.0"
+ },
+ "autoload":{
+ "psr-0":{
+ "Cilex": "src/"
+ }
+ },
+ "extra":{
+ "branch-alias":{
+ "dev-master": "1.0-dev"
+ }
+ }
+}
53 composer.lock
View
@@ -0,0 +1,53 @@
+{
+ "hash": "b4e035f4caeaa4904767c220f2c2d1a9",
+ "packages": [
+ {
+ "package": "doctrine/common",
+ "version": "2.2.1"
+ },
+ {
+ "package": "monolog/monolog",
+ "version": "dev-master",
+ "source-reference": "6c15897ac74bd53572328094eacf03fbb7b8bca7"
+ },
+ {
+ "package": "pimple/pimple",
+ "version": "dev-master",
+ "source-reference": "321db91e49b6cf8cbeed58d8db662d40de96d2c3"
+ },
+ {
+ "package": "symfony/class-loader",
+ "version": "dev-master",
+ "source-reference": "da83c40b126ce8622fc052332b2c54a135ed976c",
+ "alias": "2.1.9999999.9999999-dev"
+ },
+ {
+ "package": "symfony/console",
+ "version": "dev-master",
+ "source-reference": "5fcd408b7b34f3e544f9743cf33e179767e9a000"
+ },
+ {
+ "package": "symfony/finder",
+ "version": "dev-master",
+ "source-reference": "be30ecc95281d729ee51b9e89644d442bcf60451",
+ "alias": "2.1.9999999.9999999-dev"
+ },
+ {
+ "package": "symfony/process",
+ "version": "dev-master",
+ "source-reference": "d08c4ea1530453ce270fcba78f0bff57a0869a9b",
+ "alias": "2.1.9999999.9999999-dev"
+ },
+ {
+ "package": "symfony/validator",
+ "version": "dev-master",
+ "source-reference": "2dc01d110185b9d8eef111cafe3254d3e9a19268"
+ },
+ {
+ "package": "symfony/yaml",
+ "version": "dev-master",
+ "source-reference": "2552c643a158ee7fb6c52c0dd55806055c575841"
+ }
+ ],
+ "aliases": []
+}
6 example.php
View
@@ -0,0 +1,6 @@
+<?php
+require_once __DIR__ . '/cilex.phar';
+
+$app = new \Cilex\Application('Cilex');
+$app->command(new \Cilex\Command\GreetCommand());
+$app->run();
19 phpunit.xml.dist
View
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<phpunit backupGlobals="false"
+ backupStaticAttributes="false"
+ colors="true"
+ convertErrorsToExceptions="true"
+ convertNoticesToExceptions="true"
+ convertWarningsToExceptions="true"
+ processIsolation="false"
+ stopOnFailure="false"
+ syntaxCheck="false"
+ bootstrap="vendor/.composer/autoload.php"
+ >
+ <testsuites>
+ <testsuite name="Cilex Test Suite">
+ <directory>./tests/Cilex/</directory>
+ </testsuite>
+ </testsuites>
+</phpunit>
99 src/Cilex/Application.php
View
@@ -0,0 +1,99 @@
+<?php
+
+/*
+ * This file is part of the Cilex framework.
+ *
+ * (c) Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Cilex;
+
+use \Symfony\Component\Console;
+use \Symfony\Component\ClassLoader\UniversalClassLoader;
+
+/**
+ * The Cilex framework class.
+ *
+ * @author Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * @api
+ */
+class Application extends \Pimple
+{
+ /** @var int Version number for Cilex */

constants dosent have a @var annotation in its php doc.

/**
  * Cilex Version number
  */
Mike van Riel Owner
mvriel added a note

From a conceptual point of view I disagree but the current de-facto PHPDoc standard does not prescribe what tag to use to identify a constant's type. Seeing as I have too little Sf2 coding standards I have applied the suggestion; will be included in next push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ const VERSION = '1.0.0';
+
+ /**
+ * Registers the autoloader and necessary components.
+ *
+ * @param string $name Name for this application.
+ * @param string|null $version Version number for this application.
+ */
+ function __construct($name, $version = null)

why? Shouldnt version always be the current Cilex version?

Mike van Riel Owner
mvriel added a note

The version number is the version of the CLI tool that uses Cilex and not of Cilex itself. This version number is displayed on the help output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ $this['autoloader'] = $this->share(function () {
+ $loader = new UniversalClassLoader();
+ $loader->register();
+ return $loader;
+ });
+
+ $this['console'] = $this->share(function () use ($name, $version) {
+ return new Console\Application($name, $version);
+ });
+ }
+
+ /**
+ * Executes this application.
+ *
+ * @param bool $interactive runs in an interactive shell if true.
+ *
+ * @return void
+ */
+ public function run($interactive = false)
+ {
+ $app = $this['console'];
+ if ($interactive)

Bracket should be on the same line as the if statement.

Mike van Riel Owner
mvriel added a note

Will be fixed in next push

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ $app = new Console\Shell($app);
+ }
+
+ $app->run();
+ }
+
+ /**
+ * Adds a command object.
+ *
+ * If a command with the same name already exists, it will be overridden.
+ *
+ * @param \Cilex\Command\Command $command A Command object
+ *
+ * @api
+ *
+ * @return void
+ */
+ public function command(Command\Command $command)
+ {
+ $command->setContainer($this);
+ $this['console']->add($command);
+ }
+
+ /**
+ * Registers a service provider.
+ *
+ * @param ServiceProviderInterface $provider A ServiceProviderInterface instance
+ * @param mixed[] $values An array of values that customizes the provider
+ *
+ * @return void
+ */
+ public function register(ServiceProviderInterface $provider, array $values = array())
+ {
+ foreach ($values as $key => $value) {
+ $this[$key] = $value;
+ }
+
+ $provider->register($this);
+ }
+}
70 src/Cilex/Command/Command.php
View
@@ -0,0 +1,70 @@
+<?php
+
+/*
+ * This file is part of the Cilex framework.
+ *
+ * (c) Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Cilex\Command;
+
+use \Symfony\Component\Console;
+
+/**
+ * Base class for Cilex commands.
+ *
+ * @author Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * @api
+ */
+abstract class Command extends Console\Command\Command
+{
+ /** @var \Cilex\Application */

should be multiline. and @var $container Cilex\Application

Mike van Riel Owner
mvriel added a note

I do not know whether the first is Sf2 coding standard and will apply, the latter is not according to PHPDoc: http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.var.pkg.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ protected $container = null;
+
+ /**
+ * Sets the application container containing all services.
+ *
+ * @param \Cilex\Application $container Application object to register.
+ *
+ * @return void
+ */
+ public function setContainer(\Cilex\Application $container)
+ {
+ $this->container = $container;
+ }
+
+ /**
+ * Returns the application container.
+ *
+ * @return \Cilex\Application
+ */
+ public function getContainer()
+ {
+ return $this->container;
+ }
+
+ /**
+ * Returns a service contained in the application container or null if none
+ * is found with that name.
+ *
+ * This is a convenience method used to retrieve an element from the
+ * Application container without having to assign the results of the
+ * getContainer() method in every call.
+ *
+ * @param string $name Name of the service
+ *
+ * @see self::getContainer()
+ *
+ * @api
+ *
+ * @return \stdClass|null
+ */
+ public function getService($name)
+ {
+ return isset($this->container[$name]) ? $this->container[$name] : null;
+ }
+}
47 src/Cilex/Command/GreetCommand.php
View
@@ -0,0 +1,47 @@
+<?php
+
+/*
+ * This file is part of the Cilex framework.
+ *
+ * (c) Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Cilex\Command;
+
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+/**
+ * Example command for testing purposes.
+ */
+class GreetCommand extends Command
+{
+ protected function configure()
+ {
+ $this
+ ->setName('demo:greet')
+ ->setDescription('Greet someone')
+ ->addArgument('name', InputArgument::OPTIONAL, 'Who do you want to greet?')
+ ->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $text = 'Hello';
+ $name = $input->getArgument('name');
+ if ($name) {
+ $text .= $name;
+ }
+
+ if ($input->getOption('yell')) {
+ $text = strtoupper($text);
+ }
+
+ $output->writeln($text);
+ }
+}
185 src/Cilex/Compiler.php
View
@@ -0,0 +1,185 @@
+<?php
+
+/*
+ * This file is part of the Cilex framework.
+ *
+ * (c) Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Cilex;
+
+use Symfony\Component\Finder\Finder;
+use Symfony\Component\Process\Process;
+
+/**
+ * The Compiler class compiles the Cilex framework.
+ *
+ * This is an adapted version of the Silex\Compiler class.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ * @author Mike van Riel <mike.vanriel@naenius.com>
+ */
+class Compiler
+{
+ protected $version;
+
+ /**
+ * Compiles the Cilex source code into one single Phar file.
+ *
+ * @param string $pharFile Name of the output Phar file
+ */
+ public function compile($pharFile = 'cilex.phar')
+ {
+ if (file_exists($pharFile)) {
+ unlink($pharFile);
+ }
+
+ $process = new Process('git log --pretty="%h %ci" -n1 HEAD');
+ if ($process->run() > 0) {
+ throw new \RuntimeException('The git binary cannot be found.');
+ }
+ $this->version = trim($process->getOutput());
+
+ $phar = new \Phar($pharFile, 0, 'cilex.phar');
+ $phar->setSignatureAlgorithm(\Phar::SHA1);
+
+ $phar->startBuffering();
+
+ $finder = new Finder();
+ $finder->files()
+ ->ignoreVCS(true)
+ ->name('*.php')
+ ->notName('Compiler.php')
+ ->in(__DIR__.'/..')
+ ->in(__DIR__.'/../../vendor/pimple/pimple/lib')
+ ->in(__DIR__.'/../../vendor/symfony/class-loader/Symfony/Component/ClassLoader')
+ ->in(__DIR__.'/../../vendor/symfony/console/Symfony/Component/Console')
+ ;
+
+ foreach ($finder as $file) {
+ $this->addFile($phar, $file);
+ }
+
+ $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../LICENSE'), false);
+ $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/.composer/autoload.php'));
+ $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/.composer/ClassLoader.php'));
+ $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/.composer/autoload_namespaces.php'));
+ $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/.composer/autoload_classmap.php'));
+
+ // Stubs
+ $phar->setStub($this->getStub());
+
+ $phar->stopBuffering();
+
+ // $phar->compressFiles(\Phar::GZ);
+
+ unset($phar);
+ }
+
+ protected function addFile(\Phar $phar, \splFileInfo $file, $strip = true)
+ {
+ $path = str_replace(
+ dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath()
+ );
+
+ $content = file_get_contents($file);
+ if ($strip) {
+ $content = self::stripWhitespace($content);
+ }
+ $content = str_replace('@package_version@', $this->version, $content);
+
+ $phar->addFromString($path, $content);
+ }
+
+ protected function getStub()
+ {
+ return <<<'EOF'
+<?php
+
+/*
+ * This file is part of the Cilex framework.
+ *
+ * (c) Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+Phar::mapPhar('cilex.phar');
+
+require_once 'phar://cilex.phar/vendor/.composer/autoload.php';
+
+if ('cli' === php_sapi_name() && basename(__FILE__) === basename($_SERVER['argv'][0]) && isset($_SERVER['argv'][1])) {
+ switch ($_SERVER['argv'][1]) {
+ case 'update':
+ $remoteFilename = 'http://cilex.github.com/get/cilex.phar';
+ $localFilename = __DIR__.'/cilex.phar';
+
+ file_put_contents($localFilename, file_get_contents($remoteFilename));
+ break;
+
+ case 'check':
+ $latest = trim(file_get_contents('http://cilex.github.com/get/version'));
+
+ if ($latest != Cilex\Application::VERSION) {
+ printf("A newer Cilex version is available (%s).\n", $latest);
+ } else {
+ print("You are using the latest Cilex version.\n");
+ }
+ break;
+
+ case 'version':
+ printf("Cilex version %s\n", Cilex\Application::VERSION);
+ break;
+
+ default:
+ printf("Unknown command '%s' (available commands: version, check, and update).\n", $_SERVER['argv'][1]);
+ }
+
+ exit(0);
+}
+
+__HALT_COMPILER();
+EOF;
+ }
+
+ /**
+ * Removes whitespace from a PHP source string while preserving line numbers.
+ *
+ * Based on Kernel::stripComments(), but keeps line numbers intact.
+ *
+ * @param string $source A PHP string
+ *
+ * @return string The PHP string with the whitespace removed
+ */
+ static public function stripWhitespace($source)
+ {
+ if (!function_exists('token_get_all')) {
+ return $source;
+ }
+
+ $output = '';
+ foreach (token_get_all($source) as $token) {
+ if (is_string($token)) {
+ $output .= $token;
+ } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
+ $output .= str_repeat("\n", substr_count($token[1], "\n"));
+ } elseif (T_WHITESPACE === $token[0]) {
+ // reduce wide spaces
+ $whitespace = preg_replace('{[ \t]+}', ' ', $token[1]);
+ // normalize newlines to \n
+ $whitespace = preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace);
+ // trim leading spaces
+ $whitespace = preg_replace('{\n +}', "\n", $whitespace);
+ $output .= $whitespace;
+ } else {
+ $output .= $token[1];
+ }
+ }
+
+ return $output;
+ }
+}
44 src/Cilex/Provider/ConfigServiceProvider.php
View
@@ -0,0 +1,44 @@
+<?php
+
+/*
+ * This file is part of the Cilex framework.
+ *
+ * (c) Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Cilex\Provider;
+
+use Cilex\Application;
+use Cilex\ServiceProviderInterface;
+use Symfony\Component\Yaml;
+
+class ConfigServiceProvider implements ServiceProviderInterface
+{
+ public function register(Application $app)
+ {

The brackets are on the wrong lines, should be the same line as the control statement.

Mike van Riel Owner
mvriel added a note

you are right; will be fixed in next push

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $app['config'] = $app->share(function () use ($app)
+ {
+ switch (strtolower(end(explode('.', $app['config.path']))))
+ {
+ case 'yml':
+ $parser = new Yaml\Parser();
+ $result = new \ArrayObject(
+ $parser->parse($app['config.path'])
+ );
+ break;
+ case 'xml':
+ $result = simplexml_load_file($app['config.path']);
+ break;
+ default:
+ throw new \InvalidArgumentException(
+ 'Unable to load configuration; the provided file '
+ .'extension was not recognized. Only yml or xml allowed'
+ );
+ }
+ return $result;
+ });
+ }
+}
134 src/Cilex/Provider/DoctrineServiceProvider.php
View
@@ -0,0 +1,134 @@
+<?php
+
+/*
+ * This file is part of the Cilex framework.
+ *
+ * (c) Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Cilex\Provider;
+
+use Cilex\Application;
+use Cilex\ServiceProviderInterface;
+use Doctrine\DBAL\DriverManager;
+use Doctrine\DBAL\Configuration;
+use Doctrine\Common\EventManager;
+
+/**
+ * Doctrine DBAL Provider.
+ *
+ * This Service Provider is an adaptation of the DoctrineServiceProvider for
+ * Silex written by Fabien Potencier.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ * @author Mike van Riel <mike.vanriel@naenius.com>
+ */
+class DoctrineServiceProvider implements ServiceProviderInterface
+{
+ public function register(Application $app)
+ {
+ $app['db.default_options'] = array(
+ 'driver' => 'pdo_mysql',
+ 'dbname' => null,
+ 'host' => 'localhost',
+ 'user' => 'root',
+ 'password' => null,
+ );
+
+ $app['dbs.options.initializer'] = $app->protect(function () use ($app) {
+ static $initialized = false;
+
+ if ($initialized) {
+ return;
+ }
+
+ $initialized = true;
+
+ if (!isset($app['dbs.options'])) {
+ $app['dbs.options'] = array('default' => isset($app['db.options']) ? $app['db.options'] : array());
+ }
+
+ $tmp = $app['dbs.options'];
+ foreach ($tmp as $name => &$options) {
+ $options = array_replace($app['db.default_options'], $options);
+
+ if (!isset($app['dbs.default'])) {
+ $app['dbs.default'] = $name;
+ }
+ }
+ $app['dbs.options'] = $tmp;
+ });
+
+ $app['dbs'] = $app->share(function () use ($app) {
+ $app['dbs.options.initializer']();
+
+ $dbs = new \Pimple();
+ foreach ($app['dbs.options'] as $name => $options) {
+ if ($app['dbs.default'] === $name) {
+ // we use shortcuts here in case the default has been overriden
+ $config = $app['db.config'];
+ $manager = $app['db.event_manager'];
+ } else {
+ $config = $app['dbs.config'][$name];
+ $manager = $app['dbs.event_manager'][$name];
+ }
+
+ $dbs[$name] = DriverManager::getConnection($options, $config, $manager);
+ }
+
+ return $dbs;
+ });
+
+ $app['dbs.config'] = $app->share(function () use ($app) {
+ $app['dbs.options.initializer']();
+
+ $configs = new \Pimple();
+ foreach ($app['dbs.options'] as $name => $options) {
+ $configs[$name] = new Configuration();
+ }
+
+ return $configs;
+ });
+
+ $app['dbs.event_manager'] = $app->share(function () use ($app) {
+ $app['dbs.options.initializer']();
+
+ $managers = new \Pimple();
+ foreach ($app['dbs.options'] as $name => $options) {
+ $managers[$name] = new EventManager();
+ }
+
+ return $managers;
+ });
+
+ // shortcuts for the "first" DB
+ $app['db'] = $app->share(function() use ($app) {
+ $dbs = $app['dbs'];
+
+ return $dbs[$app['dbs.default']];
+ });
+
+ $app['db.config'] = $app->share(function() use ($app) {
+ $dbs = $app['dbs.config'];
+
+ return $dbs[$app['dbs.default']];
+ });
+
+ $app['db.event_manager'] = $app->share(function() use ($app) {
+ $dbs = $app['dbs.event_manager'];
+
+ return $dbs[$app['dbs.default']];
+ });
+
+ if (isset($app['db.dbal.class_path'])) {
+ $app['autoloader']->registerNamespace('Doctrine\\DBAL', $app['db.dbal.class_path']);
+ }
+
+ if (isset($app['db.common.class_path'])) {
+ $app['autoloader']->registerNamespace('Doctrine\\Common', $app['db.common.class_path']);
+ }
+ }
+}
70 src/Cilex/Provider/MonologServiceProvider.php
View
@@ -0,0 +1,70 @@
+<?php
+
+/*
+ * This file is part of the Cilex framework.
+ *
+ * (c) Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Cilex\Provider;
+
+use Monolog\Logger;
+use Monolog\Handler\StreamHandler;
+
+use Cilex\Application;
+use Cilex\ServiceProviderInterface;
+
+/**
+ * Monolog Provider.
+ *
+ * This class is an adaptation of the Silex MonologServiceProvider written by
+ * Fabien Potencier.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ * @author Mike van Riel <mike.vanvriel@naenius.com>
+ */
+class MonologServiceProvider implements ServiceProviderInterface
+{
+ public function register(Application $app)
+ {
+ $app['monolog'] = $app->share(function () use ($app)
+ {
+ $log = new Logger(isset($app['monolog.name']) ? $app['monolog.name'] : 'myapp');
+
+ $app['monolog.configure']($log);
+
+ return $log;
+ });
+
+ $app['monolog.configure'] = $app->protect(function ($log) use ($app)
+ {
+ $log->pushHandler($app['monolog.handler']);
+ });
+
+ $app['monolog.handler'] = function () use ($app)
+ {
+ return new StreamHandler($app['monolog.logfile'], $app['monolog.level']);
+ };
+
+ if (!isset($app['monolog.level']))
+ {
+ $app['monolog.level'] = function ()
+ {
+ return Logger::DEBUG;
+ };
+ }
+
+ if (isset($app['monolog.class_path']))
+ {
+ $app['autoloader']->registerNamespace('Monolog', $app['monolog.class_path']);
+ }
+
+ $app->error(function (\Exception $e) use ($app)
+ {
+ $app['monolog']->addError($e->getMessage());
+ });
+ }
+}
55 src/Cilex/Provider/ValidatorServiceProvider.php
View
@@ -0,0 +1,55 @@
+<?php
+
+/*
+ * This file is part of the Cilex framework.
+ *
+ * (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 Cilex\Provider;
+
+use Cilex\Application;
+use Cilex\ServiceProviderInterface;
+
+use Symfony\Component\Validator\Validator;
+use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
+use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
+use Symfony\Component\Validator\ConstraintValidatorFactory;
+
+/**
+ * Symfony Validator component Provider.
+ *
+ * This class is an adaptation of the Silex MonologServiceProvider written by
+ * Fabien Potencier.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ * @author Mike van Riel <mike.vanvriel@naenius.com>
+ */
+class ValidatorServiceProvider implements ServiceProviderInterface
+{
+ public function register(Application $app)
+ {
+ $app['validator'] = $app->share(function () use ($app)
+ {
+ return new Validator($app['validator.mapping.class_metadata_factory'], $app['validator.validator_factory']);
+ });
+
+ $app['validator.mapping.class_metadata_factory'] = $app->share(function () use ($app)
+ {
+ return new ClassMetadataFactory(new StaticMethodLoader());
+ });
+
+ $app['validator.validator_factory'] = $app->share(function ()
+ {
+ return new ConstraintValidatorFactory();
+ });
+
+ if (isset($app['validator.class_path']))
+ {
+ $app['autoloader']->registerNamespace('Symfony\\Component\\Validator', $app['validator.class_path']);
+ }
+ }
+}
27 src/Cilex/ServiceProviderInterface.php
View
@@ -0,0 +1,27 @@
+<?php
+
+/*
+ * This file is part of the Cilex framework.
+ *
+ * (c) Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Cilex;
+
+/**
+ * Interface that must implement all Cilex service providers.
+ *
+ * @author Mike van Riel <mike.vanriel@naenius.com>
+ */
+interface ServiceProviderInterface
+{
+ /**
+ * Registers services on the given app.
+ *
+ * @param Application $app An Application instance
+ */
+ function register(Application $app);
+}
102 tests/Cilex/Tests/ApplicationTest.php
View
@@ -0,0 +1,102 @@
+<?php
+
+/*
+ * This file is part of the Cilex framework.
+ *
+ * (c) Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Cilex\Tests;
+
+use \Cilex\Application;
+
+/**
+ * Mock class used to test the register method.
+ */
+class ServiceProviderMock implements \Cilex\ServiceProviderInterface
+{
+ /**
+ * Mock method to satisfy interface
+ *
+ * @param \Cilex\Application $app
+ *
+ * @return void
+ */
+ function register(\Cilex\Application $app)
+ {
+ $app['mock'] = $this;
+ }
+}
+
+/**
+ * Application test cases.
+ *
+ * @author Mike van Riel <mike.vanriel@naenius.com>
+ */
+class ApplicationTest extends \PHPUnit_Framework_TestCase
+{
+ const NAME = 'Test';
+ const VERSION = '1.0.1';
+
+ /** @var \Cilex\Application */
+ protected $fixture = null;
+
+ /**
+ * Sets up the test fixture.
+ */
+ public function setUp()
+ {
+ $this->fixture = new Application(self::NAME, self::VERSION);
+ }
+
+ /**
+ * Tests whether the constructor instantiates the correct dependencies and
+ * correctly sets the name on the Console's Application.
+ */
+ public function testConstruct()
+ {
+ $this->assertInstanceOf(
+ '\\Symfony\\Component\\Console\\Application',
+ $this->fixture['console']
+ );
+ $this->assertInstanceOf(
+ '\Symfony\Component\ClassLoader\UniversalClassLoader',
+ $this->fixture['autoloader']
+ );
+
+ $this->assertEquals(self::NAME, $this->fixture['console']->getName());
+ $this->assertEquals(self::VERSION, $this->fixture['console']->getVersion());
+ }
+
+ /**
+ * Tests the command method to see if the command is properly set and the
+ * Cilex application is added as container.
+ */
+ public function testCommand()
+ {
+ $this->assertFalse($this->fixture['console']->has('demo:greet'));
+ $this->fixture->command(new \Cilex\Command\GreetCommand());
+ $this->assertTrue($this->fixture['console']->has('demo:greet'));
+
+ $this->assertSame(
+ $this->fixture,
+ $this->fixture['console']->get('demo:greet')->getContainer()
+ );
+ }
+
+ /**
+ * Tests whether the register method applies the provided parameters to this
+ * application and correctly registers the ServiceProvider.
+ */
+ public function testRegister()
+ {
+ $provider = new ServiceProviderMock();
+ $this->fixture->register($provider, array('mock.param' => true));
+
+ $this->assertTrue($this->fixture['mock.param']);
+ $this->assertSame($this->fixture['mock'], $provider);
+ }
+}
64 tests/Cilex/Tests/Command/CommandTest.php
View
@@ -0,0 +1,64 @@
+<?php
+
+/*
+ * This file is part of the Cilex framework.
+ *
+ * (c) Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Cilex\Tests\Command;
+
+use \Cilex\Command;
+
+class CommandMock extends Command\Command {}
+
+/**
+ * Command\Command test cases.
+ *
+ * @author Mike van Riel <mike.vanriel@naenius.com>
+ */
+class CommandTest extends \PHPUnit_Framework_TestCase
+{
+ /** @var \Cilex\Command\Command */
+ protected $fixture = null;
+
+ /**
+ * Sets up the test fixture.
+ */
+ public function setUp()
+ {
+ $this->fixture = new CommandMock('demo:test');
+ }
+
+ /**
+ * Tests the getContainer and setContainer methods to see whether the
+ * provided Cilex Application is persisted.
+ */
+ public function testContainer()
+ {
+ $app = new \Cilex\Application('Test');
+
+ $this->assertNull($this->fixture->getContainer());
+
+ $this->fixture->setContainer($app);
+ $this->assertSame($app, $this->fixture->getContainer());
+ }
+
+ /**
+ * Tests whether the getService method correctly retrieves an element from
+ * the container.
+ */
+ public function testGetService()
+ {
+ $app = new \Cilex\Application('Test');
+ $this->fixture->setContainer($app);
+
+ $this->assertInstanceOf(
+ '\Symfony\Component\ClassLoader\UniversalClassLoader',
+ $app['autoloader']
+ );
+ }
+}
52 tests/Cilex/Tests/Provider/ValidatorServiceProviderTest.php
View
@@ -0,0 +1,52 @@
+<?php
+
+/*
+ * This file is part of the Cilex framework.
+ *
+ * (c) Mike van Riel <mike.vanriel@naenius.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Cilex\Tests\Provider;
+
+use Cilex\Application;
+use Cilex\Provider\ValidatorServiceProvider;
+
+/**
+ * ValidatorServiceProvider.
+ *
+ * Originally provided with the Silex Framework; test has been adapted for Cilex.
+ *
+ * @author Javier Lopez <f12loalf@gmail.com>
+ * @author Mike van Riel <mike.vanriel@naenius.com>
+ */
+class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ if (!is_dir(__DIR__.'/../../../../vendor/symfony/validator/Symfony/Component/Validator')) {
+ $this->markTestSkipped('Validator submodule was not installed.');
+ }
+ }
+
+ public function testRegister()
+ {
+ $app = new Application('Test');
+
+ $app->register(new ValidatorServiceProvider(), array(
+ 'validator.class_path' => __DIR__.'/../../../../vendor/Symfony/Component/Validator'
+ ));
+
+ return $app;
+ }
+
+ /**
+ * @depends testRegister
+ */
+ public function testValidatorServiceIsAValidator($app)
+ {
+ $this->assertInstanceOf('Symfony\Component\Validator\Validator', $app['validator']);
+ }
+}
Henrik Bjørnskov

constants dosent have a @var annotation in its php doc.

/**
  * Cilex Version number
  */
Henrik Bjørnskov

why? Shouldnt version always be the current Cilex version?

Henrik Bjørnskov

Bracket should be on the same line as the if statement.

Henrik Bjørnskov

should be multiline. and @var $container Cilex\Application

Henrik Bjørnskov

The brackets are on the wrong lines, should be the same line as the control statement.

Mike van Riel

The version number is the version of the CLI tool that uses Cilex and not of Cilex itself. This version number is displayed on the help output.

Mike van Riel

Will be fixed in next push

Mike van Riel

I do not know whether the first is Sf2 coding standard and will apply, the latter is not according to PHPDoc: http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.var.pkg.html

Mike van Riel

you are right; will be fixed in next push

Mike van Riel

From a conceptual point of view I disagree but the current de-facto PHPDoc standard does not prescribe what tag to use to identify a constant's type. Seeing as I have too little Sf2 coding standards I have applied the suggestion; will be included in next push.

Please sign in to comment.
Something went wrong with that request. Please try again.