Created
April 30, 2012 11:52
-
-
Save roderik/2557617 to your computer and use it in GitHub Desktop.
BillSplitCommand v3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Kunstmaan\Hosting\BillSplitBundle\Command; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use SimpleXMLElement; | |
use SplFileObject; | |
use SplFileInfo; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Yaml\Parser; | |
class BillSplitCommand extends ContainerAwareCommand | |
{ | |
protected function configure() | |
{ | |
$this | |
->setName('bill:split') | |
->setDescription('Split the bill') | |
->addArgument('billpath', InputArgument::REQUIRED, 'The path to the bill yml file') | |
->addArgument('nagiospath', InputArgument::REQUIRED, 'The path to the nagios.xml file'); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$bill = $this->parseBill($input->getArgument('billpath'), $output); | |
$nagios = $this->parseNagios($input->getArgument('nagiospath'), $output); | |
} | |
/** | |
* Parses the YAML file containing the bill content into an array. | |
* | |
* @param string $billpath | |
* @param OutputInterface $output | |
* @return array a multidimensional array with the bill.yml content | |
*/ | |
private function parseBill($billpath, OutputInterface $output) | |
{ | |
$output->writeln(" - Locating and parsing the Bill input file: " . $billpath); | |
$content = file_get_contents($billpath); | |
$yaml = new Parser(); | |
return $yaml->parse($content); | |
} | |
/** | |
* Parses the XML file containing the projects on a server into an array. | |
* | |
* @param string $nagiospath | |
* @param OutputInterface $output | |
* @return array a multidimensional array with the nagios.xml content | |
*/ | |
private function parseNagios($nagiospath, OutputInterface $output) | |
{ | |
$output->writeln(" - Locating and parsing the Nagios input file: " . $nagiospath); | |
$content = file_get_contents($nagiospath); | |
$result = array(); | |
$xml = new SimpleXMLElement($content); | |
foreach ($xml->children() as $project) { | |
if ($project->monitor != "false") { | |
$result[] = (string)$project->name; | |
} | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment