Commit 0a6309a7 authored by Fabien Potencier's avatar Fabien Potencier

added initial set of files for Silex

parents
[submodule "vendor/symfony"]
path = vendor/symfony
url = git://github.com/fabpot/symfony.git
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.
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
<?php
require_once __DIR__.'/src/autoload.php';
use Silex\Compiler;
$compiler = new Compiler();
$compiler->compile();
<?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();
File added
<?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;
}
}
<?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);
}
}
<?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();
symfony @ 1990fc54
Subproject commit 1990fc543b9629f91328846a5d8c2905baba4ce3
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment