Skip to content

Managing the Verbosity of symfony’s Command Object With a Trait

Dear Reader,

Update:
I was wrong and Kris Wallsmith called me on it. When installing the Command component, composer did NOT pull in any other symfony packages, or any other packages at all. I started this project with Silex and I believe that had pulled in a lot of stuff. To Composer’s credit. when I went in and removed Silex and put in the symfony component, it removed all the Silex dependancies and then dropped in the symfony component. Thank you Kris!

Long Rambling Intro of No Concequence

Recently I came up with a project I needed to build. Honestly, it’s one of those “not useful to anyone but me” kind of projects. I decided though that while I was at it, I wanted to learn something new. Since I’ve been a die-hard Zend guy since Zend Framework was 0.2, and more recently a Silex believer, I thought I would drop down a level form Silex and dig into symfony itself.

This project is strictly a command line project. No API, no web interface, just a command and a few switches. Digging around I found Symfony\Component\Console\Command\Command so I decided to give it a try. Honestly, I really like what I saw. It was even better that I was able to use composer to install it.

The project itself was a simple pump to move data from EventBright into MailChimp. Those of you who don’t know me, I do a lot of virtual events like DayCamp4Developers and NomadPHP, so this is a real time saver for me.

The one rough edge that I had was dealing with the verbosity setting of Symfony\Component\Console\Output\OutputInterface. That is what I am going to cover in this blog post.

Verbosity

When reading the symfony Command manual page it talks about verbosity. When you actually run the ./console.sh you can see that the -v tag is supported.

cal$ ./console.sh 
Nomad PHP Maintenance Script version 0.1.0

Usage:
  [options] command [arguments]

Options:
  --help           -h Display this help message.
  --quiet          -q Do not output any message.
  --verbose        -v|vv|vvv Increase the verbosity of messages: 
                             1 for normal output, 
                             2 for more verbose output and 
                             3 for debug
  --version        -V Display this application version.
  --ansi              Force ANSI output.
  --no-ansi           Disable ANSI output.
  --no-interaction -n Do not ask any interactive question.

Available commands:
  autodelete   Handle the autodelete process
  ebdatapump   Populates MailChimp lists from EventBrite events
  help         Displays help for a command
  list         Lists commands
  listLists    Prints a formatted list of the lists for a given mailChimp 
               account.

However, you have to piece things together a bit to finally get to the the point that:

-q OutputInterface::VERBOSITY_QUIETL
No options OutputInterface::VERBOSITY_NORMAL
-v OutputInterface::VERBOSITY_VERBOSE
-vv OutputInterface::VERBOSITY_VERY_VERBOSE
-vvv OutputInterface::VERBOSITY_DEBUG

It’s not confusing or anything but it it’s not really clear from the documentation.

Once I worked out the verbosity level, everything was awesome…at least until I got into my code. Then I realized that I was writing a LOT of lines that looked like this:

if ($this->output->getVerbosity()==OutputInterface::VERBOSITY_VERBOSE) {
    $this->output->writeln("My Message Goes Here");
}

That’s not really that bad until you start writing a lot fo them. In some cases I do like very verbose output, especially when debugging.

If you are curious, it is $this->output-> because I always take the $output object and store it in $this->output so I can get to it from any method in the object.

My First Solution

It didn’t take long to figure out that my Command class needed it’s own writeln() method.

protected function writeln($message,$level=1)
{
	if (is_null($this->output)) {
		return;
	}

	if ($this->output->getVerbosity()>=$level) {
		$this->output->writeln((isset($this->testing) && 
                                        $this->testing?'TESTING - ':'') . 
                                        $message);
	}

	return;
} // protected function writeln($message,$level=1)

Now, we’ve got things a little better. First, if it can’t find the $this->output, it fails silently. Second, This is where the IF statement lives so I don’t have to wrap every writeln() in a IF. It makes for much prettier code. the method takes an optional verbosity parameter so that I can specify what level the message should be displayed at. IF the message is VERBOSITY_VERY_VERBOSE and the verbosity level is VERBOSITY_NORMAL, the method just discards it and keeps going.

Finally. All of my commands support a -t testing mode. This let’s me do a dry run and make sure everything is going to work. I want it to be very clear when I am in testing mode so I prepend “TESTING “ to each line.

At this point I know a lot of you are asking “Why not just use logging?” The easy answer to that one is “I didn’t want to.” I don’t need full-on logging, I just need to be able to print stuff out to the terminal.

The Final Solution

This all looked pretty good to me. I was feeling rather pleased with myself…until I wrote my NEXT command for this toolset. (As you can see from the output above, there are a total of 3 now, it kinda got out of hand.) I started the second Command and realized I needed the writeln() there as well. So I did what most everyone does, I copied and pasted it. (Hush, you’ve done it too.)

