Symfony pass -env parameter when I run composer commands - php

When I create a docker entrypoint script for my project I run:
php /bin/composer install --no-dev
But thhat throws an exception that is:
Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "AjglBreakpointTwigExtensionBundle" from namespace "Ajgl\Twig\Extension\SymfonyBundle".
The code over the AppKernel.php that initializes the bundle is:
public function registerBundles()
{
//Other Bundle initialization
if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
$bundles[] = new Ajgl\Twig\Extension\SymfonyBundle\AjglBreakpointTwigExtensionBundle();
}
return $bundles;
}
This bundle I use int on dev and test environments where it has use. The container I build is for production use.
Therefore I want somehow to let the ScriptHandlers that when composer executes, theese run over a prod environment and not a dev one in order to supress the error message?

I suppose this is a duplicate of this issue.
Just set the SYMFONY_ENV var to prod and use composer install then:
SYMFONY_ENV=prod php /bin/composer install --no-dev

Related

BitBucket pipeline fails on test cases

I'm having hard time to configure a BitBucket pipeline to deploy a CakePHP application to a hosting server.
Reading some tutorials I've ended up with this pipeline:
image: edbizarro/bitbucket-pipelines-php7
pipelines:
branches:
master:
- step:
caches:
- composer
script:
- composer install --no-interaction --no-progress --prefer-dist
- composer test
- composer deploy-to-production
but it always fails:
Build setup -> OK
composer install -> OK
composer test -> FAIL
+composer test
phpunit --colors=always
Deprecated Error: Plugin::load() is deprecated. Use Application::addPlugin() instead. This method will be removed in 4.0.0. - /opt/atlassian/pipelines/agent/build/config/bootstrap.php, line: 179
You can disable deprecation warnings by setting Error.errorLevel to E_ALL & ~E_USER_DEPRECATED in your config/app.php. in [/opt/atlassian/pipelines/agent/build/vendor/cakephp/cakephp/src/Core/functions.php, line 311]
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.
Exception: Unable to insert fixtures for "App\Test\TestCase\Controller\CustomersControllerTest" test case. SQLSTATE[HY000] [2002] No such file or directory in [/opt/atlassian/pipelines/agent/build/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureManager.php, line 380]
Script phpunit --colors=always handling the test event returned with error code 244
I cannot ls the virtual remote folders but I can mine... so I inspected App\Test\TestCase\Controller\CustomersControllerTest:
<?php
namespace App\Test\TestCase\Controller;
use App\Controller\CustomersController;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
class CustomersControllerTest extends TestCase
{
use IntegrationTestTrait;
public $fixtures = [
'app.Customers',
'app.Orders'
];
public function testIndex()
{
$this->markTestIncomplete('Not implemented yet.');
}
public function testView()
{
$this->markTestIncomplete('Not implemented yet.');
}
public function testAdd()
{
$this->markTestIncomplete('Not implemented yet.');
}
public function testEdit()
{
$this->markTestIncomplete('Not implemented yet.');
}
public function testDelete()
{
$this->markTestIncomplete('Not implemented yet.');
}
}
Because I'm not using tests, can I (safely) avoid the composer test step?
By the way, on the hosting server the PHP version is 5.6 while in the pipeline's image is specified version 7. Might this lead to a problem?

How to force Composer script to load local class instead of global class

undefined method
(Relevant files linked at the bottom of my question.)
I let Composer run some post-install-cmd and post-update-cmd scripts. In my script I want to make use of the readlink() function from symfony/filesystem. Inside my projects /vendor folder there is the 3.4 version of the filesystem package, fine.
I use Symfony\Component\Filesystem\Filesystem; at the top of my file.
But whenever I run:
$fs = new Filesystem();
$path = '/path/to/some/symlink';
if ($fs->readlink($path)) {
// code
}
I get the following error which tells me I'm calling an undefined method:
PHP Fatal error: Uncaught Error: Call to undefined method
Symfony\Component\Filesystem\Filesystem::readlink() in
/Users/leymannx/Sites/blog/scripts/composer/ScriptHandler.php:160
OK, so I double-checked the class inside my project's /vendor folder. This method is there. My IDE points me there. But when I run:
$fs = new Filesystem();
get_class_methods($fs);
this method is not listed.
Which file is it trying to load the method from?
OK, so I tried to check which file it's loading this class from:
$fs = new Filesystem();
$a = new \ReflectionClass($fs);
echo $a->getFileName();
and that returns me phar:///usr/local/Cellar/composer/1.7.2/bin/composer/vendor/symfony/filesystem/Filesystem.php – But why? Why is it taking the package from my Mac's Cellar? That's odd.
But OK, so I thought that's a Homebrew issue, and uninstalled the Homebrew Composer $ brew uninstall --force composer and installed it again as PHAR like documented on https://getcomposer.org/doc/00-intro.md#globally.
But now it's the same.
$fs = new Filesystem();
$a = new \ReflectionClass($fs);
echo $a->getFileName();
returns me phar:///usr/local/bin/composer/vendor/symfony/filesystem/Filesystem.php.
But why? Why does it pick up the (outdated) package from my global Composer installation? How can I force my script to use the project's local class and not the one from my global Composer installation?
What else?
Initially my $PATH contained /Users/leymannx/.composer/vendor/bin /usr/local/bin /usr/bin /bin /usr/sbin /sbin. I removed /Users/leymannx/.composer/vendor/bin to only return /usr/local/bin /usr/bin /bin /usr/sbin /sbin. Still the same.
I also tried setting the following in my composer.json. Still the same:
"optimize-autoloader": true,
"classmap-authoritative": true,
"vendor-dir": "vendor/",
I finally created an issue on GitHub: https://github.com/composer/composer/issues/7708
https://github.com/leymannx/wordpress-project/blob/master/composer.json
https://github.com/leymannx/wordpress-project/blob/master/scripts/composer/ScriptHandler.php
This is matter of context where your code is run. If you're executing some method directly in post-install-cmd it will be executed inside of Composer's process. It means that it will share all code bundled inside of composer.phar. Since you can't have two classes with the same FQN, you can't include another Symfony\Component\Filesystem\Filesystem in this context.
You can bypass this by running your script inside of separate process. You may create post-install-cmd.php file where you do all bootstrapping (like require vendor/autoload.php) and call these methods. Then run this file in your post-install-cmd hook:
"scripts": {
"post-install-cmd": [
"php post-install-cmd.php"
]
},

