#!/usr/bin/env php
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.doctrine-project.org>.
 */

use Amf\Application;
use Amf\File;
use Amf\Cli;

use Zend\Code\Reflection\ClassReflection;

chdir(dirname(realpath(dirname($_SERVER['argv'][0]))));

require_once "vendor/autoload.php";

Application::bootstrap();
ini_set('html_errors', 0);
$c = Application::get('config');

$app = new Cli\Application($c->name, $c->version);

foreach ($c->plugins as $id => $options) {
	if ($options === false || $options === null)
		continue;

	$plugin_options = Application::pluginOptions($id);
	$plugin_class = $plugin_options->class;

	$reflection = new ClassReflection($plugin_class);
	$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);

	foreach ($methods as $method) {
		$docblock = $method->getDocBlock();
		if (!$docblock)
			continue;

		$tag = $docblock->getTag("command");
		if ($tag === false)
			continue;

		$command = new Cli\CommandCallback($tag->getContent());

		// Description
		$description = $docblock->getShortDescription();
		if ($description)
			$command->setDescription($description);

		// Arguments / Options
		foreach ($docblock->getTags() as $tag) {
			switch ($tag->getName()) {
				case 'argument':
					$argument = explode(' ', $tag->getContent(), 3);

					if (count($argument) != 3)
						continue;

					$types = $argument[1];
					$types = explode('|', $types);
					$type = 0;
					foreach ($types as $t) {
						$type |= constant("Symfony\Component\Console\Input\InputArgument::$t");
					}

					$command->addArgument($argument[0], $type, $argument[2]);
				break;

				case 'option':
					$option = explode(' ', $tag->getContent(), 3);

					if (count($option) != 3)
						continue;

					$name = $option[0];
					$shortcut = null;

					$name = explode(',', $name);
					if (count($name) == 2)
						$shortcut = $name[1];
					$name = $name[0];

					$types = $option[1];
					$types = explode('|', $types);
					$type = 0;
					foreach ($types as $t) {
						$type |= constant("Symfony\Component\Console\Input\InputOption::$t");
					}

					$command->addOption($name, $shortcut, $type, $option[2]);
				break;
			}
		}

		$command->setCallback($method->getClosure());
		$app->add($command);
	}

	if (method_exists($plugin_class, 'cli'))
		$plugin_class::cli($app, $plugin_options);
}

$app->run();
