suggestion for finding domain

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
audrey
Forum Newbie
Posts: 12
Joined: Sat Mar 03, 2007 2:19 pm

suggestion for finding domain

Post by audrey »

The __construct method in Swift.php currently has:

Code: Select all

public function __construct(Swift_IConnection &$object, $domain=false)
	{
		if (!$domain) $domain = !empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'SwiftUser';
If run from CLI, $_SERVER['SERVER_NAME'] will not exist. I suggest you change to:

Code: Select all

public function __construct(Swift_IConnection &$object, $domain=NULL)
{
	$domain = !is_null($domain)
		? $domain
		: !empty($_SERVER['SERVER_NAME'])
			? $_SERVER['SERVER_NAME']
			: !empty($_SERVER['HOSTNAME'])
				? $_SERVER['HOSTNAME']
				: 'SwiftUser';

Personally, i'm just passing it as the second param to new Swift() but i thought somebody might want to see this.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

The logic will probably fail (see http://us3.php.net/manual/sv/language.o ... .php#41251), and the nested ternary conditions are not clear to someone who wants to go in and read/edit the code. In this case, if statements, or even a seperate function or class would be better.
Last edited by Benjamin on Sun Apr 01, 2007 12:14 am, edited 1 time in total.
audrey
Forum Newbie
Posts: 12
Joined: Sat Mar 03, 2007 2:19 pm

Post by audrey »

A ternary always sounded to me like something that belongs in a nest.

Anyhoo, i think it's eminently readable. Indentation is crucial, of course.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Please see my revised post above, which cites an example.
audrey
Forum Newbie
Posts: 12
Joined: Sat Mar 03, 2007 2:19 pm

Post by audrey »

Right you are!

And with the added braces, i'd have to agree that it would become unreadable. Otherwise, i have no problem following it.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It's probably a fair trade-off that it's setabble in the constructor anyway. I don't think anything in $_SERVER exists if running on CLI does it?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_SERVER is mostly filled with environment variables when run from the command line.
audrey
Forum Newbie
Posts: 12
Joined: Sat Mar 03, 2007 2:19 pm

Post by audrey »

This will quickly show you:
$ php -r 'phpinfo();' > phpinfo.txt
Post Reply