Skip to content

Commit

Permalink
Initial drop.
Browse files Browse the repository at this point in the history
  • Loading branch information
simensen committed Jan 4, 2013
0 parents commit e9b570e
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
composer.lock
vendor
65 changes: 65 additions & 0 deletions README.md
@@ -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
33 changes: 33 additions & 0 deletions composer.json
@@ -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"
}
}
}
47 changes: 47 additions & 0 deletions src/Dflydev/ContentAnalysis/Analyzer/Analyzer.php
@@ -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);
}
}
@@ -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 src/Dflydev/ContentAnalysis/Detector/CompositeDetector.php
@@ -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 src/Dflydev/ContentAnalysis/Detector/DefaultDetectorFactory.php
@@ -0,0 +1,11 @@
<?php

namespace Dflydev\ContentAnalysis\Detector;

class DefaultDetectorFactory
{
public static function create()
{
return new ApacheMimeTypesExtensionDetector;
}
}
11 changes: 11 additions & 0 deletions src/Dflydev/ContentAnalysis/Detector/DetectorInterface.php
@@ -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);
}
@@ -0,0 +1,11 @@
<?php

namespace Dflydev\ContentAnalysis\MediaType;

class DefaultMediaTypeParserFactory
{
public static function create()
{
return new WebignitionMediaTypeParser;
}
}
@@ -0,0 +1,8 @@
<?php

namespace Dflydev\ContentAnalysis\MediaType;

interface MediaTypeParserInterface
{
public function parse($type);
}
@@ -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);
}
}
39 changes: 39 additions & 0 deletions src/Dflydev/ContentAnalysis/Metadata/Metadata.php
@@ -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);
}
}

0 comments on commit e9b570e

Please sign in to comment.