I quickly realized the error of my ways. In the past, I would have subclassed Symfony\Component\Console\Command\Command and added it to the child class, and used that. That didn’t feel right either. So I wrote my very first Trait in PHP, this felt right. I love the simplicity of sharing it amongst all the other Commands. I love that I don’t have to manage sub-classes of the parent class, and yet I don’t have to hack the core either. (Again, hush, you’ve done it at least as much as I have.)

So, for better or worse, I now have a trait.

namespace NomadPHP\Traits;

trait WritelineTrait {
	protected $output;

	protected function writeln($message,$level=1)
	{
		if (is_null($this->output)) {
			return;
		}

		if ($this->output->getVerbosity()>=$level) {
			$this->output->writeln((isset($this->testing) && 
                                               $this->testing?'TESTING - ':'') . 
                                               $message);
		}

		return;
	} // protected function writeln($message,$level=1)
} // trait writeline

To use I just add this line inside of my Class definition.

use FollowMe\Traits\WritelineTrait;

Inside my class gets me everything I need and I never have to worry about it again…at least for this project.

Conclusion

Until next time,
I <3 |<
=C=

Sexism and PHP

Dear Reader,

UPDATE – 03/20/2013
For what it is worth, Anna and I have exchanged emails and I did apologize for some of my actions because I consider them un-professional. She has graciously accepted my apology. While we still stand on opposite sides of the aisle on this matter, at least we are now smiling and waving at each other. :)

Recently at SunshinePHP, my friend Ligaya Turmell and I had a discussion about sexism in the PHP community. My question to her was something like “I’ve been running PHP conferences for 7 years now and have never heard of an incident at any conference of which I was a part.” She had a great response. I am paraphrasing here, Lig, feel free to correct me.

“The PHP community has not had the problem with sexism that other communities seem to have but that is because from the early days, we have had strong women role models. Women like Lara Thomson, Sara Golemon, Liz Smith, and the like have played such a prominent role in the community that no, we’ve not had that big of a problem.”

I do not mean to imply that we can’t do better but if you look at all the recent blog posts about sexism at conferences, you will notice that none of them are PHP conferences…until now.

Today @webandphp rolled out a new T-Shirt at PHPUK. I’ll leave it to you to go find the picture, I’m not going to give them the Google juice of a link. I’ll just say THIS IS WHY WE CAN’T HAVE NICE THINGS!

No it’s not horrifically offensive. I’ve seen worse at conferences like OSCON…but that was from members of other communities that think it’s cute.

Here’s the bottom line. @webandphp thinks this is ok; that’s fine, it’s their opinion. My opinion is that it’s not.

  • As a man, I’m embarrassed
  • As a board member of PHPWomen.org, I’m offended
  • As a member of the PHP Community, I’m sad

So I’m going to take a stand in my little corner of the world and I don’t care if anybody else does.

To all PHP Conference organizers, I will not participate in any conference that @webphp or their parent company S&S is involved in. I will not attend, I will not speak. If I agree to speak or purchase a ticket and then they get involved in any way, I will refuse to participate.

It’s a small step, I know. Largely symbolic because to date, I’ve attended 2 conferences that S&S has been involved in and none that @webandPHP has been a part of. (To the best of my knowledge) But it’s my small step.

And to whomever is operating the @webandphp twitter account”

“@CalEvans Cal you act like I’m not a part of the community, I would gladly speak to you on the phone, if you would talk to me :) ?”

I don’t get to define what the PHP Community is, but I’m not part of ANY community that identifies itself with stuff like this.

UPDATE: Thanks to my friend @JCarouth, I am informed that @webandPHP was a media sponsor of @SunshinPHP, a conference that I attended and at which I spoke. As much as I LOVE @sunshinePHP and it’s founder @AdamCulp, I will stand behind my pledge. Again, it’s a small step, but it’s my small step.

UPDATE: It should be noted for those that do not know, all of this happend just recently. Sunshine PHP took place a few weeks ago and before this incident. I do not mean to disparage SunshinePHP or my friend Adam Culp in any way. :)

Until next time,
I <3 |< =C= For more on this topic, check out the Sexism in Tech I recorded on the topic with 4 respected women in the community.

PHP Master Series Vol 1

Dear Reader,

There is a lot of good information shared at conferences and sadly not even 1% of developers who identify themselves as PHP developers, ever get to attend one. This makes me sad. So I’m trying something new. This may work, it may fail miserably., let’s see. :)

