Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e9b570e
Showing
12 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
composer.lock | ||
vendor |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
Content Analysis | ||
================ | ||
|
||
Analyze content to determine the appropriate [Internet media type][1]. | ||
|
||
|
||
Requirements | ||
------------ | ||
|
||
* PHP 5.3+ | ||
|
||
|
||
|
||
Installation | ||
------------ | ||
|
||
Through [Composer][2] as [dflydev/content-analysis][3]. | ||
|
||
|
||
Usage | ||
----- | ||
|
||
// Instantiate the Analyzer | ||
$analyzer = new Dflydev\ContentAnalysis\Analyzer\Analyzer; | ||
|
||
// Detect a media type from a filename (file does not need to exist) | ||
$mediaType = $analyzer->detectFromFilename('/path/to/whatever.png'); | ||
|
||
// See the media type as a string | ||
print $mediaType."\n"; | ||
|
||
// Explore the media type | ||
print var_dump($mediaType); | ||
|
||
// image/png | ||
// | ||
// object(webignition\InternetMediaType\InternetMediaType)#8 (3) { | ||
// ["type":"webignition\InternetMediaType\InternetMediaType":private]=> | ||
// string(5) "image" | ||
// ["subtype":"webignition\InternetMediaType\InternetMediaType":private]=> | ||
// string(3) "png" | ||
// ["parameters":"webignition\InternetMediaType\InternetMediaType":private]=> | ||
// array(0) { | ||
// } | ||
//} | ||
|
||
|
||
|
||
License | ||
------- | ||
|
||
MIT, see LICENSE. | ||
|
||
|
||
Not Invented Here | ||
----------------- | ||
|
||
This work was heavily influenced by [Apache Tika][4] and [Ferret][5]. | ||
|
||
|
||
[1]: http://en.wikipedia.org/wiki/Internet_media_type | ||
[2]: http://getcomposer.org | ||
[3]: https://packagist.org/packages/dflydev/content-analysis | ||
[4]: http://tika.apache.org | ||
[5]: https://github.com/versionable/Ferret |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "dflydev/content-analysis", | ||
"description": "Internet media type analysis framework", | ||
"keywords": ["mime", "content", "type", "detection"], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Dragonfly Development Inc.", | ||
"email": "info@dflydev.com", | ||
"homepage": "http://dflydev.com" | ||
}, | ||
{ | ||
"name": "Beau Simensen", | ||
"email": "beau@dflydev.com", | ||
"homepage": "http://beausimensen.com" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.3.3", | ||
"dflydev/apache-mime-types": "1.0.*", | ||
"webignition/internet-media-type": "0.*" | ||
}, | ||
"autoload": { | ||
"psr-0": { | ||
"Dflydev\\ContentAnalysis": "src" | ||
} | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.0-dev" | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Dflydev\ContentAnalysis\Analyzer; | ||
|
||
use Dflydev\ContentAnalysis\Detector\DefaultDetectorFactory; | ||
use Dflydev\ContentAnalysis\Detector\DetectorInterface; | ||
use Dflydev\ContentAnalysis\MediaType\DefaultMediaTypeParserFactory; | ||
use Dflydev\ContentAnalysis\MediaType\MediaTypeParserInterface; | ||
use Dflydev\ContentAnalysis\Metadata\Metadata; | ||
|
||
class Analyzer | ||
{ | ||
private $detector; | ||
private $mediaTypeParser; | ||
|
||
public function __construct(DetectorInterface $detector = null) | ||
{ | ||
if (null === $detector) { | ||
$detector = DefaultDetectorFactory::create(); | ||
} | ||
|
||
$this->detector = $detector; | ||
$this->mediaTypeParser = DefaultMediaTypeParserFactory::create(); | ||
} | ||
|
||
public function setDetector(DetectorInterface $detector) | ||
{ | ||
$this->detector = $detector; | ||
|
||
return $this; | ||
} | ||
|
||
public function setMediaTypeParser(MediaTypeParserInterface $mediaTypeParser) | ||
{ | ||
$this->mediaTypeParser = $mediaTypeParser; | ||
|
||
return $this; | ||
} | ||
|
||
public function detectFromFilename($filename) | ||
{ | ||
$metadata = new Metadata; | ||
$metadata->set(Metadata::RESOURCE_NAME_KEY, $filename); | ||
|
||
return $this->detector->detect($this->mediaTypeParser, null, $metadata); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Dflydev/ContentAnalysis/Detector/ApacheMimeTypesExtensionDetector.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Dflydev\ContentAnalysis\Detector; | ||
|
||
use Dflydev\ApacheMimeTypes\PhpRepository; | ||
use Dflydev\ApacheMimeTypes\RepositoryInterface; | ||
use Dflydev\ContentAnalysis\MediaType\MediaTypeParserInterface; | ||
use Dflydev\ContentAnalysis\Metadata\Metadata; | ||
|
||
class ApacheMimeTypesExtensionDetector implements DetectorInterface | ||
{ | ||
private $repository; | ||
|
||
public function __construct(RepositoryInterface $repository = null) | ||
{ | ||
if (null === $repository) { | ||
$repository = new PhpRepository; | ||
} | ||
|
||
$this->repository = $repository; | ||
} | ||
|
||
public function detect(MediaTypeParserInterface $mediaTypeParser, $input = null, Metadata $metadata = null) | ||
{ | ||
if (null === $metadata) { | ||
return null; | ||
} | ||
|
||
$filepath = $metadata->get(Metadata::RESOURCE_NAME_KEY); | ||
|
||
if (null === $filepath) { | ||
return null; | ||
} | ||
|
||
$extension = strtolower(pathinfo($filepath, PATHINFO_EXTENSION)); | ||
|
||
$type = $this->repository->findType($extension); | ||
|
||
if (null !== $type) { | ||
return $mediaTypeParser->parse($type); | ||
} | ||
|
||
return null; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Dflydev/ContentAnalysis/Detector/CompositeDetector.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Dflydev\ContentAnalysis\Detector; | ||
|
||
use Dflydev\ContentAnalysis\MediaType\MediaTypeParserInterface; | ||
use Dflydev\ContentAnalysis\Metadata\Metadata; | ||
|
||
class CompositeDetector implements DetectorInterface | ||
{ | ||
private $detectors; | ||
|
||
public function __construct(array $detectors = null) | ||
{ | ||
if (null === $detectors) { | ||
$detectors = array(); | ||
} | ||
|
||
$this->detectors = $detectors; | ||
} | ||
|
||
public function addDetector(DetectorInterface $detector) | ||
{ | ||
$this->detectors[] = $detector; | ||
} | ||
|
||
public function detect(MediaTypeParserInterface $mediaTypeParser, $input = null, Metadata $metadata = null) | ||
{ | ||
foreach ($this->detectors as $detector) { | ||
$type = $detector->detect($mediaTypeParser, $input, $metadata); | ||
|
||
if (null !== $type) { | ||
return $type; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Dflydev/ContentAnalysis/Detector/DefaultDetectorFactory.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Dflydev\ContentAnalysis\Detector; | ||
|
||
class DefaultDetectorFactory | ||
{ | ||
public static function create() | ||
{ | ||
return new ApacheMimeTypesExtensionDetector; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Dflydev/ContentAnalysis/Detector/DetectorInterface.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Dflydev\ContentAnalysis\Detector; | ||
|
||
use Dflydev\ContentAnalysis\MediaType\MediaTypeParserInterface; | ||
use Dflydev\ContentAnalysis\Metadata\Metadata; | ||
|
||
interface DetectorInterface | ||
{ | ||
public function detect(MediaTypeParserInterface $mediaTypeParser, $input = null, Metadata $metadata = null); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Dflydev/ContentAnalysis/MediaType/DefaultMediaTypeParserFactory.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Dflydev\ContentAnalysis\MediaType; | ||
|
||
class DefaultMediaTypeParserFactory | ||
{ | ||
public static function create() | ||
{ | ||
return new WebignitionMediaTypeParser; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Dflydev/ContentAnalysis/MediaType/MediaTypeParserInterface.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Dflydev\ContentAnalysis\MediaType; | ||
|
||
interface MediaTypeParserInterface | ||
{ | ||
public function parse($type); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Dflydev/ContentAnalysis/MediaType/WebignitionMediaTypeParser.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Dflydev\ContentAnalysis\MediaType; | ||
|
||
use webignition\InternetMediaType\Parser\Parser; | ||
|
||
class WebignitionMediaTypeParser implements MediaTypeParserInterface | ||
{ | ||
private $parser; | ||
|
||
public function __construct(Parser $parser = null) | ||
{ | ||
if (null === $parser) { | ||
$parser = new Parser; | ||
} | ||
|
||
$this->parser = $parser; | ||
} | ||
|
||
public function parse($type) | ||
{ | ||
return $this->parser->parse($type); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Dflydev\ContentAnalysis\Metadata; | ||
|
||
class Metadata | ||
{ | ||
const RESOURCE_NAME_KEY = 'resourceName'; | ||
|
||
private $metadata = array(); | ||
|
||
public function get($name) | ||
{ | ||
$values = $this->getList($name); | ||
|
||
if (count($values)) { | ||
return $values[0]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function getList($name) | ||
{ | ||
if (!isset($this->metadata[$name])) { | ||
return array(); | ||
} | ||
|
||
return $this->metadata[$name]; | ||
} | ||
|
||
public function set($name, $value) | ||
{ | ||
if (!isset($this->metadata[$name])) { | ||
$this->metadata[$name] = array(); | ||
} | ||
|
||
$this->metadata[$name] = is_array($value) ? $value : array($value); | ||
} | ||
} |