How to create an Aura DI container?

I installed a composer, downloaded the Aura, created an index.pxp and wrote in it:
require('vendor/autoload.php');
use Aura\Di\ContainerBuilder;
$builder = new ContainerBuilder();
$di = $builder->newInstance();
$object = $di->newInstance('Vendor\Package\ClassName');
But phpStorm says:Undefined namespase DI
And i have error: Fatal error: Class 'Aura\Di\ContainerBuilder' not found in... on line 4
I do as follows: http://auraphp.com/packages/3.x/Di/getting-started.html#1-1-1
In order for the composer auto-loader to pick up \Aura\Di, the dependency needs to be managed by composer.
You can easily do this by executing
composer require aura/di
which will add the dependency to your composer.json file and register with the auto-loader.
If you have manually downloaded and installed aura/di, you can revert that.

Class not found with Guzzle Http Client

This is a strange error that's constantly occurring.
Fatal error: Class 'Guzzle\Http\Client' not found in /home/futcoins/public_html/autobuyer/classes/shopify.php on line 15
This is the source code. I think this question is pretty straight forward and I've been stuck with this problem for a couple of days, any ideas?
use Guzzle\Http\Client;
use Guzzle\Plugin\Cookie\CookiePlugin;
use Guzzle\Plugin\Cookie\CookieJar\FileCookieJar;
class Shopify {
//initialise the class
public function __construct() {
}
public function GetOrders() {
$client = new Client(null); //Line 15 where errors occurs
$request = $client->get("url");
$response = $request->send();
$json = $response->json();
return $json;
}
}
So you have a declaration at the top
use Guzzle\Http\Client;
That means you either have an autoloader or have included the appropriate file(s) manually. So you need to find the file that has that class and include it or else PHP will be looking for code you've not given to it.
I'm not sure it is the right solution for you but I had the exact same problem and to fix it I updated composer on my server and regenerated the autoload file:
sudo /usr/bin/composer.phar self-update
/usr/bin/composer.phar dump-autoload
I'm not sure it is necessary but I also restarted apache:
sudo /etc/init.d/httpd restart
To prevent this in the future and because we are using Elastic Beanstalk I created an configuration file to make sure that composer is up to date:
commands:
01updateComposer:
command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update
option_settings:
- namespace: aws:elasticbeanstalk:application:environment
option_name: COMPOSER_HOME
value: /root
Source: http://blogs.aws.amazon.com/php/post/Tx2M04LCN1UEE0E/Reduce-Composer-Issues-on-Elastic-Beanstalk

Symfony 2 no command for generating doctrine crud

i`m trying to generate CRUD for some entities in Symfony 2, apparently the
generate:doctrine:crud command is unavailable.
[InvalidArgumentException]
Command "generate:doctrine:crud" is not defined.
also , in the list for available commands, I only get one command.
generate
generate:doctrine:entities Generates entity classes and method stubs from your mapping information
is there a bundle or something in the configuration missing, or what is the cause for not having this functionality.
Addition:
The doctrine:generate:crud command is provided by the SensioGeneratorBundle
Please also make sure you have the bundle available and registered in your app/AppKernel.php like this:
class AppKernel extends Kernel
{
public function registerBundles()
{
// ...
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
// ...
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
// ...
}
As in my example the command is also normally only available in the dev environment. Therefore ...
php app/console --env=prod doctrine:generate:crud
.. or any other configuration that uses production enviroment won't work.
doctrine:generate:crud is the command you should use
You can see a list of commands using php app/console list

Categories