I have to make modifications on a Symfony project and I am currently wondering how this project works.
It has a services.php file in Company/Bundle/Resources/config that manages the instantiation of services, but it is not imported anywhere...
I looked in app/config/config.yml, config_dev.yml, config_prod.yml, security.yml, etc and there are no imports for this file!
I know it used by the application, but I have no idea how it is imported :/.
Does it make use of the DependenctInjection service? Have a look at the docs here
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
$container = new ContainerBuilder();
$loader = new PhpFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.php');
This will autoload the service when the symfony app is initialized.
Related
I'm trying something so much time but maybe it's not even possible.
Sorry for my bad language.
So, I followed Symfony Doc https://symfony.com/doc/current/bundles.html to create new Bundle, and than I followed https://symfony.com/doc/current/bundles/extension.html to create DI extension.
My files:
AcmeHelloExtension.php
namespace App\Acme\HelloBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class AcmeHelloExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('services.xml');
}
}
AcmeHelloBundle.php
namespace App\Acme\HelloBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeHelloBundle extends Bundle
{
}
I added it to config/bundles.php
src/Acme/HelloBundle/Resources/config/services.yaml
services:
App\Acme\HelloBundle\AcmeHelloBundle:
tags: ['example-tags']
This service file isn't auto loaded, do I need to do next steps or it should work? When I check it with debug:container ....bundle...
Option-Tags has empty value. When I put this code in config/services.yaml it works.
The basic problem was that the bundle's source code was located under the project's src directory:
project
src
Ztest
ZtestBundle.php
This in turn was causing the bundle's services to be be autowired by the application's config/services.yaml file:
# project\config\services
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
...
- '../src/Ztest' # Add this to fix the problem
Excluding the bundle's source code fixed the issues. Autowiring at the application level overrides any manual wiring done at the bundle level.
Of course in general if you do decide you need a bundle then it's source code should be in it's own independent directory:
project
src
src-ztest-bundle
For this to work you also need to update the psr-4 section of composer.json and run "composer dump-autoload".
Keep in mind that in Symfony 4+, the only recommended usage of custom bundles is for code shared across multiple Symfony applications. In which case, the bundle should ultimately end up in it's own repository and composer package.
However, custom bundles inside of an application are still supported and there are times where they come in useful.
I'm pretty new to PHP and Symfony and I just don't seem to use the Filesystem Component right.
I've followed the offical documentation, found under:
https://symfony.com/doc/current/components/using_components.html
and tried to install it, but I just dont get where I'm supposed to write the code to.
The docs say:
Once Composer has downloaded the component(s), all you need to do is include the vendor/autoload.php file that was generated by Composer. This file takes care of autoloading all of the libraries so that you can use them immediately:
// File example: src/script.php
// update this to the path to the "vendor/"
// directory, relative to this file
require_once __DIR__.'/../vendor/autoload.php';
use Symfony\Component\Finder\Finder;
$finder = new Finder();
$finder->in('../data/');
// ...
but where do I have to change this line?
in the vendor/autoload.php? Appreciate any help :)
If you are using Symfony framework, you don't need to install this component. It's altready included in symfony/symfony and is already registered.
You can simply use it like:
use Symfony\Component\Finder\Finder;
$finder = new Finder();
See this doc
Amazon's MWS PHP client library is just a zip file without any namespace, Is there a way to use this library with Laravel application or with any application which uses composer for its dependency management.
Sure, just create your own library directory in your Laravel app. I usually keep mine just inside app directory and call it Libraries. Dump the source files inside a folder such as AmazonMWS.
.config.inc.php comes with an autoloader but it won't be used. Instead you could probably just open your composer.json in your Laravel project and tell it to autoload your new Library directory targeting the config. Ex:
"autoload-dev": {
"classmap": [
"app/Libraries/AmazonMWS/Client.php"
]
}
Make sure config.inc.php is accessible at the AmazonMWS root.
Then run composer dump-autoload to regenerate the autoloaders. If done properly then you should be able to instantiate any of the MWS classes without a namespace.
In your Controller, include the following use:
use \MarketplaceWebServiceProducts_Client;
Now you can call your service as expected:
$config = [...];
$service = new MarketplaceWebServiceProducts_Client(
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
APPLICATION_NAME,
APPLICATION_VERSION,
$config);
Or just omit use \MarketplaceWebServiceProducts_Client; and instantiate directly with namespace prefix, your choice.
I'm building an app using Silex (the micro-framework).
As my app are growing in size and the need of using the same code in several routes rises, I want to organise everything a little more..
My idea is to create some custom classes, and then share them with my app:
$app['test'] = $app->share(function () {
require_once('../vendor/acme/src/test.php');
$testClass = new Test();
return new $testClass;
});
This actually works, but I need help with the following:
Autoload the class with composer (the way its supposed to be done in Silex).
Be able to use the existing Doctrine DBAL connection and methods within my class.
I hope someone can give me some tips how to get on, because I'm not finding the Silex docs very useful and I'm a beginner with both Silex and Composer.
Check the composer docs about autoloading, and when you added your config you should run composer dump-autoload to regenerate the composer autoloader. Then your require_once should not be necessary anymore.
Most likely this will work (assuming class Test is in src/Test.php):
{
"autoload": {
"psr-0": {
"": "src/"
}
}
}
This will make any PSR-0 compliant class inside src/ autoloadable.
Regarding your second point (using DBAL in your class), you should configure your class as a silex service that accesses the db service. Read up on services at http://silex.sensiolabs.org/doc/services.html
I am wanting to use the Symfony\Component\Process\ProcessBuilder class and can see that it is included as part of the Silex codebase within the vendors folder. I am using the Silex phar file and assume that because I can readily instantiate other Symfony components like Request, Response and so on that it will correctly locate the file to include when I use the full namespace.
$foo = new Symfony\Component\HttpFoundation\Request(); //works fine
However, when I try and create and instance of it using:
$foo = new Symfony\Component\Process\ProcessBuilder(); //class not found
It gives me a class not found error. Does anyone know why this is and how I can use this class from the Silex phar without including the component seperately within my project?
It looks like the Process Symfony component is not included in the compiled Silex phar file.