Services Generator bundle for Symfony2
This package contains a bundle to easily create basic services efficiently and without effort from command line., (*1)
The package add a new command line app/console generate:service that create all the config and code for a basic service:, (*2)
- Modify the services.xml (only works for xml language only)
- Create a Lib Class
- Inject dependencies (Entity Manager)
Installation
Step 1: Install vendors
Installation depends on your version of Symfony:, (*3)
Symfony 2.1.x: Composer
Composer is a project dependency manager for PHP. You have to list
your dependencies in a composer.json
file:, (*4)
``` json
{
"require-dev": {
"jalis/service-generator": "dev-master"
}
}, (*5)
To actually install Service Generator in your project, download the composer binary and run it:
``` bash
wget http://getcomposer.org/composer.phar
# or
curl -O http://getcomposer.org/composer.phar
php composer.phar install
Symfony 2.0.x: bin/vendors.php
method
If you're using the bin/vendors.php
method to manage your vendor libraries,
add the following entries to the deps
in the root of your project file:, (*6)
[JalisServiceGeneratorBundle]
git=https://github.com/javijalis/JalisServiceGeneratorBundle.git
target=/bundles/Jalis/Bundle/ServiceGeneratorBundle
Next, update your vendors by running:, (*7)
``` bash
$ ./bin/vendors, (*8)
Finally, add the following entries to your autoloader:
``` php
<?php
// app/autoload.php
$loader->registerNamespaces(array(
// ...
'Jalis' => __DIR__.'/../vendor/bundles',
));
Step 2: Enable the bundle
Finally, enable the bundle in the kernel:, (*9)
``` php
<?php
// app/AppKernel.php, (*10)
public function registerBundles()
{
$bundles = array(
// ...
new Jalis\Bundle\ServiceGeneratorBundle\JalisServiceGeneratorBundle(),
);
}, (*11)
## How to use:
Execute the command:
``` php
$ app/console generate:service
Follow the instructions:, (*12)
``` php
Your service code must be written in Manager directory. This command helps
you generate them easily., (*13)
Each service is hosted under a namespace (like Acme/Bundle/BlogBundle).
(which must have Bundle as a suffix)., (*14)
Bundle namespace: myFolder/Bundle/myBundle, (*15)
Your service must have a name for call it, (*16)
Service Name: example, (*17)
Your service need EntityManager?, (*18)
Do you need entity Manager in your service [no]? yes, (*19)
After that, you only have to use the service where ever you want:
``` php
$my_service = $this->get('exampleManager');
The class for your code used in the service is in the folder BundleGiven/Manager/exampleManager.php
``` php
<?php
namespace BundleGiven\Manager;, (*20)
use Doctrine\ORM\EntityManager;, (*21)
class exampleManager
{
protected $em;, (*22)
public function __construct(EntityManager $em){
$this->em = $em;
}
public function getInfo() {
return "name: exampleManager";
}
//... your code
}
```, (*23)
ToDo
- Option for create Twig extensions
- Add more options for inject more native services (monolog, mailer) and own services
- Refactor Command class code
- What about services in yml?