Permalink
Browse files

added initial set of files for Silex

  • Loading branch information...
fabpot committed Sep 16, 2010
0 parents commit 0a6309a74c96c5c15f3c87ba3497680a4c249e0b
Showing with 247 additions and 0 deletions.
  1. +3 −0 .gitmodules
  2. +19 −0 LICENSE
  3. +45 −0 README.md
  4. +8 −0 compile.php
  5. +22 −0 example.php
  6. BIN silex.phar
  7. +74 −0 src/Silex/Compiler.php
  8. +63 −0 src/Silex/Framework.php
  9. +12 −0 src/autoload.php
  10. +1 −0 src/vendor/symfony
@@ -0,0 +1,3 @@
+[submodule "vendor/symfony"]
+ path = vendor/symfony
+ url = git://github.com/fabpot/symfony.git
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2010 Fabien Potencier
+
+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,0 +1,45 @@
+Silex, a simple Web Framework
+=============================
+
+Silex is a simple web framework to develop simple websites:
+
+ require_once __DIR__.'/silex.phar';
+
+ use Symfony\Component\HttpFoundation\Response;
+ use Silex\Framework;
+
+ $framework = new Framework(array(
+ '/hello/:name' => function ($name)
+ {
+ return new Response('Hello '.$name);
+ },
+ ));
+
+ $framework->handle()->send();
+
+Silex is based on [Symfony2][1].
+
+Requirements
+------------
+
+Silex works with PHP 5.3.2 or later.
+
+Installation
+------------
+
+Installing Silex is as easy as it can get. Download the [`Silex.phar`][2] file
+and you're done!
+
+More Information
+----------------
+
+Read the documentation of Symfony2 for more information about how you can
+leverage Symfony2 features.
+
+License
+-------
+
+Silex is licensed under the MIT license.
+
+[1]: http://symfony-reloaded.org/
+[2]: http://github.com/fabpot/silex/blob/master/silex.phar
@@ -0,0 +1,8 @@
+<?php
+
+require_once __DIR__.'/src/autoload.php';
+
+use Silex\Compiler;
+
+$compiler = new Compiler();
+$compiler->compile();
@@ -0,0 +1,22 @@
+<?php
+
+require_once __DIR__.'/silex.phar';
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Silex\Framework;
+
+$hello = function ($name)
+{
+ return new Response('Hello '.$name);
+};
+
+$framework = new Framework(array(
+ '/hello/:name' => $hello,
+));
+
+// Simulate a request without a Client
+//$request = Request::create('/hello/Fabien');
+//$framework->handle($request)->send();
+
+$framework->handle()->send();
Binary file not shown.
@@ -0,0 +1,74 @@
+<?php
+
+namespace Silex;
+
+use Symfony\Component\Finder\Finder;
+use Symfony\Framework\Kernel;
+
+/*
+ * This file is part of the Goutte utility.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.org>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * The Compiler class compiles the Silex framework.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.org>
+ */
+class Compiler
+{
+ public function compile($pharFile = 'silex.phar')
+ {
+ if (file_exists($pharFile)) {
+ unlink($pharFile);
+ }
+
+ $phar = new \Phar($pharFile, 0, 'Silex');
+ $phar->setSignatureAlgorithm(\Phar::SHA1);
+
+ $phar->startBuffering();
+
+ $finder = new Finder();
+ $finder->files()->name('*.php')->exclude('tests')->in(__DIR__.'/..');
+ foreach ($finder as $file) {
+ $path = str_replace(realpath(__DIR__.'/../..').'/', '', realpath($file));
+ $content = Kernel::stripComments(file_get_contents($file));
+
+ $phar->addFromString($path, $content);
+ }
+
+ // Stubs
+ $phar['_cli_stub.php'] = $this->getStub();
+ $phar['_web_stub.php'] = $this->getStub();
+ $phar->setDefaultStub('_cli_stub.php', '_web_stub.php');
+
+ $phar->stopBuffering();
+
+ // $phar->compressFiles(\Phar::GZ);
+
+ unset($phar);
+ }
+
+ protected function getStub()
+ {
+ return <<<EOF
+<?php
+/*
+ * This file is part of the Silex utility.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.org>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+require_once __DIR__.'/src/autoload.php';
+
+__HALT_COMPILER();
+EOF;
+ }
+}
@@ -0,0 +1,63 @@
+<?php
+
+namespace Silex;
+
+use Symfony\Component\HttpKernel\HttpKernel;
+use Symfony\Component\HttpKernel\Controller\ControllerResolver;
+use Symfony\Component\EventDispatcher\EventDispatcher;
+use Symfony\Component\EventDispatcher\Event;
+use Symfony\Component\Routing\RouteCollection;
+use Symfony\Component\Routing\Route;
+use Symfony\Component\Routing\Matcher\UrlMatcher;
+
+/*
+ * This file is part of the Goutte utility.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.org>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * The Silex framework class.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.org>
+ */
+class Framework extends HttpKernel
+{
+ protected $routes;
+
+ public function __construct($map)
+ {
+ $this->routes = new RouteCollection();
+ foreach ($map as $pattern => $to) {
+ $route = new Route($pattern, array('_controller' => $to));
+ $this->routes->addRoute(str_replace(array('/', ':'), '_', $pattern), $route);
+ }
+
+ $dispatcher = new EventDispatcher();
+ $dispatcher->connect('core.request', array($this, 'parseRequest'));
+ $resolver = new ControllerResolver();
+
+ parent::__construct($dispatcher, $resolver);
+ }
+
+ public function parseRequest(Event $event)
+ {
+ $request = $event['request'];
+
+ $matcher = new UrlMatcher($this->routes, array(
+ 'base_url' => $request->getBaseUrl(),
+ 'method' => $request->getMethod(),
+ 'host' => $request->getHost(),
+ 'is_secure' => $request->isSecure(),
+ ));
+
+ if (false === $attributes = $matcher->match($request->getPathInfo())) {
+ return false;
+ }
+
+ $request->attributes->add($attributes);
+ }
+}
@@ -0,0 +1,12 @@
+<?php
+
+require_once __DIR__.'/vendor/symfony/src/Symfony/Framework/UniversalClassLoader.php';
+
+use Symfony\Framework\UniversalClassLoader;
+
+$loader = new UniversalClassLoader();
+$loader->registerNamespaces(array(
+ 'Symfony' => __DIR__.'/vendor/symfony/src',
+ 'Silex' => __DIR__,
+));
+$loader->register();
Submodule symfony added at 1990fc

0 comments on commit 0a6309a

Please sign in to comment.