Live geek or die tryin'

PHP: Generate Documentation With DocBlox

I recently have had to generate a PHP project’s documentation and I’ve chosen the popular PhpDocumentor for this purpose.
This tool is unfortunately outdated, hasn’t been updated since 2007, and thus throws a lot of PHP warnings and cannot do what I was asking it.

My friend Ludo has suggested using DocBlox which is, compared to PhpDocumentor, more modern - supports PHP 5.3, faster, and consumes less memory.

After trying it, I can say that it’s indeed a great tool, inasmuch as I could generate documentation for my project 5 seconds after installing it.

How to use it:

Installation

Make PEAR know about DocBlox’s channel

pear channel-discover pear.docblox-project.org

Then install DocBlox(-beta)

pear install docblox/DocBlox-beta

The command line docblox should be available for use now.

Use

docblox -d /path/folder1,/path/folder1/folder2 -t /path/to/target/directory

Easy, ha? :)

Make sure you separate the source directories by commas only, no spaces.
You can also use some of the basic PhpDocumentor commands.

Symfony 2: Using Global Variables

In order to use variables in a Symfony2 controller, you’ll have to set them up in parameters.ini, or in any other ini file on condition that you’ll import it into config.yml.
In case you want to use them in a Twig template, you must set them up in config.yml.

Once done, you’ll be able to whether call them using getParameter() from a controller or with the syntax “ from a Twig template.

Set up your variable

In an ini file:

[parameters]
    myVariable = 2

In config.yml:

twig:
  globals:
    myTwigVariable: %myVariable%

Import your ini file into config.yml

You can skip this step if you already set up the variables in parameters.ini.

imports:
    - { resource: myFile.ini }

Use the global variable

In a controller:

$myVariable = $this->container->getParameter('myVariable');

In a Twig template

<p>
    My variable: 
</p>

Enjoy. ;)

Translated from my french post Symfony2: Utiliser des variables globales

Symfony2: Using Multiple Databases

In case your website, built with Symfony2, is using multiple databases, you must setup as much connections and Entity Managers as databases.

An Entity Manager is simply an interface that speaks to the database.

In order to illustrate my example, I will be using two databases: site and forum.

Set up the connections in config.yml

doctrine:
  dbal:
    default_connection: site # specify the connexion used by default
    connections:
      site:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   site
        user:     site_usr
        password: site_pwd
        charset:  UTF8
      forum:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   forum
        user:     forum_usr
        password: forum_pwd
        charset:  UTF8

Set up EM, in the same file

To begin with, disable auto-mapping, so you will be able to choose which EM is going to manage which bundle’s entities.

doctrine:
  orm:
    auto_mapping: false

Note: If there’s an error bounded with this line, just delete it.

Then, right below, set up EM while specifying mappings to each bundle.

doctrine:
  orm:
    auto_mapping: false
    default_entity_manager: web # specify the EM used by default (when using console commands f.e)

    entity_managers:
      web:
        connection: web
        mappings:
          DinduksFooBundle : ~
          DinduksBarBundle : ~
          FOSUserBundle: ~
      forum:
        connection: forum
        mappings:
          DinduksForumBundle : ~

Simply put, mappings connect and constitute the bridge between an EM and the entities of a specific bundle. In other terms, mappings call up an EM X when wanting to manipulate an entity Y.

Warning: Always make sure you map your fresh-created bundles to an EM, otherwise a “non-existent bundle” error will occur when using the doctrine:generate:entities command.

Use

In the same way you call your default EM, call the EM you need by passing its name to the getEntityManager() function.

$forumEm = $this->get('doctrine')->getEntityManager('forum');

I hope you’ll find this tutorial useful. :)

Translated from my french post Symfony2: Utiliser plusieurs bases de données

Git: Rename or Change a Repository URL

If you’re new to Git, this tip will allow you to save some of your precious time.

Open .git/config in your project’s local directory, and, in the category [remote “origin”], modify the url parameter, using the new location or the new repository’s name.

OR

Use this command:

git remote set-url origin <new url>

Note: If you’re using GitHub, you must also rename the project on the platform: Go to the repository’s homepage, click the Admin button and change the name.

Translated from my french post Git: Renommer ou changer l’adresse d’un dépôt

MaNGOS: Executing GM Commands via SOAP

Hi,

I have recently built a site for a World of Warcraft server using Symfony2 framework. Among the features I had to implement, there was a shop where donators can buy rewards, such as levels, gold, items and teleportations.

During my first reflexions, as a Web developer who has no knownledge about MaNGOS server, I thought of rewarding players by adding or updating data in the database. I started experimenting that way, but I quickly realized that it was leading me nowhere.

There are some reasons why:

  • Adding levels using the database is useless if the player is online. Because it doesn’t apply instantly and once the player logs off, the server will override the level in the database with the player’s level on the logout.
  • The samething applies for teleportation.
  • Sending an item: I won’t give much details because it’s too complicated, but basically, sending an item using the database is a pain in the ass since it requires creating many rows here and there. Some of these rows are in tables with non-documented columns, which make them (almost) impossible to create. #mindfuck

As you may already know if you’re familiar with MaNGOS, all the actions mentionned above are doable using GM commands. Fortunately, MaNGOS server already has a SOAP Web Service allowing to execute GM commands directly on the server. Using it will not only allow you to do things quickly and easily, but it is also safe since you know that MaNGOS native commands will do the what you ask them the right way.

This is how to use SOAP with PHP:

Enable SOAP

First of all, make sure to have the SOAP extension enabled: extension=php_soap.dll on Windows.

Store your server’s info in variables:

$soapUsername = 'mangos_usr';
$soapPassword = 'mangos_pwd';
$soapHost = '127.0.0.1';
$soapPort = '7878';

Then create an instance of SoapClient and pass to it the required parameters

$client = new SoapClient(NULL, array(
    'location' => "http://$soapHost:$soapPort/",
    'uri'      => 'urn:MaNGOS',
    'style'    => SOAP_RPC,
    'login'    => $soapUsername,
    'password' => $soapPassword,
));

The login and the passwords are the ones that you use to log into your GM account.

Create a command

$command = "character level Dinduks 70";

Note that your command musn’t start with a dot.

Execute that command

$result = $client->executeCommand(new SoapParam($command, 'command'));

$result will contain the same text that you’d see in yellow color if you did execute the command in game.

It’s as simple as that!

Bonus

I wrote a short script that allows to execute GM commands from a command prompt. It’s pretty funny and can be useful if you want to do something without starting an instance of the game. You can grab the script on Github: GM-Commands-Script

Have fun!