Skip to content

Commit

Permalink
PSR2, composer and Pico 2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
nliautaud committed Jul 4, 2018
1 parent 59a0cc2 commit 5902782
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
67 changes: 38 additions & 29 deletions PicoPagesImages.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

/**
* Access the images of the current page with {{ images }} in Pico CMS.
* Get the images of the current page with {{ images }} in Pico CMS.
*
* @author Nicolas Liautaud
* @author Nicolas Liautaud <[email protected]>
* @license http://opensource.org/licenses/MIT The MIT License
* @link http://nliautaud.fr
* @link http://picocms.org
* @license http://opensource.org/licenses/MIT The MIT License
*/
class PicoPagesImages extends AbstractPicoPlugin
{
private $path = '';
const API_VERSION = 2;
private $path;
private $root;

/**
Expand All @@ -19,22 +19,27 @@ class PicoPagesImages extends AbstractPicoPlugin
*
* Triggered after Pico has discovered the content file to serve
*
* @see Pico::getBaseUrl()
* @see Pico::getRequestFile()
* @param string &$file absolute path to the content file to serve
* @see Pico::resolveFilePath()
* @see Pico::getRequestFile()
*
* @param string &$file absolute path to the content file to serve
*
* @return void
*/
public function onRequestFile(&$requestFile)
public function onRequestFile(&$file)
{
$contentDir = $this->getConfig('content_dir');
$contentDirLength = strlen($contentDir);
if (substr($requestFile, 0, $contentDirLength) !== $contentDir)
return;
if (substr($file, 0, $contentDirLength) !== $contentDir) {
return;
}
$contentExt = $this->getConfig('content_ext');
$this->path = substr($requestFile, $contentDirLength);
$this->path = substr($file, $contentDirLength);
$this->path = rtrim($this->path, "index$contentExt");
$this->path = rtrim($this->path, $contentExt);
if ($this->path) $this->path .= '/';
if ($this->path) {
$this->path .= '/';
}
}
/**
* Triggered after Pico has read its configuration
Expand All @@ -45,44 +50,49 @@ public function onRequestFile(&$requestFile)
*/
public function onConfigLoaded(array &$config)
{
if (!empty($config['images_path']))
if (!empty($config['images_path'])) {
$this->root = rtrim($config['images_path'], '/') . '/';
else $this->root = 'images/';
} else {
$this->root = 'images/';
}
}
/**
* Triggered before Pico renders the page
*
* @see Pico::getTwig()
* @see DummyPlugin::onPageRendered()
* @param Twig_Environment &$twig twig template engine
* @param array &$twigVariables template variables
* @param string &$templateName file name of the template
* @see DummyPlugin::onPageRendered()
*
* @param string &$templateName file name of the template
* @param array &$twigVariables template variables
*
* @return void
*/
public function onPageRendering(Twig_Environment &$twig, array &$twigVariables, &$templateName)
public function onPageRendering(&$templateName, array &$twigVariables)
{
$twigVariables['images'] = $this->images_list();
$twigVariables['images'] = $this->getImages();
}
/**
* Return the list and infos of images in the current directory.
*
* @return array
*/
private function images_list()
private function getImages()
{
$images_path = $this->root . $this->path;

$data = array();
$pattern = '*.{[jJ][pP][gG],[jJ][pP][eE][gG],[pP][nN][gG],[gG][iI][fF]}';
$images = glob($images_path . $pattern, GLOB_BRACE);

if (!is_array($images)) return array();
if (!is_array($images)) {
return array();
}

foreach( $images as $path )
{
foreach ($images as $path) {
$imagesize = getimagesize($path);
if (!is_array($imagesize)) $imagesize = array();
list($width, $height, $type, $size) = array_pad($imagesize, 4, '');
if (!is_array($imagesize)) {
$imagesize = array();
}
list($width, $height,, $size) = array_pad($imagesize, 4, '');

$data[] = array (
'url' => $this->getBaseUrl() . $images_path . pathinfo($path, PATHINFO_BASENAME),
Expand All @@ -97,4 +107,3 @@ private function images_list()
return $data;
}
}
?>
30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "nliautaud/pico-pages-images",
"type": "pico-plugin",
"description": "Get images corresponding to the current page in Pico CMS",
"keywords": [ "pico", "picocms", "picocms-plugin", "pico-pages-images"],
"homepage": "http://picocms.org/",
"license": "MIT",
"authors": [
{
"name": "Nicolas Liautaud",
"homepage": "https://github.com/nliautaud/pico-pages-images",
"role": "Lead Developer"
},
{
"name": "Contributors",
"homepage": "https://github.com/nliautaud/pico-pages-images/graphs/contributors"
}
],
"support": {
"docs": "https://github.com/nliautaud/pico-pages-images/blob/master/README.md",
"issues": "https://github.com/nliautaud/pico-pages-images/issues",
"source": "https://github.com/nliautaud/pico-pages-images"
},
"require": {
"php": ">=5.4.0"
},
"autoload": {
"classmap": [ "PicoPagesImages.php" ]
}
}

0 comments on commit 5902782

Please sign in to comment.