How to disable Kint in local environment under Laravel 4 - laravel-4

I'm using Kint via Composer in Laravel 4 by loading kint first in composer.json so that dd() is defined by kint, not laravel (suggested here).
I want to leave debug calls in my app, and disable Kint if not in the local environment. I'm successfully using config overrides for Anvard using the following structure:
/app/config/local/packages/provider/package_name/overridefile.php
Unfortunately, this is not working for Kint with the following structure:
/app/config/packages/raveren/kint/local/config.php or
/app/config/packages/raveren/kint/local/config.default.php
The Kint documentation states:
You can optionally copy the included config.default.php and rename to config.php to override default values…
…which works for me (/vendor/raveren/kint/config.php)
How do I achieve this:
without editing a file in the /vendor/ directory that will get overwritten by composer
so that kint is only enabled in the local envirnoment
I've also tried adding the following to a helpers.php file which is called before composer in /bootstrap/autoload.php as suggested here:
<?php
isset( $GLOBALS['_kint_settings'] ) or $GLOBALS['_kint_settings'] = array();
$_kintSettings = &$GLOBALS['_kint_settings'];
/** #var bool if set to false, kint will become silent, same as Kint::enabled(false) or Kint::$enabled = false */
$_kintSettings['enabled'] = false;
unset( $_kintSettings );
(but no dice :)
Any suggestions? TIA!

I'm not familiar with kint but checked the documentation and found that, to disable kint output, you may use (in runtime)
// to disable all output
Kint::enabled(false);
In Laravel you can check the environment using
$env = App::environment();
if($env == 'your_predefined_environment') {
Kint::enabled(false);
}
To configure your environment, you may check the documentation.
Update : I've setup my local environment as givel below (in bootstrap/start.php)
$env = $app->detectEnvironment(array(
'local' => array('*.dev'),
));
And in my local machine, I've setup a virtual mashine which has laravel4.dev as it's base url, so if I visit the app using laravel4.dev or laravel4.dev/logon then I can check the environment in my BaseController.php and it detects the local environment because of .dev
public function __construct()
{
if(App::environment() == 'local') {
// do something
}
}
In your case, I don't know where is the first debug/trace you used to print the output, so you should keep the environment checking and disabling the Kint code before you use any debug/trace but you may try this (if it works for you) but you can check the environment in your filter/routes files too.

Hmm.. I'm not sure if this is the ideal way to do it, but this works, and seems Laravel'ish:
// top of app/start/global.php
Kint::enabled(false);
and
// app/start/local.php
Kint::enabled(true);
(assuming you've got a local environment defined: see #TheAlpha's answer for more info)
http://laravel.com/docs/lifecycle#start-files

Related

PHP Laravel Server Environment Configuration

I am new with laravel and I am confused on how to setup my server environment configuration. So here is a sample code I've seen in some tutorials, but still I am not satisfied that this would be flexible enough specially when my other co-developers use it.
$env = $app->detectEnvironment(array(
'local' => ['127.0.0.1', gethostname()],
'production' => ['ipforproductionhere'] ));
You can put in local names of your and other developer PC for example:
'local' => ['yourpcname', 'yourcoworkerpcname', 'yourothercoworkerpcname'],
And of all you when developing on those machines will work in local environment.
EDIT
You can also use other type of environment detecting for example:
$env = $app->detectEnvironment(function(){
if (!isset($_SERVER['HTTP_HOST']) ||
strpos($_SERVER['HTTP_HOST'],'.') === false) {
return 'local';
}
return 'production';
});
Now assuming each developer create virtualhost without a dot and uses for example http://myproject as domain it will use local environment and if dot will be found in HTTP_HOST it will use production environment.

Laravel 4.2, Artisan::call() ignoring --env option

