Skip to content

Commit

Permalink
Pinion v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
husseinalhammad committed Nov 29, 2017
1 parent ece061b commit 4bc7a0f
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 0 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Pipit Pinion

Pipit Pinion is a [Perch Feather](https://docs.grabaperch.com/api/feathers/) that helps you manage front-end assets such as CSS and Javascript through Perch.

## Installation
* Download the latest version of Pinion.
* Unzip the download
* Place the `pipit_pinion` folder in `perch/addons/feathers`
* Add Pinion to your `perch/config/feathers.php`
* Enable Feathers in your `perch/config/config.php`

### Adding Pinion to `feathers.php`
If you don't have the file `perch/config/feathers.php`, create it and add:

```php
<?php
include(PERCH_PATH.'/addons/feathers/pipit_pinion/runtime.php');
?>
```


### Enabling Feathers
You also need to enable Feathers in your `perch/config/config.php`

```php
define('PERCH_FEATHERS', true);
```


## Configuration
You can define default folders in `perch/addons/feathers/pipit_pinion/config.php` for Pinion to look for your CSS and Javascript files.

| Name | Value |
|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------|
| PIPIT_PINION_ASSETS_DIR | The name of the directory where your production assets are. The path is relative to root. |
| PIPIT_PINION_ASSETS_DEV_DIR | The name of the directory where your development assets are. Pinion uses this directory when `PERCH_PRODUCTION_MODE` is set to`PERCH_DEVELOPMENT`. The path is relative to root. |

The below example uses a folder called `assets` for production and `src` for development.

```php
define('PIPIT_PINION_ASSETS_DIR', 'assets');
define('PIPIT_PINION_ASSETS_DEV_DIR', 'src');
```

In this example, CSS files need to be in `assets/css` and `src/css`, and Javascript files need to be in `assets/js` and `src/js`. This way you can use `perch_get_css()` and `perch_get_javascript()` to link all the files from the respective folder to your document by default.

```php
.
├── assets/
│ ├── js/
│ └── css/
├── src/
│ ├── js/
│ └── css/
└── perch
```

The `perch_get_css()` and `perch_get_javascript()` functions have several options you can use giving you a lot of flexibility to control what gets inserted into your document and in what order. For more details check the [Function Reference](https://grabapipit.com/pipits/feathers/pinion/docs/functions) page.
7 changes: 7 additions & 0 deletions pipit_pinion/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
define('PIPIT_PINION_ASSETS_DIR', 'assets');
define('PIPIT_PINION_ASSETS_DEV_DIR', 'src');

define('PIPIT_PINION_ASSETS_PATH', dirname(PERCH_PATH) . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR , PIPIT_PINION_ASSETS_DIR));
define('PIPIT_PINION_ASSETS_DEV_PATH', dirname(PERCH_PATH) . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR , PIPIT_PINION_ASSETS_DEV_DIR));

92 changes: 92 additions & 0 deletions pipit_pinion/lib/PipitPinion_Helper.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

class PipitPinion_Helper
{

public function get_dir($opts, $dir_name)
{
$dir = [];

if(isset($opts['dir']))
{
$dir['path'] = dirname(PERCH_PATH) . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR , $opts['dir']);

$dir['url'] = '/'.$opts['dir'];
if(substr($opts['dir'], -1) !== '/')
{
$dir['url'] = '/'.$opts['dir'].'/';
}
}
else if(PERCH_PRODUCTION_MODE == "PERCH_DEVELOPMENT")
{
$dir['path'] = PIPIT_PINION_ASSETS_DEV_PATH . DIRECTORY_SEPARATOR . $dir_name;
$dir['url'] = '/' . PIPIT_PINION_ASSETS_DEV_DIR . '/' . $dir_name . '/';
}
else
{
$dir['path'] = PIPIT_PINION_ASSETS_PATH . DIRECTORY_SEPARATOR . $dir_name;
$dir['url'] = '/' . PIPIT_PINION_ASSETS_DIR . '/' . $dir_name . '/';
}

return $dir;
}



