hostnet / hn-dependency-injection-plugin
Introduces the Symfony DI container in a Symfony 1 application.
Installs: 43 398
Dependents: 0
Suggesters: 0
Security: 0
Stars: 15
Watchers: 8
Forks: 10
Open Issues: 0
Requires
- php: >=7.3
- doctrine/dbal: ^2.13.4||^3.1.1
- doctrine/doctrine-bundle: >=1.7.0
- doctrine/orm: >=2.6
- symfony/symfony: ^4.4||^5.0
- symfony/symfony1: ~1.1.27
Requires (Dev)
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5.6
README
- Introduces the Symfony 2 DI container in a Symfony 1 application.
- Transforms a Symfony 2 doctrine configuration into a format readable for Symfony 1.x. Use Doctrine entities and propel classes with the same database configuration!
Installation
- Download Composer.
- Run
php composer.phar require hostnet/hn-dependency-injection-plugin
- Make
apps/<app>/config/<app>Configuration
extendHostnet\HnDependencyInjectionPlugin\ApplicationConfiguration
. - [Optional] override the
getKernel
method to return your own kernel, registering the bundles you want.protected function createKernel() { return new MyKernel($this); }
class MyKernel extends Hostnet\HnDependencyInjectionPlugin\Symfony1Kernel { public function registerBundles() { $bundles = array( new Symfony\Bundle\FrameworkBundle\FrameworkBundle() ); return array_merge($bundles, parent::registerBundles()); } }
- Create
apps/<app>/config/config.yml
to configure your Doctrine dbal, Doctrine orm, and possibly the FrameworkBundle. See also the example configuration. - If you don't want to generate the propel backwards compatability layer, add this
toparameters: hn_entities_enable_backwards_compatible_connections: false
config.yml
, orparameters.yml
if you prefer to have them separate. - To ensure proper autoloading when using Doctrine entities, remove if you have
and add the following to yourrequire_once __DIR__ . '/../vendor/autoload.php';
config/ProjectConfiguration.php
.
That way Doctrine knows where to find your entities.use Doctrine\Common\Annotations\AnnotationRegistry; $loader = require __DIR__.'/../vendor/autoload.php'; AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
- Be sure to set up your permissions properly, see "Setting up your permissions".
- After this is done we can do a little cleanup. To prevent confusion you should remove
config/databases.yml
, since only the Symfony 2 configuration is read at this point.
Changelog
2.0.0
- Removed the Symfony1Panel class, it's added automatically now.
1.1.0
- Added a web debug panel with a link to the Symfony 2 profiler.
For this you need to activate the WebProfilerBundle, which you should only activate in dev
if ($this->getEnvironment() == 'dev') {
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
}
Then you need to configure the WebProfilerBundle and the profiler; add this to your config_dev.yml:
web_profiler:
toolbar: true
framework:
router: { resource: "%kernel.root_dir%/../config/sf2_routing_dev.yml" }
profiler:
only_exceptions: false
only_master_requests: true
Add this to sf2_routing_dev.yml
to make the WebProfilerBundle accessible:
_wdt:
resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix: /_wdt
_profiler:
resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler
_main:
resource: sf2_routing.yml
And in web/*.php, replace $configuration->handle($request)->send();
with:
$response = $configuration->handle($request);
$response->send();
$configuration->terminate($request, $response);
You should now have a new panel in the Symfony 1 web debug toolbar with a link to the Symfony 2 profiler!
1.0.0
- First official release
0.15
- Added
hn_entities_enable_backwards_compatible_connections
parameter
Running the unit-tests
- Clone the repository yourself
- Go to the directory of the clone
- Run
composer.phar install
- Run
phpunit
Moving the error handling to Symfony 2
When migrating, you will eventually hit a point where you want to log
errors properly. By default the sfFrontWebController
contains a try
catch block where exceptions will be caught. In order to move all error
handling to Symfony 2, you can create your own front controller. If you
remove the try catch in this front controller, all errors will
be caught by the Symfony 2 uncaught exception handler.
If your route is matched with the sf1 route but sf1 doesn't know about the
route, it will throw an sf404ErrorException
. This exception will be caught
and wrapped into an HttpNotFoundException
and the kernel.exception
will
be fired eventually. This will make sure you can handle all exceptions in
Symfony 2.
Additionally, you can throw exceptions in sf1 such as AccessDeniedException
,
NotFoundHttpException
, \RuntimeException
etc. They will all be caught. In
case of a 404 error, the sf1 fallback will only be triggered once and can only
be triggered as a first controller. Once you've succesfully entered Symfony 2
via either the sf1 route or the normal entry, it will no longer be initiated.
To add the front controller, you will have to set it in factories.yml
.
all: controller: class: MyCustomFrontController
To enable the Symfony2 404 handling, you will have to set the following parameter to true:
hn_entities_enable_symfony2_404: true