Setting PHP environment variable on GAE dev mode (dev_appserver.py) - php

I have Zend Framework project running on Google App Engine. Everything works great, with the exception of one thing:
I can't set an environment variable to tell Zend that I'm in development mode
Any ideas?
(I'm using PHP 5.4 in Ubuntu)
What I've tried
(Obviously I don't want to set the variable through app.yaml... I can't set it through .htaccess because it's not used. I would rather not have to add any conditions in my code to possibly set the variable that way, either)
I'm using PHPStorm (IDE), which has a nice plugin for GAE. It even has an option in the project configurations for environment variables - except that it doesn't work. I'm setting the variable in PHPStorm (Edit Configurations > Google App Engine for PHP > Command Line > Environment Variables), but if I do a
getenv()
from code, it returns
boolean false
Why
My goal in all this is to dynamically load the development configurations for my project, particuarly so I can use MySQL locally, instead of connecting up to CloudSQL while developing and testing the apps.

The solution I'm going with, based on the comments by #tim-hoffman is the following little bit of logic on my /public/index.php file, placed before the APPLICATION_ENV constant is defined:
$env = getenv("SERVER_SOFTWARE");
if ($env !== false) {
if ((bool)preg_match("/development/", strtolower($env))) {
define("APPLICATION_ENV", "development");
}
}
In development mode, SERVER_SOFTWARE will return the string "Development/X.X" (version number), and in production, it'll be "Google App Engine/X.Y.Z"

Related

How to set up a staging environment based on a symlink to prod configuration, without showing debug information?

I would like to setup a staging environment with the same configuration as the prod environment.
According to the docs, I proceed as follow:
I create a staging symlink that points on prod
configure the env in .env: APP_ENV=staging
clear the cache: php bin/console cache:clear
ask for an URL that does not exist to trigger a 404 error: http://localhost:8080/an-url-that-does-not-exists
When the APP_ENV=prod, my custom error page is render properly, but when APP_ENV=staging, the debug message NotFoundHttpException is rendered?
The profiler is not displayed.
What am I missing?
tldr;
Crate an .env.staging file and use it to set APP_DEBUG to 0. Debug mode and environment are set independently.
By default, unless you set it explicitly debug mode (APP_DEBUG) is set from the environment automagically.
This happens in the following steps:
In your front-controller (index.php, usually) you would find this line:
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
And on DotEnv::bootEnv() you'll find this:
$debug = $_SERVER[$k] ?? !\in_array($_SERVER[$this->envKey], $this->prodEnvs, true);
This will compare your APP_ENV with an array of "environments" that DotEnv, considers "production-like". By default, this array includes only prod.
You could modify the instance of the DotEnv by calling setProdEnvs():
(new Dotenv())
->setProdEnvs(['prod', 'staging'])
->bootEnv(dirname(__DIR__).'/.env');
... but generally simply disabling debug mode on your .env file would be enough.

Symfony 4 Environment variable not found composer install

I'm completly new to Symfony and tried the following guide: https://github.com/thecodingmachine/symfony-vuejs ... but without docker (I have a simple webspace, I can't use docker there).
Now I'm stuck right in the beginning, when calling composer install in the app root. I get the following message:
In EnvVarProcessor.php line 131:
Environment variable not found: "DATABASE_URL".
Well, that sounds easy, I have to setup an enviroment variable ... but I'm not using docker and I don't want to set up a temporarly variable in the shell. Few seconds of google helped me, that I can use .env for my problem like descriped here: https://symfony.com/doc/current/configuration.html#configuration-based-on-environment-variables
In the example project is already a .env file, so I extendet it by DATABASE_URL. But suddenly it is not taking that variable.
I'm working on a macbook with a simple apache/php setup without forther configuration.
What am I missing?

Get XDEBUG_SESSION_START header in Phalcon request

I am trying to remotely start debugger in multiple microservices using Phalcon, the first microservice accepts XDEBUG_SESSION_START header but I cannot get the header in the request when I instantiate the Request object so I guess that Apache is removing it after the debugger starts.
Is it exists any option or way to disable this behavior and make the Phalcon code know when the Xdebug flag is present in the Request in order to forward that flag when calling other microservices?
I know that I can set auto_start option for Xdebug but that option seems to slow down the microservices.
The final code should looks similar to:
$DEBUG = $request.getHeaders()['XDEBUG_SESSION_START']
if (!is_empty($DEBUG)) {
$headers['XDEBUG_SESSION_START'] == 1;
}
....
I think this would be OK and it will not cause problems even when code will be deployed to PROD environments.

How to mock, use or override Environment::isCli() in TYPO3 v9 unit tests

I'm trying to get a TYPO3 v8 system updated to TYPO3 v9, but when it comes to unit-testing, I got some errors. I was able to fix some of them on my own but this one here's a very difficult one for me, because unit-testing is somewhat new to me in general.
I already searched the web, the TYPO3 documentation (which seems like the important parts are missing?), asked some friends and tried some things on my own, but nothing helped.
$this->environmentMock = $this->createMock(Environment::class);
$this->environmentMock->expects($this->once())
->method("::isCli")
->will($this->returnValue(TRUE));
I'm expecting to manually override the static function ::isCli() that comes with the Environment class. If that's not possible, is there any other "workaround", like setting a protected variable or something like that?
Currently this is my error message:
Trying to configure method "::isCli" which cannot be configured because it does not exist, has not been specified, is final, or is static
Thanks in advance!
Update 1:
After using #susis tip, I get the following error when appending the code:
TypeError: Return value of TYPO3\CMS\Core\Core\Environment::getContext() must be an instance of TYPO3\CMS\Core\Core\ApplicationContext, null returned
Additional information: My project is just an extension folder with TYPO3 v9 sources required in its own composer.json. No web, no htdocs, just the extension folder.
Update 2:
Here's a full gist of my test file.
Update 3:
Even the debugger isn't helping me in this case, see attached screenshot:
xdebug phpstorm applicationcontext environment screenshot
Update 4:
I updated the gist, added the environment vars to the phpunit.xml file and added parent::setUp() to the top of the setUp() method but the error is still the same:
TypeError : Return value of TYPO3\CMS\Core\Core\Environment::getContext() must be an instance of TYPO3\CMS\Core\Core\ApplicationContext, null returned
/Users/xyz/my_redirect/public/typo3/sysext/core/Classes/Core/Environment.php:97
/Users/xyz/my_redirect/Tests/Unit/Hooks/RequestHandlerHookTest.php:41
Update 5:
I updated the gist and removed the environment settings from the phpunit.xml due to what I've seen that they didn't work either. At this moment, the test is working but I'm still not sure if it's done the right way. Thanks for your help!
You can initialize the Environment you want in your tests, for example with:
Environment::initialize(
Environment::getContext(),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath() . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
This is the same way as it is done in TYPO3 Core tests and allows you to customize the complete environment. If you are using the TYPO3 testing framework / UnitTestCase base classes, you can use the property protected $backupEnvironment = true; to make sure the environment is reset after your test.
For an example, you can have a look at the ResourceCompressorIntegrationTest

Symfony: disable exceptions in prod mode

I'm a beginner with Symfony.
I have a Symfony 3.1 project. I put y project on my remote server. Everything works fine. Then I use SensioLabsInsight to check errors in my project. This analyse tool gives me those 2 warnings messages
:
"Exceptions should not be enabled in production" and "Symfony applications should not contain a config.php file"
Then my first question is how can I disable Exceptions in production ?
My second question is may I remove the config.php file in production (located in myBundle/web/config.php) ?
Then my first question is how can I disable Exceptions in production ?
As far as I can see when searching this on Google, I think it's because the second parameter passed to new AppKernel('prod', ...); is true, which enables debug mode. Set it to false. (same applies to debug settings in config_prod.yml).
My second question is may I remove the config.php file in production (located in myBundle/web/config.php) ?
Yes, also remove any front-controller (like app_dev.php) except from your production one. Remember: The standard edition is just a recommendation. Symfony doesn't force you any file or directory in the directory structure.
In the new Symfony version, there is a new var APP_DEBUG, which you send to Kernel in the second parameter.
Just add new var to your .env file
APP_DEBUG=false
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);

Categories