Skip to content

Commit

Permalink
v1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian committed Feb 10, 2016
1 parent aba6c66 commit 77907f5
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 73 deletions.
215 changes: 143 additions & 72 deletions styla/magazine/components/Magazine.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php namespace Styla\Magazine\Components;

use Cms\Classes\ComponentBase;
use Request;
use Cache;

class Magazine extends ComponentBase
{
Expand All @@ -25,14 +27,14 @@ public function defineProperties()
'type' => [
'description' => 'Type of content to be fetched.',
'title' => 'Type',
'default' => 'Story',
'default' => 'magazine',
'type' => 'dropdown',
'options' => ['magazine' => 'Magazine', 'story' => 'Story', 'tag' => 'Tag']
],
'integration' => [
'description' => 'Snippet or CDN Integration switch.',
'title' => 'Integration',
'default' => 'Snippet',
'default' => 'snippet',
'type' => 'dropdown',
'options' => ['snippet' => 'Snippet', 'cdn' => 'CDN']
],
Expand All @@ -41,38 +43,65 @@ public function defineProperties()
'title' => 'Feedlimit (optional)',
'default' => '',
'type' => 'string'
]
],
'duration' => [
'description' => 'Duration in minutes.',
'title' => 'Caching duration',
'default' => 20,
'type' => 'string',
'required' => true,
'validationPattern' => '^[0-9]+$',
'validationMessage' => 'The Duration property can contain only numeric symbols'
],
'environment' => [
'description' => 'Source of the magazine domain. Stage only works with Snippet integration',
'title' => 'Environment',
'default' => 'live',
'type' => 'dropdown',
'depends' => ['integration'],
'options' => ['live' => 'Live', 'stage' => 'Stage']
],
];
}

public function getEnvironmentOptions()
{
$integration = Request::input('integration'); // Load the country property value from POST

$environments = [
'snippet' => ['live'=>'Live', 'stage'=>'Stage'],
'cdn' => ['live'=>'Live']
];

return $environments[$integration];
}

