Friday, December 23, 2011
Microsoft Hackathon at PHPBenelux Conference 2012
Even if you have no knowledge of any of Microsoft's PHP solutions you're more then happy to join us and see how you can build applications so they can be distributed with the Web Platform Installer for running on Windows IIS servers.
Or maybe you just ended up with a new job in a company that only runs Windows Servers. We'll explain what you need to do so your applications run smoothly on these Microsoft IIS infrastructures.
And of course we'll have a look at Windows Azure, the cloud solution of Microsoft that allows you to develop PHP apps for the future. With grandmasters Maarten Balliauw and Craig Kitterman it's going to be an experience comparable to Alice in Wonderland. Are you ready to see how deep the Microsoft PHP tunnel runs?
Get your tickets now for the PHPBenelux Conference 2012 and join us to hack and learn more of all the Microsoft PHP tools and blow away your teammates and competitors with awesome applications.
Sunday, December 18, 2011
Configuring Zend Framework apps for Windows Azure
Building web applications is nothing new anymore, as we've been
doing it since the early days of the internet, but we've always
done this on a single system. Even when Zend Framework came round,
we kept doing the same thing and build apps for a single
environment.
But as I've discussed already in my previous article, developing for the cloud requires another
approach.
With Zend Framework
developing applications running on these separate compontents
becomes really easy. It's like having your cloud toolbox right in
your pocket.
Databases
With Zend Framework,
connecting to databases is really easy and swapping out a database
brand is just a matter of modifying your configuration
application/configs/application.ini.
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "10.20.30.40"
resources.db.params.username = "user1"
resources.db.params.password = "secret"
resources.db.params.dbname = "db1"
resources.db.isDefaultTableAdapter = true
Even if you need to connect to multiple databases, you can just
pile them up as configuration setting and be done with it.
resources.multidb.server1.adapter = "pdo_mysql"
resources.multidb.server1.host = "10.20.30.40"
resources.multidb.server1.username = "user1"
resources.multidb.server1.password = "secret1"
resources.multidb.server1.dbname = "db1"
resources.multidb.server1.default = true
resources.multidb.server2.adapter = "pdo_pgsql"
resources.multidb.server2.host = "10.20.30.41"
resources.multidb.server2.username = "user2"
resources.multidb.server2.password = "secret2"
resources.multidb.server2.dbname = "db2"
resources.multidb.server3.adapter = "pdo_sqlite"
resources.multidb.server3.dbname = APPLICATION_PATH "/files/db/project.db"
But this is just the basics. When dealing with the cloud you
often get a connection string for the host, so it's real easy to
hook up to an relational database in the cloud. Here's an example
to connecting to SQL Azure.
resources.db.adapter = "SQLSRV"
resources.db.params.host = "abcdefghijk.database.windows.net"
resources.db.params.port = 1234
resources.db.params.username = "user1"
resources.db.params.password = "secret"
resources.db.params.dbname = "db1"
resources.db.isDefaultTableAdapter = true
Bottom line, no worries connecting to a cloud database. Zend Framework has your
back!
Sessions
As you want to assist your visitors as much as possible, you
probably want to use sessions in your application. If you don't
configure anything, PHP stores session on your filesystem by
default and so does Zend
Framework.
For the cloud, you don't want to write to your local filesystem,
so you set up session storage. I chose to use the SQL Azure server
I already had setup using the following settings.
resources.session.use_only_cookies = true
resources.session.gc_maxlifetime = 864000
resources.session.remember_me_seconds = 864000
resources.session.saveHandler.class = "Zend_Session_SaveHandler_DbTable"
resources.session.saveHandler.options.name = "session"
resources.session.saveHandler.options.primary = "id"
resources.session.saveHandler.options.modifiedColumn = "modified"
resources.session.saveHandler.options.dataColumn = "data"
resources.session.saveHandler.options.lifetimeColumn = "lifetime"
No further changes need to be done as all fields are defined and
this Zend_Session_SaveHandler_DbTable takes care of all the rest.
Caching
Just like databases, providing caching for your application
requires just a few simple configuration settings, this example
sets up a memcache service.
resources.cachemanager.memcached.frontend.name = Core resources.cachemanager.memcached.frontend.options.automatic_serialization = On resources.cachemanager.memcached.backend.name = Libmemcached resources.cachemanager.memcached.backend.options.servers.one.host = localhost resources.cachemanager.memcached.backend.options.servers.one.port = 11211 resources.cachemanager.memcached.backend.options.servers.one.persistent = On
Caching on the cloud requires a little different approach as most
cloud services offer their own flavor of caching, making it
difficult to find a PHP driver that is capable to access this
cloud caching layer. But don't let this stop you in moving to the
cloud. Windows Azure provides a superb caching platform, and I'll
show you later in these series how to modify your application as
it requires a little tweek on the configuration of your Windows
Azure installation.
Storage
Uploading and distributing files can be considered as an importan
part of any application, and with Zend Framework you can
manage file uploads relatively simple using Zend_Form and
Zend_File.
An example would be to upload a small image.
public function uploadAction()
{
$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->setDestination(APPLICATION_PATH '/files/upload');
if (!$adapter->receive()) {
$messages = $adapter->getMessages();
echo implode("\n", $messages);
}
$this->view->filename = $adapter->getFileName('avatar', false);
}
Saving into a specific location is done with
$adapter->setDestination(), but this still requires the usage
of a local location! And we know that in the cloud saving locally
has no use! Luckily for you, Zend
Framework has a bunch of components that will allow you to
store files onto a Windows Azure Storage instance.
public function uploadAction()
{
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/api.ini', APPLICATION_ENV);
$azure = new My_Cloud_WindowsAzure_AzureStorage(
$config->azure->storage->account,
$config->azure->storage->primkey);
$container = $config->azure->storage->container;
$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->setDestination(APPLICATION_PATH '/files/upload');
if (!$adapter->receive()) {
$messages = $adapter->getMessages();
echo implode("\n", $messages);
}
$storageClient = new Zend_Service_WindowsAzure_Storage_Blob(
My_Cloud_WindowsAzure_AzureStorage::AZURE_STORAGE_HOST,
$azure->getAccountName(), $azure->getPrimaryKey());
$result = null;
if (!$storageClient->containerExists($container)) {
$result = $storageClient->createContainer($container);
$storageClient->setContainerAcl($container,
Zend_Service_WindowsAzure_Storage_Blob::ACL_PUBLIC_CONTAINER);
}
$fileName = $adapter->getFileName('resume', true);
$result = $storageClient->putBlob(
$container, basename($fileName), $fileName
);
$this->view->filename = basename($fileName);
$this->view->location = sprintf('http://%s.%s/%s/%s',
$azure->getAccountName(),
My_Cloud_WindowsAzure_AzureStorage::AZURE_STORAGE_HOST,
$config->azure->storage->container,
$result->Name);
}
As you can see, you only need to add a little more functionality
to the first example. But once you have everything in place,
nothing can stop you achieving your goals.
; These are local configuration settings
; Primarily used to access API's using credential tokens
[production]
...
azure.storage.account = "myblobstorageaccount1"
azure.storage.primkey = "fjldjljdlfjadladjsljdfljfdljd/akjddfjldfjjd22kajdajfei3234ajldjfjklajlajd=="
azure.storage.container = "myblobstoragecontainer"
...
[staging: production]
[testing: production]
[development: production]
In this example I used an api.ini which is very convenient to
store your most import settings like account names, api keys and
passwords to various api services. It's just a file containing
key-value pairs for easy configuration. In this case my INI
settings look like the following listing.
Next
Now that we've talked about the specific settings in your Zend Framework application
it's about time we set up our environment, download all the tools,
register for a Windows Azure account and get started with running
our Zend Framework app
in the cloud. So stay tuned for more.
Sunday, December 11, 2011
Windows Azure for PHP developers
Introduction
Since a couple of years the term "cloud" has moved from simple buzz-word to real business opportunities that make a difference within the market. The technology behind "cloud" is not new as we've grown accustomed to server clusters, distributed datacenters and separation of responsibilities, but the "cloud" offers everyone the opportunity to scale and not just the companies who have a huge cash reserve to purchase another rack with servers.
The strange thing is that cloud is being provided by companies you wouldn't expect to serve it. Best known are Amazon.com and Rackspace.com that offers a complete set of services you can use to run applications in the cloud, they provide the infrastructure and you have to set up and maintain your platform and infrastructure. Microsoft also jumped on this cloud hype offering a platform in the cloud. And this is exactly the reason it caught my attention and got me interested. I'm a developer and I don't want to fiddle with setting up and maintaining an operating system, basically since I don't have the time for it.
If you are eager to learn more
about cloud and what different kind of cloud solutions are out
there, I can recommend the book "PHP
Development In The Cloud" by Ivo Jansch and Vito Chin. In this book both
authors look beyond the buzz of the cloud and discuss what each
cloud service provider has to offer for PHP developers and how you
can easily consume their services using PHP code. A must read for everyone
that has an interest in developing web applications using PHP and cloud technology, as I
already posted a
book review for this book here on my
blog.
Windows Azure
I got in touch with Microsoft's
Windows Azure
back in 2008 through our local Microsoft Evengalist Katrien De Graeve, who
told me Windows
Azure is like running a Windows 2008 Server with IIS in the
cloud. Along with Maarten
Balliauw Microsoft
was working out more tools and features to support PHP (amongst other open source
technologies) on their new cloud platform.
I was completely sold when Josh Holmes came to
Brussels in 2009 and told us more about what Windows Azure has
to offer and how perfectly it is to build applications consuming
these cloud services, without having to deal with setting up and
maintaining the platform the run on. And so, I started playing
with it and discovering about what Windows Azure is
all about, what kind of services they offer and how I as a PHP developer could make good use
out of it. In 2010 PHPBenelux
and Microsoft joind
efforts to promote Windows
Azure by organizing a PHPonAzure contest to
develop an application that would run and make use of Windows Azure's
services.
Developing for cloud
When developing PHP
applications, you mostly develop for a single server setup or
maybe one server running your PHP
code and one server for the database. This is the easiest way to
set up an environment as you don't need to worry about anything
except to connect to the correct database.
When developing for the cloud, more things should be considered
before you get started. Some things that you take for granted
require some additional thoughts when designing the architecture
of your application. Fortunately for you, there are only a few
things to take into account before you can deploy your latest
killer app in the cloud provided by Windows Azure.
Operating System
On a bare metal server or even a virtual server, you know you
need to set up your own operating system (or have your hosting
service provider or sysadmin do this for you). This means you need
to take responsibilty in keeping your OS up-to-date with security
fixes and all kinds of software upgrades.
Windows Azure is
a platform on it's own, comparable to a fully set up Windows 2008
Server complete with IIS 7. Microsoft takes care of updating and
securing their instances you use without compromizing your
application.
Data Storage
When creating a normal website session data, logs, caches and file uploads are often stored on the local filesystem. With a cloud solution, you need to consider that multiple instances of your web application might be up and running and don't share each others filesytem. To facilitate filesystem storage, cloud services offer so called "file buckets" or storage endpoints. Windows Azure offers 5 types of storage:
- Blob storage: simple binary storage for video, images, audio
and other types of files
- Table storage: a structured storage for large amounts of data,
behaving like a NoSQL storage
- Queue: a messaging queue to transfer messages between
applications and services
- Windows Azure Drive: a mounted shared disk partition formatted as NTFS VHD
- SQL Azure: a true relational database based on Microsoft SQL Server technology
One storage location I need to mention a little separate is
Windows Azure Cache, a memory storage. It's a caching layer
comparable to Memcached or WinCache,
residing completely in memory.
File uploads are easy as you only need to worry where they end up and how you make sure files cannot overwrite existing files, unless specified to do so. Using one of the 5 storage solutions provided by Windows Azure is not difficult using PHP, it only requires correct settings for transferring the uploaded file to the chosen storage. PHP's move_uploaded_file() method can be used in most cases. In other cases, use the Windows Azure SDK for PHP as it offers a rich abstraction layer for the different storage options of Windows Azure.
Sessions in PHP can easily be
stored in a database, a storage endpoint or on a memory layer by
modifying the session_set_save_handler().
Windows Azure can
store session data on the blob storage, table storage, the Windows
Azure Drive, in SQL Azure or on it's Cache layer. I prefer to use
either the SQL Azure or the Cache, as they both perform really
fast, even for session data.
When using logs within your application, you also need to be
aware that your log files should reside somwehere central. Best is
to either log against Table storage or SQL Azure as it's a fast
way to keep track of changes and easily accessible. But, storing
logs on the blob storage is perfectly good as well, only know it
might grow your storage space too much.
Caching is something best handled by Windows Azure Cache, the
memory storage I mentioned earlier. It's fast, very easy to
maintain and super easy to scale in or out.
Data itself can be stored in SQL Azure or on Table Storage. SQL
Azure is a full featured relational database, comparable to
Microsoft SQL Server. It offers all the features you can expect
from a relational database, including stored procedures,
transactions and separation of responsibilities. Table Storage is
a simple key/value storage table, that behaves more like a NoSQL
storage and thus capable of storing documents and constructs in an
organized way.
Other considerations
Cloud applications should be designed to scale horizontally. This
means when your application requires more resources to cope with
the usage, more instances of the application will run together
behaving as a single application.
Cloud is also "pay as you go". If you use a little, you pay a
little. If you use a lot, you pay also a lot but less then when
you need to scale with traditional hardware. The following graphs
by Josh Holmes
explains it best.
Now it doesn't matter if you're a big company or just a start-up.
You can launch your product slowly and grow as your usage and
budget grows. A perfect match.
Next
Now that I've introduced you to what Windows Azure has
to offer PHP developers, or any
other technology developer for that matter, I show you how I have
a simple Zend Framwork application build on a traditional LAMP
stack deployed to Windows
Azure. So stay tuned for more.
Related links
Wednesday, November 16, 2011
PHPBenelux Conference 2012 Announcement
The PHPBenelux team only announced the names of tutorial speakers, but with names like Ivo Jansch, Torsten Rhinne, Fabien Potencier and Matthew Weier O'Phinney you know it's going to rock! And that's tutorials only.
It's all happening on January 27 and 28, so make sure you block those days in your calendar, because the center of Europe is going to buzz PHP like it never had before!!! Get your tickets now while their still hot (and cheaper than cheap)!!!
Sunday, September 18, 2011
Pfcongres 2011 wrap up
My keynote was well received by the audience, although some comments were made it needed a more elegant flow of community tips. It even started a discussion whether I should use visuals or not to emphasize my words. I will take these comments into consideration when I'm reviewing these slides.
Although I spend a major part of the conference in the so-called "Hallway tracks" I was able to jump into a couple of great talks that have intrigued me to look into their details. A couple of tracks that really stood out were "SPL Data Structures and their Complexity" by Jurrien Stutterheim, "15 Protips for MySQL users" by Joshua Thijssen and of course the closing keynote "PHP — Status Check" by no one less than co-founder of Zend Mr. Zeev Suraski.
The good
The team of PHPFreakz managed to do the impossible: creating a good balance for Dutch and non-Dutch speakers, offering a good lunch (especially for Dutch standards) with a wide variety of sandwiches and a constant flow of good coffee.
The bad
I have no complaints regarding the organization, the audience or the venue. I just felt bad I had to wake up at 6am just to be there on time.
The ugly
The venue's wifi was a real burden, and I know from experiences that most conference wifi settings are flakey as a rule, but this wifi experience was bad, even considering the flakey ones.
Conclusion
This conference showed the world again that a small community group can amaze everyone by bringing quality speakers into their community offering them high level content at a really reasonable prize. I'd like to express my gratitude to the organization to be part of this event.
Bonus challenge
I keep thinking about what Joshua Thijssen said during his talk about getting certified as a MySQL engineer. As it was on my to-do list for a couple of years, I think I needed the push Joshua had given me to follow up on my commitments and schedule an exam.
Thursday, September 15, 2011
Quality Assurance on PHP projects - PHPUnit lessons learned
And I believe Lars Tesmer has done the latter, where he blogs on testing PHPUnit itself and the lessons learned in that process.
Thank you Lars for sharing your lessons.
Saturday, September 03, 2011
Quality Assurance on PHP projects - PHPUnit part 4
In this fourth part we should focus on playing the game. Let's go over the steps again:
- each player chooses a symbol
- for each turn (max 9 turns)
- a player places his symbol on the grid
- if 3 symbols appear in a single row (horizontal, vertical or diagonal)
- player is a winner
public function testGameCanBePlayed()
{
$playerX = $this->_ttt->getPlayers()->seek(0)->current();
$playerO = $this->_ttt->getPlayers()->seek(1)->current();
$this->assertFalse($this->_ttt->play(0, 0, $playerX));
$this->assertFalse($this->_ttt->play(0, 1, $playerX));
$this->assertTrue($this->_ttt->play(0, 2, $playerX));
$this->_ttt->setGrid(new Grid());
$this->assertFalse($this->_ttt->play(0, 0, $playerX));
$this->assertFalse($this->_ttt->play(1, 0, $playerX));
$this->assertTrue($this->_ttt->play(2, 0, $playerX));
$this->_ttt->setGrid(new Grid());
$this->assertFalse($this->_ttt->play(0, 0, $playerX));
$this->assertFalse($this->_ttt->play(1, 1, $playerX));
$this->assertTrue($this->_ttt->play(2, 2, $playerX));
$this->_ttt->setGrid(new Grid());
$this->assertFalse($this->_ttt->play(0, 2, $playerX));
$this->assertFalse($this->_ttt->play(1, 1, $playerX));
$this->assertTrue($this->_ttt->play(2, 0, $playerX));
}
As you see, we only tested the functionality to see if we can have a
winner when 3 identical symbols appear in a single row horizontal,
vertical or diagonal. We can make a couple of comments on this test:- this test is named wrong as it doesn't test the gameplay functionality
- this test should be branched out into different tests for each row
- this test doesn't test turn-by-turn game play
public function rowColProvider()
{
return array (
array (array (array (0,0), array (0,1), array (0,2))),
array (array (array (0,0), array (1,0), array (2,0))),
array (array (array (0,0), array (1,1), array (2,2))),
array (array (array (0,2), array (1,1), array (2,0))),
);
}
/**
* @dataProvider rowColProvider
*/
public function testGameplayCanDetectWinner($rowCols)
{
$player = $this->_ttt->getPlayers()->seek(0)->current();
$this->assertFalse($this->_ttt->play($rowCols[0][0], $rowCols[0][1], $player));
$this->assertFalse($this->_ttt->play($rowCols[1][0], $rowCols[1][1], $player));
$this->assertTrue($this->_ttt->play($rowCols[2][0], $rowCols[2][1], $player));
}
Now we know that with each turn, the return value will indicate if
we have a winner (TRUE) or not (FALSE). But we still need to see if
we can have the same result when playing with two players. As you
see with the last line, the third entry gives us a positive result.But we still don't have a gameplay going! We need to have two players enter the arena and each player playing turn by turn. So how do we approach this? Well, the easiest way for now is to create a test that does just that. The benefit here is we can decide which player is going to win the game.
public function testGameCanBePlayed()
{
$playerX = $this->_ttt->getPlayers()->seek(0)->current();
$playerO = $this->_ttt->getPlayers()->seek(1)->current();
$this->assertFalse($this->_ttt->play(0, 0, $playerX));
$this->assertFalse($this->_ttt->play(0, 1, $playerO));
$this->assertFalse($this->_ttt->play(1, 1, $playerX));
$this->assertFalse($this->_ttt->play(2, 2, $playerO));
$this->assertFalse($this->_ttt->play(1, 0, $playerX));
$this->assertFalse($this->_ttt->play(2, 0, $playerO));
$this->assertTrue($this->_ttt->play(1, 2, $playerX));
}
Visual this result looks like the following grid: X | O |
X | X | X
O | | O
But most importantly our tests are still green, giving us that wonderful feeling of achievement.Let's finish up our tests so we can see no one can play any further after we've got a winner. PHPUnit provides a nice anotation we can use for this: depends. So now we can test that the game is stopped, depending on our test "testGameCanBePlayed".
For this to work, we need to return our game (in this case $this->_ttt). Just add the following line at the bottom of the test class "testGameCanBePlayed".
public function testGameCanBePlayed()
{
$playerX = $this->_ttt->getPlayers()->seek(0)->current();
$playerO = $this->_ttt->getPlayers()->seek(1)->current();
$this->assertFalse($this->_ttt->play(0, 0, $playerX));
$this->assertFalse($this->_ttt->play(0, 1, $playerO));
$this->assertFalse($this->_ttt->play(1, 1, $playerX));
$this->assertFalse($this->_ttt->play(2, 2, $playerO));
$this->assertFalse($this->_ttt->play(1, 0, $playerX));
$this->assertFalse($this->_ttt->play(2, 0, $playerO));
$this->assertTrue($this->_ttt->play(1, 2, $playerX));
return $this->_ttt;
}
And the next step is simple: /**
* @depends testGameCanBePlayed
* @expectedException RuntimeException
*/
public function testGameStopsAfterWinning($game)
{
$playerO = $game->getPlayers()->seek(1)->current();
$game->play(2,1, $playerO);
}
In order for this test to succeed we need to add a couple of things to our game:- a status property that will tell us if there's a winner
- a setter method to flip the flag once a winner is detected
- a checking method to verify a winner is not yet detected
- modify our play method so it throws a RuntimeException when we try to play when a winner exists.
<?php
...
class Tictactoe
{
...
/**
* Status indicator to say there's a winner or not
*
* @var bool
*/
protected $_winner = false;
...
/**
* Sets a flag to indicate this game has a winner
*
* @param bool $flag
* @return Tictactoe
*/
public function setWinner($flag = true)
{
$this->_winner = $flag;
return $this;
}
/**
* Checks if the game has a winner
*
* @return bool
*/
public function hasWinner()
{
return $this->_winner;
}
/**
* Plays the game and returns TRUE if a player has become a winner, FALSE
* if the player is not (yet) a winner.
*
* @param int $row
* @param int $column
* @param Player $player
* @return bool
* @throws RuntimeException
*/
public function play($row, $column, Player $player)
{
if ($this->hasWinner()) {
throw new RuntimeException('Game already has a winner');
}
$this->getGrid()->setSymbol($row, $column, $player->getSymbol());
return $this->isWinner($player);
}
/**
* Returns TRUE if a player has become a winner, FALSE if not.
*
* @param Player $player
* @return bool
*/
public function isWinner(Player $player)
{
if ($this->getGrid()->inRow($player->getSymbol())) {
$this->setWinner();
return true;
}
if ($this->getGrid()->inColumn($player->getSymbol())) {
$this->setWinner();
return true;
}
if ($this->getGrid()->inDiagonal($player->getSymbol())) {
$this->setWinner();
return true;
}
return false;
}
}
That's it. We covered the main purpose of the game and we did it semi
test driven. We can start playing a cute little game of Tictactoe and
covered a couple important features of PHPUnit. I also showed that it's
not a bad thing if you modify parts of your tests to achieve new or
other specifications.The full game source code can be found on my github account (https://github.com/DragonBe/tictactoe), and as you go over the log, you can follow along with these series as well. When playing with the source code, you might think about testing edge cases: playing 9 rounds and no winner, try to place a symbol off the grid, place twice a symbol on the grid of a single player (cheating), and so on.
Another thing could be that you turn this little game into an online game. What are the things you need to concider in that situation. Are our tests still valid or do we need to modify our architecture and our tests to support that kind of game playing?
Since it's on GitHub, you can fork it and send me a pull request once you have an edge case figured out.












