123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace Aws\CloudTrail;
- use Aws\S3\S3Client;
- class LogRecordIterator implements \OuterIterator
- {
-
- private $logFileReader;
-
- private $logFileIterator;
-
- private $records;
-
- private $recordIndex;
-
- public static function forTrail(
- S3Client $s3Client,
- CloudTrailClient $cloudTrailClient,
- array $options = []
- ) {
- $logFileIterator = LogFileIterator::forTrail(
- $s3Client,
- $cloudTrailClient,
- $options
- );
- return new self(new LogFileReader($s3Client), $logFileIterator);
- }
-
- public static function forBucket(
- S3Client $s3Client,
- $s3BucketName,
- array $options = []
- ) {
- $logFileReader = new LogFileReader($s3Client);
- $iter = new LogFileIterator($s3Client, $s3BucketName, $options);
- return new self($logFileReader, $iter);
- }
-
- public static function forFile(
- S3Client $s3Client,
- $s3BucketName,
- $s3ObjectKey
- ) {
- $logFileReader = new LogFileReader($s3Client);
- $logFileIterator = new \ArrayIterator([[
- 'Bucket' => $s3BucketName,
- 'Key' => $s3ObjectKey,
- ]]);
- return new self($logFileReader, $logFileIterator);
- }
-
- public function __construct(
- LogFileReader $logFileReader,
- \Iterator $logFileIterator
- ) {
- $this->logFileReader = $logFileReader;
- $this->logFileIterator = $logFileIterator;
- $this->records = array();
- $this->recordIndex = 0;
- }
-
-
- public function current()
- {
- return $this->valid() ? $this->records[$this->recordIndex] : false;
- }
-
- public function next()
- {
- $this->recordIndex++;
-
-
- while (!$this->valid()) {
- $this->logFileIterator->next();
- $success = $this->loadRecordsFromCurrentLogFile();
- if (!$success) {
-
- break;
- }
- }
- }
-
- public function key()
- {
- if ($logFile = $this->logFileIterator->current()) {
- return $logFile['Key'] . '.' . $this->recordIndex;
- }
- return null;
- }
-
- public function valid()
- {
- return isset($this->records[$this->recordIndex]);
- }
-
- public function rewind()
- {
- $this->logFileIterator->rewind();
- $this->loadRecordsFromCurrentLogFile();
- }
-
- public function getInnerIterator()
- {
- return $this->logFileIterator;
- }
-
- private function loadRecordsFromCurrentLogFile()
- {
- $this->recordIndex = 0;
- $this->records = array();
- $logFile = $this->logFileIterator->current();
- if ($logFile && isset($logFile['Bucket']) && isset($logFile['Key'])) {
- $this->records = $this->logFileReader->read(
- $logFile['Bucket'],
- $logFile['Key']
- );
- }
- return (bool) $logFile;
- }
- }
|