コード例 #1
0
ファイル: FeedController.php プロジェクト: alphadevx/alpha
 /**
  * Handle GET requests.
  *
  * @param Alpha\Util\Http\Request $request
  *
  * @return Alpha\Util\Http\Response
  *
  * @since 1.0
  *
  * @throws Alpha\Exception\ResourceNotFoundException
  */
 public function doGET($request)
 {
     self::$logger->debug('>>doGET($request=[' . var_export($request, true) . '])');
     $config = ConfigProvider::getInstance();
     $params = $request->getParams();
     $response = new Response(200);
     try {
         if (isset($params['ActiveRecordType'])) {
             $ActiveRecordType = urldecode($params['ActiveRecordType']);
         } else {
             throw new IllegalArguementException('ActiveRecordType not specified to generate feed!');
         }
         if (isset($params['type'])) {
             $type = $params['type'];
         } else {
             throw new IllegalArguementException('No feed type specified to generate feed!');
         }
         if (class_exists($ActiveRecordType)) {
             $this->ActiveRecordType = $ActiveRecordType;
         } else {
             throw new IllegalArguementException('No ActiveRecord available to render!');
         }
         $this->type = $type;
         $this->setup();
         switch ($type) {
             case 'RSS2':
                 $feed = new RSS2($this->ActiveRecordType, $this->title, str_replace('&', '&', $request->getURI()), $this->description);
                 $feed->setFieldMappings($this->fieldMappings[0], $this->fieldMappings[1], $this->fieldMappings[2], $this->fieldMappings[3]);
                 $response->setHeader('Content-Type', 'application/rss+xml');
                 break;
             case 'RSS':
                 $feed = new RSS($this->ActiveRecordType, $this->title, str_replace('&', '&', $request->getURI()), $this->description);
                 $feed->setFieldMappings($this->fieldMappings[0], $this->fieldMappings[1], $this->fieldMappings[2], $this->fieldMappings[3]);
                 $response->setHeader('Content-Type', 'application/rss+xml');
                 break;
             case 'Atom':
                 $feed = new Atom($this->ActiveRecordType, $this->title, str_replace('&', '&', $request->getURI()), $this->description);
                 $feed->setFieldMappings($this->fieldMappings[0], $this->fieldMappings[1], $this->fieldMappings[2], $this->fieldMappings[3], $this->fieldMappings[4]);
                 if ($config->get('feeds.atom.author') != '') {
                     $feed->addAuthor($config->get('feeds.atom.author'));
                 }
                 $response->setHeader('Content-Type', 'application/atom+xml');
                 break;
         }
         // now add the twenty last items (from newest to oldest) to the feed, and render
         $feed->loadBOs(20, $this->sortBy);
         $response->setBody($feed->render());
         // log the request for this news feed
         $feedLog = new LogProviderFile();
         $feedLog->setPath($config->get('app.file.store.dir') . 'logs/feeds.log');
         $feedLog->writeLine(array($this->ActiveRecordType, $this->type, date('Y-m-d H:i:s'), $request->getUserAgent(), $request->getIP()));
     } catch (IllegalArguementException $e) {
         self::$logger->error($e->getMessage());
         throw new ResourceNotFoundException($e->getMessage());
     }
     self::$logger->debug('<<doGet');
     return $response;
 }
コード例 #2
0
ファイル: FeedTest.php プロジェクト: alphadevx/alpha
 public function testAddItemToRSSandParse()
 {
     $feed = new RSS('Alpha\\Model\\Article', 'Test Feed Title', 'http://www.alphaframework.org/', 'Test Feed Description');
     $feed->setFieldMappings('title', 'URL', 'description', 'created_ts', 'OID');
     $feed->addBO($this->BO);
     $xml = $feed->render();
     $reader = new XMLReader();
     $validXML = $reader->XML($xml);
     $this->assertTrue($validXML, 'Confirming that the generated XML can be parsed correctly');
     $simpleXML = new SimpleXMLElement($xml);
     $simpleXML->registerXPathNamespace('rss', 'http://purl.org/rss/1.0/');
     $channels = $simpleXML->xpath('//rss:channel');
     $this->assertEquals('Test Feed Title', (string) $channels[0]->title, 'Testing that the feed title is present');
     $this->assertEquals('http://www.alphaframework.org/', (string) $channels[0]->link, 'Testing that the feed URL is present');
     $items = $simpleXML->xpath('//rss:item');
     $this->assertEquals('Test Article Title', (string) $items[0]->title, 'Testing that the feed item title is present');
     $this->assertEquals('Test Article Description', (string) $items[0]->description, 'Testing that the feed item description is present');
     $this->assertEquals('2011-01-01T00:00:00+00:00', (string) $items[0]->updated, 'Testing that the feed item publish time is present');
 }