public function get_files($dir)
{
$files = scandir($dir);
unset($files[array_search('.', $files, true)]);
unset($files[array_search('..', $files, true)]);


foreach($files as $file)
{
if(is_dir($dir . DIRECTORY_SEPARATOR . $file))
{
$sub_files = $this->get_files($dir . DIRECTORY_SEPARATOR . $file);

foreach($sub_files as $sub_file)
{
$files[] = $file . '/' . $sub_file;
}

unset($files[array_search($file, $files, true)]);
}
}


return $files;
}



public function reorder_files($files, $pre)
{
$files = array_values($files);
for($i = count($pre); $i > 0; $i--)
{
if(array_search($pre[$i-1], $files, true))
{
unset($files[array_search($pre[$i-1], $files, true)]);
array_unshift($files, $pre[$i-1]);
}
}

return $files;
}



public function exclude_files($files, $excludes)
{
$files = array_values($files);
foreach($excludes as $exclude)
{
$result = array_search($exclude, $files, true);
unset($files[$result]);
}

return array_values($files);
}
}
143 changes: 143 additions & 0 deletions pipit_pinion/runtime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

PerchSystem::register_feather('PipitPinion');
include('config.php');
include('lib/PipitPinion_Helper.class.php');

class PerchFeather_PipitPinion extends PerchFeather
{

public function get_css($opts, $index, $count)
{
if(isset($opts['dev']) && $opts['dev'] && PERCH_PRODUCTION_MODE !== "PERCH_DEVELOPMENT")
{
return false;
}

$out = array();
$prefix = '/';

if(isset($opts['files']))
{
$files = $opts['files'];
}
else
{
$Helper = new PipitPinion_Helper();
$dir = $Helper->get_dir($opts, 'css');
$files = $Helper->get_files($dir['path']);
$prefix = $dir['url'];

if(isset($opts['pre']))
{
$files = $Helper->reorder_files($files, $opts['pre']);
}
if(isset($opts['exclude']))
{
$files = $Helper->exclude_files($files, $opts['exclude']);
}
}


foreach($files as $file)
{
if(substr($file, strrpos($file, '.' )+1) == 'css')
{
$out[] = $this->_single_tag('link', [
'rel'=>'stylesheet',
'href'=>$prefix.$file,
'type'=>'text/css'
]);
}
}


if(isset($opts['fonts']))
{
$out[] = $this->_single_tag('link', [
'rel'=>'stylesheet',
'href'=>$opts['fonts'],
'type'=>'text/css'
]);
}



return implode("\n\t", $out)."\n";
}







public function get_javascript($opts, $index, $count)
{
if(isset($opts['dev']) && $opts['dev'] && PERCH_PRODUCTION_MODE !== "PERCH_DEVELOPMENT")
{
return false;
}

$out = array();
$prefix = '/';

if(isset($opts['files']))
{
$files = $opts['files'];
}
else
{
$Helper = new PipitPinion_Helper();
$dir = $Helper->get_dir($opts, 'js');
$files = $Helper->get_files($dir['path']);
$prefix = $dir['url'];

if(isset($opts['pre']))
{
$files = $Helper->reorder_files($files, $opts['pre']);
}
if(isset($opts['exclude']))
{
$files = $Helper->exclude_files($files, $opts['exclude']);
}
}



foreach($files as $file)
{
if(substr($file, strrpos($file, '.' )+1) == 'js')
{
if(strrpos($file, '/'))
{
$component = substr($file, strrpos($file, '/') + 1);
}
else
{
$component = $file;
}


if (!$this->component_registered($component))
{
$attrs = [];
if(isset($opts['attrs'][$file]))
{
$attrs = $opts['attrs'][$file];
}
$attrs['src'] = $prefix.$file;

$out[] = $this->_script_tag($attrs);
$this->register_component($component);
}
}
}


return implode("\n\t", $out)."\n";
}


}
?>

0 comments on commit 4bc7a0f

Please sign in to comment.