Skip to content

Migrating from 2.x.x to 3.x.x

mreiche edited this page Jul 15, 2016 · 8 revisions

Architecture

Caching and logging functionalities were removed from essence. As they were used mainly on the top level of the library (i.e. in the Essence class), it made sense to let the user handle them directly. Removing those functionalities simplified the code and shouldn't make it more complicated to setup.

$url = 'http://...';

// 2.x.x
// using essence-interfaces (https://github.com/felixgirault/essence-interfaces)

$Essence = Essence\Essence::instance([
	'Cache' => Essence\Di\Container::unique(function() {
		return new Essence\Cache\Engine\Doctrine(
			new Doctrine\Common\Cache\ArrayCache()
		);
	})
]);

$Media = $Essence->embed($url);

// 3.x.x

$Cache = new Doctrine\Common\Cache\ArrayCache();

if (!$Cache->contains($url)) {
	$Cache->save($url, $Essence->extract($url));
}

$Media = $Cache->fetch($url);

Instanciation

Essence should now be instanciated with the new keyword:

// 2.x.x
$Essence = Essence\Essence::instance();

// 3.x.x
$Essence = new Essence\Essence();

Configuration

Providers configuration now lies into the DI container.

This configuration is now separated in two components: some factories that handle the creation of the different providers, and filters that associate URL patterns to the providers.

This allows for a better separation of concern:

// 2.x.x
$Essence = Essence\Essence::instance([
	'providers' => [
		'SoundCloud' => [
			'class' => 'OEmbed',
			'filter' => '#soundcloud\.com/[a-zA-Z0-9-_]+/[a-zA-Z0-9-]+#i',
			'endpoint' => 'http://soundcloud.com/oembed?format=json&url=%s'
		]
	]
]);

// 3.x.x
$Essence = new Essence\Essence([
	// the factory
	'SoundCloud' => Container::unique(function($C) {
		return $C->get('OEmbedProvider')->setEndpoint(
			'http://soundcloud.com/oembed?format=json&url=:url'
		);
	}),

	// the filter
	'filters' => [
		'SoundCloud' => '~soundcloud\.com/[a-zA-Z0-9-_]+/[a-zA-Z0-9-]+~i'
	]
]);

API changes

Some methods of the Essence class were renamed:

// 2.x.x
$Essence->extract($source); // crawls extractable URLs from an URL or an HTML source
$Essence->embed($url); // extracts information about an URL
$Essence->embedAll($urls); // extracts information about many URLs

// 3.x.x
$Essence->crawl($html); // crawls extractable URLs from an HTML source
$Essence->crawlUrl($url); // crawls extractable URLs from an URL
$Essence->extract($url); // extracts information about an URL
$Essence->extractAll($urls); // extracts information about many URLs

Basically, extract() becomes crawl()/crawlurl(""), and embed()/embedAll() become extract()/extractAll().

I'm aware that it could be confusing for existing users, but it should be more meaningful for the new ones.

Clone this wiki locally