Jump to content

Set ready-dev.php in addition to config-dev.php for local environment


Ivan Gretsky
 Share

Recommended Posts

Most of us know and use site/config-dev.php file. If present, it is used instead of site/config.php, so it is easy to set database connection and debug mode for local development, not touching the production config. It is also very useful when working with git. You can simply ignore it in the .gitignore file, so local settings won’t end up in the repo.

But sometimes you need to add code to site/ready.php or site/init.php just for the dev environment. For example, to add ryan’s super cool on demand images mirrorer. I can’t live without it when working with big sites, which have more assets then I want to download to my desktop.

It would be great if there was something like site/ready-dev.php for this. Not out-of-the-box, but it’s pretty easy to achieve. Unlike site/config-dev.php, site/ready.php is not hardcoded. It’s name is set with a special config setting:

// wire/config.php

$config->statusFiles = array(
	'boot' => '',
	'initBefore' => '',
	'init' => 'init.php',
	'readyBefore' => '',
	'ready' => 'ready.php',
	'readySite' => '',
	'readyAdmin' => '',
	'render' => '',
	'download' => '',
	'finished' => 'finished.php',
	'failed' => '',
);

As you can see, we can not only define, which files are loaded on init, ready and finished runtime states, but probably even add more if we need to.
So we override this setting in site/config-dev.php like this:

// site/config-dev.php

// Change ready.php to ready-dev.php
$temp = $config->statusFiles;
$temp['ready'] = 'ready-dev.php';
$config->statusFiles = $temp;

For some reason we can’t just do

$config->statusFiles['ready'] = 'ready-dev.php';

and have to override the whole array. Maybe you PHP gurus can explain this in the comments.

Now we can create the site/ready-dev.php file and place all the dev-only code there. Important thing is to include the main site/ready.php.

// site/ready-dev.php

include 'ready.php';

// DEV HOOK TO MIRROR ASSETS ON DEMAND
$wire->addHookAfter('Pagefile::url, Pagefile::filename', function($event) {

	$config = $event->wire('config');
	$file = $event->return;
  
	if($event->method == 'url') {
		// convert url to disk path
		$file = $config->paths->root . substr($file, strlen($config->urls->root));
	}
  
	if(!file_exists($file)) {
		// download file from source if it doesn't exist here
		$src = 'https://mysite.com/site/assets/files/';
		$url = str_replace($config->paths->files, $src, $file);
		$http = new WireHttp();
		try {
			$http->download($url, $file);
		} catch (\Exception $e) {
			bd($file, "Missing file");
		}
	}
});

Do not forget to replace "mysite.com" if you’re copypasting this))

Now, add the newly created file to the `.gitignore` and we’re done.
 

# .gitignore

# Ignore dev files
site/config-dev.php
site/ready-dev.php

Thanks for reading!
 

  • Like 12
Link to comment
Share on other sites

Hi Ivan,
very nice tip. I haven't known about this. May come in handy. Thank you for sharing. ?

 

Regarding your question on overwriting single items of config arrays, you can do it this way:

$config->statusFiles('ready', 'ready-dev.php');

 

Also, if I want to overwrite more then one item, I prefer this way:

$config->statusFiles = array_merge($config->statusFiles, [
	'init' => 'init-dev.php',
	//'readyBefore' => '',
	'ready' => 'ready-dev.php',
]);

 

  • Like 5
  • Thanks 1
Link to comment
Share on other sites

9 hours ago, Ivan Gretsky said:

For some reason we can’t just do


$config->statusFiles['ready'] = 'ready-dev.php';

and have to override the whole array. Maybe you PHP gurus can explain this in the comments.

I am not a guru, but:

When you access $object->property, a magic __get() method is called that returns a copy of the value of this property. If the value is an array and you try to add an item, only the copy is affected, not the original property value. For this reason, an error message is displayed that your attempt to change the value has no effect. The magic __set() method must be used to change the (whole) value of the property.

Both ways (yours and @horst 's) are fine to set the value.

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...