I'm building an application that needs to create a new database, perform migrations and seed db data via a web page.
I'm trying to achieve this with the following code in Laravel 4.2. Note, this is within a controller I've setup.
Artisan::call("migrate", array(
"--env" => "production"
));
No matter what environment I pass with the "--env" option, the environment that the migration is run on is the current environment that the site is currently running on. Ie. If I'm running on my local environment, and I run the above, it will execute the migration on the local environment which isn't what I'm looking to do.
If I run the equivalent command php artisan --env=production migrate from the command line, I get the results I'm looking to achieve. For the time being, I'm getting past this via passthru() but I'd like to take advantage of this Artisan facade if I can.
Does anyone know what's going on with this?
This isn't a pleasant way to do it, but it works.
Assuming your Artisan environment is based on $_SERVER['HTTP_HOST'] and you know the HTTP_HOST that will load your environment then you can set it manually before calling start.php
I used this to define the Artisan environment based on the base_url I was using in a Behat profile. That way I could configure fixture my database before running tests.
/**
* #BeforeSuite
*/
public static function runFixtures(SuiteEvent $suiteEvent) {
// Get the environment domain
$parameters = $suiteEvent->getContextParameters();
$baseUrl = $parameters['base_url'];
$urlParts = parse_url($baseUrl);
$_SERVER['HTTP_HOST'] = $urlParts['host'];
// Now call start.php
require_once 'bootstrap/start.php';
// Call Artisan
$stream = fopen('php://output', 'w');
Artisan::call(
'migrate:refresh',
[
'--seed' => true,
],
new StreamOutput($stream)
);
}
--env is the option to specify application's environment when the application is starting. In other words, if you specify --env option, Laravel will use your specified environment instead runs a detecting method in environment detecting method.
So, If you run artisan via CLI with --env option, In start file, artisan can detect --env option from $_SERVER variable, specify the application environment and run your command.
In contrast, when you call Artisan::call(), Laravel will resolve the console application class (Illuminate\Console\Application) and run your command. Because your application was started, then Application just runs your command without detecting environment. More over, latest version of migration command class use application environment to get a database connection
Therefore, when your call Artisan::call() the --env option is completely omitted.
Just my opinion. If you really want to avoid using passthru() function, you can rename the production database connection name in app/config/database.php to unique name e.g. production and set your default database connection to your new name. When you want to migrate production database, just call Artisan::call('migrate', array('--database' => 'production', '--force' => true)) instead of changing the environment.

Laravel Environment Configuration with Heroku

I am writing my code on Nitrous.IO using Laravel I configured my start.php to the following
$env = $app->detectEnvironment(array(
'local' => array('harrenhal-php-95199'),
'staging' => array('*.herokuapp.com'),
));
I tried this format I also tried the hostname itself both seems not to working. is there a specific practice to do so?
Once I run hostname on the shell in heroku I get this result "9e7831e0-284c-48b8-88a4-3afbbbac0b35" which changes over time.
the problem php don't detect herokuapp.com so the staging environment doesn't work
From the Laravel docs
If you need more flexible environment detection, you may pass a
Closure to the detectEnvironment method, allowing you to implement
environment detection however you wish
$env = $app->detectEnvironment(function()
{
return $_SERVER['MY_LARAVEL_ENV'];
});

Laravel, how to get the environment value?

I know that I can access the environment value using the global $env variable, but is there the right way to get this value?
You're in luck - this was just added in Beta 4 - see here for details
Added App::environment method.
Edit: these are now a number of various ways to get the environment variable as of Laravel 4.1
App::environment()
app()->environment()
app()->env
$GLOBALS['env'] // not recommended - but it is possible
You can also specifically check for if the current environment is set to 'local'
App::isLocal()
app()->isLocal()
...or 'production'
App::isProduction()
app()->isProduction()
You can also specifically check for if the current environment is set to 'testing'
App::runningUnitTests()
app()->runningUnitTests()
You can also use app()->env.
In Laravel 4 and 5, the Laravel official docs suggest using:
$environment = App::environment();
You may also pass arguments to the environment method to check if the
environment matches a given value:
if (App::environment('local'))
{
// The environment is local
}
if (App::environment('local', 'staging'))
{
// The environment is either local OR staging...
}

