コード例 #1
0
 /**
  * @param OutputInterface $output
  */
 private function getLocations(OutputInterface $output)
 {
     $output->writeln('<info>Starting to get Locations</info>');
     $output->writeln('<info>URL: ' . $this->url . 'Locations+Overview</info>');
     $html = file_get_contents($this->url . 'Locations+Overview');
     $crawler = new Crawler($html);
     /* Get Regions by Faction */
     $factionRegions = $crawler->filter('.span_1_of_4');
     foreach ($factionRegions as $domElement) {
         /* @var \DOMElement $domElement */
         $content = trim($domElement->textContent);
         $locations = array_map('trim', explode("\n", $content));
         $output->writeln('<info>Getting Factions</info>');
         $faction = $this->em->getRepository('AppBundle:Faction')->findOneBy(array('name' => $locations[0]));
         $output->writeln('<info>Faction: ' . $locations[0] . '</info>');
         if (!$faction instanceof Faction) {
             $faction = new Faction();
             $faction->setName($locations[0]);
             $this->em->persist($faction);
         }
         unset($locations[0]);
         $output->writeln('<info>Getting Regions</info>');
         foreach ($locations as $r) {
             $output->writeln('<info>Region: ' . $r . '</info>');
             $region = $this->em->getRepository('AppBundle:Region')->findOneBy(array('name' => $r));
             if (!$region instanceof Region) {
                 $region = new Region();
                 $region->setName($r);
                 $region->setFaction($faction);
                 $this->em->persist($region);
             }
         }
     }
     $this->em->flush();
     /* Get Region by Province */
     $provinceRegions = $crawler->filter('table.wiki_table');
     $output->writeln('<info>Getting provinces</info>');
     $provincesArray = array();
     foreach ($provinceRegions->children() as $domElement) {
         $output->writeln('<info>Making a Province Array</info>');
         /* @var \DOMElement $domElement */
         $content = trim($domElement->textContent);
         $contentArray = array_map('trim', explode("\n", $content));
         $index = $contentArray[0];
         unset($contentArray[0]);
         if ($index != 'Province') {
             $provincesArray[$index] = array_map('trim', explode(',', $contentArray[1]));
         }
     }
     $output->writeln('<info>Saving Factions</info>');
     foreach ($provincesArray as $prov => $regions) {
         $output->writeln('<info>' . $prov . '</info>');
         $province = $this->em->getRepository('AppBundle:Province')->findOneBy(array('name' => $prov));
         if (!$province instanceof Province) {
             $province = new Province();
             $province->setName($prov);
             $this->em->persist($province);
         }
         foreach ($regions as $r) {
             $output->writeln('<info>' . $r . '</info>');
             $region = $this->em->getRepository('AppBundle:Region')->findOneBy(array('name' => $r));
             if (!$region instanceof Region) {
                 $region = new Region();
                 $region->setName($r);
             }
             $region->setProvince($province);
             $this->em->persist($region);
         }
     }
     $this->em->flush();
     $output->writeln('<info>We\'re done.</info>');
 }