public function onRun()
{
$domain = $this->property('domain'); # styla Username
$type = $this->property('type'); # type of the site (set in plugin)
$param = $this->param('param'); # url parameter
$integration = $this->property('integration'); # Integration type
$feedlimit = $this->property('feedlimit'); # Enable/Disable feedlimit

// Pass to page - DEBUG reasons
$this->page['domain'] = $domain;
$this->page['type'] = $type;
$this->page['param'] = $param;
$this->page['integration'] = $integration;
$this->page['feedlimit'] = $feedlimit;

// --- Create Preloader ---------------------
if($feedlimit){
$this->page['Preloader'] = '<script id="stylaMagazine" src="//live.styla.com/scripts/preloader/'.$domain.'.js" data-feedlimit="'.$feedlimit.'"></script>';
$this->page['domain'] = $this->property('domain'); # styla Username
$this->page['type'] = $this->property('type'); # type of the site (set in plugin)
$this->page['param'] = $this->param('param'); # url parameter
$this->page['integration'] = $this->property('integration'); # Integration type
$this->page['feedlimit'] = $this->property('feedlimit'); # Enable/Disable feedlimit
$this->page['duration'] = $this->property('duration'); # Duration for caching
$this->page['environment'] = $this->property('environment'); # source for magazine (live.styla.com or stage.styla.com)

// --- Create Snippet ---------------------

if($this->property('feedlimit')){
$this->page['Preloader'] = '<script id="stylaMagazine" src="//'.$this->property('environment').'.styla.com/scripts/preloader/'.$this->property('domain').'.js" data-feedlimit="'.$this->property('feedlimit').'"></script>';
}
else{
$this->page['Preloader'] = '<script id="stylaMagazine" src="//live.styla.com/scripts/preloader/'.$domain.'.js"></script>';
$this->page['Preloader'] = '<script id="stylaMagazine" src="//'.$this->property('environment').'.styla.com/scripts/preloader/'.$this->property('domain').'.js"></script>';
}

// --- Fetch SEO content ----------------------
switch($type){

switch($this->property('type')){
case 'tag':
if($param != ""){
$json = @file_get_contents('http://seo.styla.com/clients/'.$domain.'?url=tag%2F'.$param);
if($this->param('param') != ""){
$json = $this->fetchAndRemember( $this->property('duration'), $this->property('environment'), 'tag' );
if($json != FALSE){
$obj = json_decode($json);
$this->page['SEO_head'] = $obj->html->head;
Expand All @@ -82,74 +111,116 @@ public function onRun()
break;

case 'story':
$json = @file_get_contents('http://seo.styla.com/clients/'.$domain.'?url=story%2F'.$param);
$json = $this->fetchAndRemember( $this->property('duration'), $this->property('environment'), 'story' );
if($json != FALSE){
$obj = json_decode($json);
$this->page['SEO_head'] = $obj->html->head;
$this->page['SEO_body'] = $obj->html->body;
}
break;

case 'magazine':
$json = @file_get_contents('http://seo.styla.com/clients/'.$domain.'?url=user%2F'.$domain);
if($json != FALSE){
case 'magazine':
$json = $this->fetchAndRemember( $this->property('duration'), $this->property('environment'), 'magazine' );
if($json != FALSE){
$obj = json_decode($json);
$this->page['SEO_head'] = $obj->html->head;
$this->page['SEO_body'] = $obj->html->body;
}
break;
}

// --- CDN Stuff ----------------------------
$filename = "version.txt";
$currentTime = time();
$age = null;
$max_age = 100;
$version = null;
// --- CDN Version ----------------------------

if (file_exists($filename)) {
$age = $currentTime - filemtime($filename);
if($age > $max_age){ // fetch new version from styla
$currentVersion = @file_get_contents('http://live.styla.com/api/version/'.$domain);
if($currentVersion != FALSE){
// Rewrite file to cache
$file = fopen($filename, "w");
$version = $currentVersion;
fwrite($file, $version);
fclose($file);
}
else{ // version couldnt be fetched from server -> read from cache
$file = fopen($filename, "r");
$version = fread($file, filesize($filename));
fclose($file);
}
}
else{ // Read version from cache
$file = fopen($filename, "r");
$version = fread($file, filesize($filename));
fclose($file);
//var_dump($version);
}
}
else{ // file not present yet -> create it
$currentVersion = @file_get_contents('http://live.styla.com/api/version/'.$domain);
if($currentVersion != FALSE){
// Rewrite file to cache
$file = fopen($filename, "w");
$version = $currentVersion;
fwrite($file, $version);
fclose($file);
}
}
$version = $this->fetchAndRemember( $this->property('duration'), $this->property('environment'), 'version' );
$this->page['version'] = $version;

// Create code snippets
$this->page['Styles'] = '<link rel="stylesheet" type="text/css" href="http://cdn.styla.com/styles/clients/'.$domain.'.css?v='.$version.'">';
if($feedlimit){
$this->page['Script'] = '<script async src="http://cdn.styla.com/scripts/clients/'.$domain.'.js?v='.$version.'" data-feedlimit="'.$feedlimit.'"></script>';
// --- Create CDN code snippets ---------------------------

$this->page['Styles'] = '<link rel="stylesheet" type="text/css" href="http://cdn.styla.com/styles/clients/'.$this->property('domain').'.css?v='.$version.'">';

if($this->property('feedlimit')){
$this->page['Script'] = '<script async src="http://cdn.styla.com/scripts/clients/'.$this->property('domain').'.js?v='.$version.'" data-feedlimit="'.$this->property('feedlimit').'"></script>';
}
else{
$this->page['Script'] = '<script async src="http://cdn.styla.com/scripts/clients/'.$domain.'.js?v='.$version.'"></script>';
$this->page['Script'] = '<script async src="http://cdn.styla.com/scripts/clients/'.$this->property('domain').'.js?v='.$version.'"></script>';
}

}

/***************************************
* C A C H E F U N C T I O N S
***************************************/

// Fetch and remember
public function fetchAndRemember($minutes, $environment, $type){

$key = '';
$url = '';

switch($type){

case 'version':
$key = 'styla_CDN_version';

if($environment == 'live'){
$url = 'http://live.styla.com/api/version/'.$this->property('domain');
}
else{
$url = 'http://stage.styla.com/api/version/'.$this->property('domain');
}
break;

case 'magazine':
$key = 'styla_SEO_magazine';

if($environment == 'live'){
$url = 'http://seo.styla.com/clients/'.$this->property('domain').'?url=user%2F'.$this->property('domain');
}
else{
$url = 'http://seoapistage1.magalog.net/clients/'.$this->property('domain').'?url=user%2F'.$this->property('domain');
}
break;

case 'story':

$key = 'styla_SEO_story_'.substr($this->page['param'], -6); // create cache key with story ID

if($environment == 'live'){
$url = 'http://seo.styla.com/clients/'.$this->property('domain').'?url=story%2F'.$this->page['param'];
}
else{
$url = 'http://seoapistage1.magalog.net/clients/'.$this->property('domain').'?url=story%2F'.$this->page['param'];
}
break;

case 'tag':

$key = 'styla_SEO_tag_'.$this->page['param'];

if($environment == 'live'){
$url = 'http://seo.styla.com/clients/'.$this->property('domain').'?url=tag%2F'.$this->property('param');
}
else{
$url = 'http://seoapistage1.magalog.net/clients/'.$this->property('domain').'?url=tag%2F'.$this->page['param'];
}
break;

default: break;
}

$value = Cache::remember($key, $minutes, function() use($url){
return @file_get_contents($url);
});

/*
// DEBUG
$this->page['url'] = $url;
$this->page['key'] = $key;
$this->page['minutes'] = $minutes;
$this->page['cache_env'] = $environment;
$this->page['cache_type'] = $type;
*/

return $value;

}
}
19 changes: 18 additions & 1 deletion styla/magazine/components/magazine/default.htm
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
{#
<pre>Type: {{ type }}</pre>
<pre>Env: {{ environment }}</pre>
<pre>Param: {{ param }}</pre>

<pre>Version: {{ version }}</pre>
<pre>------- Cache --------</pre>
<pre>Key: {{ key }}</pre>
<pre>Minutes: {{ minutes }}</pre>
<pre>Env: {{ cache_env }}</pre>
<pre>Type: {{ cache_type }}</pre>
<pre>Url: {{ url }}</pre>
<pre>Value: {{ value }}</pre>
<pre>------- SEO ----------</pre>
<pre>Head: {{ SEO_head }}</pre>
#}

{# Magazine Integration ----------- #}

{% if integration == 'snippet' %}
Expand All @@ -15,4 +32,4 @@
{% put head %}
{{ SEO_head|raw }}
{% endput %}
{{ SEO_body|raw }}
{{ SEO_body|raw }}
4 changes: 4 additions & 0 deletions styla/magazine/updates/version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
- Added CDN Integration
1.3.0:
- Tag pages now have <head> informations
1.4.0:
- Added root magazine file caching for SEO data
- Introducing helper functions for caching
- Introducing Environment setting (switch between staging and test)

0 comments on commit 77907f5

Please sign in to comment.