Changing cache_dir and log_dir with Symfony2

Because of deployment constraints, I would like to have the log and cache directories used by my Symfony2 application somewhere under /var/... in my file system. For this reason, I am looking for a way to configure Symfony and to override the default location for these two directories.
I have seen the kernel.cache_dir and kernel.log_dir and read the class Kernel.php. From what I have seen, I don't think that it is possible to change the dir locations by configuration and I would have to patch the Kernel.php class.
Is that true, or is there a way to achieve what I want without modifying the framework code?
Add the following methods to app/AppKernel.php (AppKernel extends Kernel) making them return your preferred paths:
public function getCacheDir()
{
return $this->rootDir . '/my_cache/' . $this->environment;
}
public function getLogDir()
{
return $this->rootDir . '/my_logs';
}
I was happy to find your post, but I was a little bit confused of the unhelping answers.
I got the same problem and found out that the logs are depending on the config parameter
kernel.logs_dir.
So I just added it to my config.yml parameters:
kernel.logs_dir: /var/log/symfonyLogs
I hope it will helpfull for you even, if its a late answer.
i think the easiest way is to link the folder to another place. We have made this on the prod server but when you develop local perhaps on windows its a bit complicated to set the symlinks.
ln -s /var/cache/ /var/www/project/app/cache
something like this.
I would like to offer an alternative and that is to set environment variables to change these directories. This way it's easier to set depending on the stage. (testing, production or development)
export SYMFONY__KERNEL__CACHE_DIR "/your/directory/cache"
export SYMFONY__KERNEL__LOGS_DIR "/your/directory/logs"
Environment variables can also be set in the virtual host with SetEnv.
When reading kernel parameters symfony will look for all the $_SERVER variables that start with SYMFONY__, strip the first part and convert all the double underscores into a .
Source code
See line 568 to 608
In symfony you can override the cache (and logs) directory by extending the method in AppKernel.
// app/appKernel.php
class AppKernel extends Kernel
{
// ...
public function getCacheDir()
{
return $this->rootDir.'/'.$this->environment.'/cache';
}
}
Check out http://symfony.com/doc/current/cookbook/configuration/override_dir_structure.html#override-cache-dir
I used the configuration solution from Dragnic but I put the paths in the parameters.yml file because this file is ignored by git. in other words, it's not synchronized from my PC to the git repository so there is no impact in the prod environment.
# app/config/parameters.yml
parameters:
database_driver: pdo_mysql
[...]
kernel.cache_dir: "T:/project/cache"
kernel.logs_dir: "T:/project/logs"
Configuration: Windows7, WAMP 2.4 and Symfony 2.3.20.
But you have to know that:
Overwriting the kernel.cache_dir parameter from your config file is a very bad idea, and not a supported way to change the cache folder in Symfony.
It breaks things because you would now have different cache folders for the kernel Kernel::getCacheDir() and for the parameter.
Source: https://github.com/symfony/AsseticBundle/issues/370
So you should use it only in dev environment and if you don't want to change the content of the app/AppKernel.php file, otherwise see the other answers.
No accepted answer, and a really old question, but I found it with google, so I post here a more recent way to change the cache directory, and the logs directory, (source here)
remember, short syntax for arrays require php 5.4
you can select the env to modify, and manage different cache and logs directories if you want
public function getCacheDir()
{
if (in_array($this->environment, ['prod', 'test'])) {
return '/tmp/cache/' . $this->environment;
}
return parent::getCacheDir();
}
public function getLogDir()
{
if (in_array($this->environment, ['prod', 'test'])) {
return '/var/log/symfony/logs';
}
return parent::getLogDir();
}

Resources