I went through the PHP conferences from the second-half of this year and picked out 6 that really stood out. This was a very difficult task; the PHP community and conferences are blessed with a bounty of great talks and speakers. The criteria I used was topics, joind.in score, and speaker. I tried to find talks that were on topics that are of interest to developers, that others liked, and that were given by speakers I know, and know do a good job.

  • Designing Beautiful Software – Matthew Weier O’Phinney
  • Cryptography For The Average Developer – Anthony Ferrara
  • Javascript Best Practices & BackboneJS for the PHP Developer – Ryan Weaver
  • Your code sucks, let’s fix it. – Rafael Dohms
  • Building a Firehose – Ian Barber 
  • From POX to HATEOAS, A Real Company’s Journey Building a RESTful API – Luke Stokes

The end result is a one-day, virtual conference I’m calling “The PHP Master Series, Vol 1”. If you are a PHP programmer and want to learn more, I encourage you to take a look at it. We would love to have you join us. :)

Until next time,
I <3 |<
=C=

Twilio Quick-hit

Dear Reader,

UPDATE: I got a tweet from @twilio, they have updated the documentation. I’m leaving this blog post here to remind me but the docs now tell you the default encoding.

Ok, so I finally found a project that would let me play with twilio. I’ll post more about the project later. However, there is one thing that I wanted to blog – more for me than you.

When you record something in twilio, the default encoding is WAV.

Now if you read the TwiML docs for <Record> this fact is not mentioned at all. (They spend a lot of space up-selling their transcription service but no space at all talking about the default encoding) However, in twilio REST documentation they make a point to tell you this. If you read it, you find out that you can actually request the file as an mp3 and twilio will encode it for you if you like. That is way cool.

Overall, I really like twilio. Outside of the encoding issue, it took me about 3 hours to whip up my little application. I’ll blog about the application next week and maybe do a blog talking about both twilio and tropo now that I’ve used them both.

Until next time,
I <3 |<
=C=

Don’t Hire PHP Community Members!

Dear Reader,

It is no secret that I spend a lot of time promoting the PHP Community. It is a vibrant, helpful and friendly community and I’ve said before that I believe it to be one of the most important assets of the PHP language.

I’m also a realist though; I’ve built teams and I’ve hired developers. I know what it takes to put together good teams, I’ve even written down my thoughts on hiring and managing developers elsewhere. I have experience in this area and I have strong opinions. I am going to share one of those opinions with you right now.

Don’t hire developers who are active members of the PHP Community.

PHP community members solve problems

Active members of the PHP community solve problems. I mean they get their hands dirty in code – theirs or someone else’s – and solve problems. They are used to collaborating with other PHP community members to solve real world problem for themselves, their employers, or other community members who need their help. They spend time helping friends on IRC solve problems; problems that they may eventually face in their day job. They don’t do it because they were paid to; they solved the problem because they could.

If they can’t solve a problem, they usually know who can

Active members of the PHP community not only share what they know, they build up a list of others who are willing to share with them. Most of the time it doesn’t matter if the problem is for a project they contribute to or part of their day job, if there is a problem to be solved, members of the PHP community know who to call to get help. Since they help others, they have a cache of good will that they can use to get problems solved at work.

They love to show off

PHP community members love to show off and they do so by helping others. You can often find them showing other teammates something new they learned while working on a project they contribute to in their off-hours. They organize User Groups just so they can show off to others. It’s why they love to speak at conferences, so they can show off stuff they have learned.

They make their employers look good at conferences.

Active PHP community members love to speak at conferences and they will want you to help pay for it. Their speaking is nothing more than showing off. It doesn’t matter that their presenting makes your company a thought leader and makes it easier for you to attract other developers. All you get as a return on your investment is a smarter, better connected, inspired and rejuvenated developer. Trust me, I understand, you’ve got deadlines to meet and can’t have a developer out for a week showing off and finding solutions to the difficult problems they are working on for you. It doesn’t matter that they come back energized and inspired. It probably doesn’t even matter that they burn off all this new-found energy solving problems, and building solutions faster and better. All that matters is they weren’t in their cube for a week, right?

They work for free

No, not for you, don’t be stupid; but most active members of the PHP community contribute to one or more open source projects on their own time. This means that even when they aren’t paid to do so, they are coding; learning, honing their skills that they then come back and use for you.

Conclusion

In short, no, don’t hire active PHP community members. Hire the developers that are happy to punch in at 9 and out at 5, go home and tinker in their workshop. Honestly, there are enough teams out their vying for active members of the PHP community because they recognize them as the cream of the crop as far as developers go. They want them on their team and are counting on you to to pass over all active PHP community members because you think they they are too high maintenance. You keep thinking that, just hope your competition does too.

Until next time,
I <3 |<
=C=

Update:

I’ve had several people tweet to me asking if I was being serious or sarcastic. (“You serious, Clarke?“) This post is of course, tongue-in-cheek. Active members of the PHP community are some of the best developers you can hire and are a sought after commodity. If you are lucky enough to hire one, take care of them and hold onto them.