Skip to content

Commit 1b243f9

Browse files
author
James Wigger
committed
Added tests for path / file helpers
1 parent 9adf20c commit 1b243f9

8 files changed

Lines changed: 285 additions & 74 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Base-PHP-Helpers
1+
# BaseLayout-PHP-Helpers
22

3-
PHP Helper functions for building sites using Base 5
3+
PHP Helper functions for building sites using BaseLayout 5
44

55
## Installation
66

@@ -10,7 +10,7 @@ Add the following to your `composer.json` file:
1010
"repositories": [
1111
{
1212
"type": "git",
13-
"url": "git@github.com:RootStudio/Base-PHP-Helpers.git"
13+
"url": "git@github.com:RootStudio/BaseLayout-PHP-Helpers.git"
1414
}
1515
],
1616
"require": {
@@ -20,7 +20,7 @@ Add the following to your `composer.json` file:
2020

2121
## Features
2222

23-
This package includes PHP functions that streamline building sites using the Base 5 boilerplate.
23+
This package includes PHP functions that streamline building sites using the BaseLayout 5 boilerplate.
2424

2525
* `mix()` and `public_path()` functions for asset management
2626
* API compatibility with `perch_layout_*` functions
@@ -37,7 +37,7 @@ The mix helper should be used to call in static assets compiled with Laravel Mix
3737
<head>
3838
<meta charset="utf-8">
3939

40-
<title>Base NG</title>
40+
<title>BaseLayout NG</title>
4141

4242
<link rel="stylesheet" href="<?php echo mix('assets/css/global.css'); ?>">
4343
</head>

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@
2222
"files": [
2323
"helpers.php"
2424
]
25+
},
26+
"require-dev": {
27+
"phpunit/phpunit": "^6.3",
28+
"mockery/mockery": "^0.9.9"
2529
}
2630
}

helpers.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<?php use RootStudio\Base;
1+
<?php
2+
3+
use RootStudio\Base;
4+
use RootStudio\BaseLayout;
25

36
/**
47
* Return path to document root
@@ -10,8 +13,9 @@
1013
if (!function_exists('base_public_path')) {
1114
function base_public_path($path = '')
1215
{
13-
$basePath = realpath(__DIR__ . '/../../../');
14-
$userPath = realpath($basePath . ($path ? '/' . trim($path, '/') : $path));
16+
$Base = new Base(realpath(__DIR__ . '/../../../'));
17+
18+
$userPath = $Base->getPublicPath() . ($path ? '/' . trim($path, '/') : $path);
1519

1620
return $userPath;
1721
}
@@ -74,8 +78,8 @@ function base_asset($path, $manifestDirectory = '')
7478
if (!function_exists('base_layout')) {
7579
function base_layout($file, array $data = [], $return = false)
7680
{
77-
$Base = Base::fetch();
78-
$Base->setLayoutVars($data);
81+
$BaseLayout = BaseLayout::fetch();
82+
$BaseLayout->setLayoutVars($data);
7983

8084
$path = base_public_path('layouts/' . ltrim($file, '/') . '.php');
8185

@@ -88,11 +92,11 @@ function base_layout($file, array $data = [], $return = false)
8892
ob_start();
8993
}
9094

91-
$Base->incrementLayoutDepth();
95+
$BaseLayout->incrementLayoutDepth();
9296

9397
include $path;
9498

95-
$Base->decrementLayoutDepth();
99+
$BaseLayout->decrementLayoutDepth();
96100

97101
if ($return) {
98102
return ob_get_clean();
@@ -113,9 +117,9 @@ function base_layout($file, array $data = [], $return = false)
113117
if (!function_exists('base_layout_var')) {
114118
function base_layout_var($key, $return = false)
115119
{
116-
$Base = Base::fetch();
120+
$BaseLayout = BaseLayout::fetch();
117121

118-
$value = $Base->getLayoutVar($key);
122+
$value = $BaseLayout->getLayoutVar($key);
119123

120124
if ($return) return $value;
121125

@@ -133,9 +137,9 @@ function base_layout_var($key, $return = false)
133137
if (!function_exists('base_layout_has')) {
134138
function base_layout_has($key)
135139
{
136-
$Base = Base::fetch();
140+
$BaseLayout = BaseLayout::fetch();
137141

138-
$value = $Base->getLayoutVar($key);
142+
$value = $BaseLayout->getLayoutVar($key);
139143

140144
if ($value) return true;
141145

phpunit.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="default">
13+
<directory suffix="Test.php">./tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist processUncoveredFilesFromWhitelist="true">
18+
<directory suffix=".php">./src</directory>
19+
</whitelist>
20+
</filter>
21+
<php>
22+
</php>
23+
</phpunit>

src/Base.php

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,94 +9,65 @@
99
class Base
1010
{
1111
/**
12-
* Singleton instance
12+
* Root directory path
1313
*
14-
* @var Base;
14+
* @var string
1515
*/
16-
static protected $instance;
16+
protected $basePath;
1717

1818
/**
19-
* Set layout variables
19+
* Base constructor.
2020
*
21-
* @var array
21+
* @param null|string $basePath
2222
*/
23-
private $layoutVars = [];
24-
25-
/**
26-
* Depth of layout calls
27-
*
28-
* @var int
29-
*/
30-
private $layoutDepth = 1;
31-
32-
/**
33-
* Return instance of class
34-
*
35-
* @return Base
36-
*/
37-
public static function fetch()
23+
public function __construct($basePath = null)
3824
{
39-
if (!isset(self::$instance)) {
40-
$c = __CLASS__;
41-
self::$instance = new $c;
25+
if ($basePath) {
26+
$this->setBasePath($basePath);
4227
}
43-
44-
return self::$instance;
4528
}
4629

4730
/**
48-
* Set layout variables
31+
* Set a the root directory path
4932
*
50-
* @param array $data
51-
*/
52-
public function setLayoutVars(array $data)
53-
{
54-
if ($this->layoutDepth > 1 && is_array($data)) {
55-
$this->layoutVars = array_merge($this->layoutVars, $data);
56-
} else {
57-
$this->layoutVars = $data;
58-
}
59-
}
60-
61-
/**
62-
* Return layout variables
33+
* @param $basePath
6334
*
64-
* @return array
35+
* @return $this
6536
*/
66-
public function getLayoutVars()
37+
public function setBasePath($basePath)
6738
{
68-
return $this->layoutVars;
39+
$this->basePath = rtrim($basePath, '\/');
40+
41+
return $this;
6942
}
7043

7144
/**
72-
* Return single variable by key
45+
* Return path to root directory
7346
*
74-
* @param $key
75-
*
76-
* @return mixed|string
47+
* @return string
7748
*/
78-
public function getLayoutVar($key)
49+
public function getBasePath()
7950
{
80-
if (isset($this->layoutVars[$key])) {
81-
return $this->layoutVars[$key];
82-
}
83-
84-
return '';
51+
return $this->basePath;
8552
}
8653

8754
/**
88-
* Increase layout call depth
55+
* Return path to public directory
56+
*
57+
* @return string
8958
*/
90-
public function incrementLayoutDepth()
59+
public function getPublicPath()
9160
{
92-
$this->layoutDepth++;
61+
return $this->getBasePath() . DIRECTORY_SEPARATOR . 'public';
9362
}
9463

9564
/**
96-
* Decrease layout call depth
65+
* Return path to layout directory
66+
*
67+
* @return string
9768
*/
98-
public function decrementLayoutDepth()
69+
public function getLayoutPath()
9970
{
100-
$this->layoutDepth--;
71+
return $this->getPublicPath() . DIRECTORY_SEPARATOR . 'layouts';
10172
}
10273
}

src/BaseLayout.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php namespace RootStudio;
2+
3+
/**
4+
* Class Base
5+
*
6+
* @package RootStudio
7+
* @author James Wigger <james@rootstudio.co.uk>
8+
*/
9+
class BaseLayout
10+
{
11+
/**
12+
* Singleton instance
13+
*
14+
* @var BaseLayout;
15+
*/
16+
static protected $instance;
17+
18+
/**
19+
* Set layout variables
20+
*
21+
* @var array
22+
*/
23+
private $layoutVars = [];
24+
25+
/**
26+
* Depth of layout calls
27+
*
28+
* @var int
29+
*/
30+
private $layoutDepth = 1;
31+
32+
/**
33+
* Return instance of class
34+
*
35+
* @return BaseLayout
36+
*/
37+
public static function fetch()
38+
{
39+
if (!isset(self::$instance)) {
40+
$c = __CLASS__;
41+
self::$instance = new $c;
42+
}
43+
44+
return self::$instance;
45+
}
46+
47+
/**
48+
* Set layout variables
49+
*
50+
* @param array $data
51+
*/
52+
public function setLayoutVars(array $data)
53+
{
54+
if ($this->layoutDepth > 1 && is_array($data)) {
55+
$this->layoutVars = array_merge($this->layoutVars, $data);
56+
} else {
57+
$this->layoutVars = $data;
58+
}
59+
}
60+
61+
/**
62+
* Return layout variables
63+
*
64+
* @return array
65+
*/
66+
public function getLayoutVars()
67+
{
68+
return $this->layoutVars;
69+
}
70+
71+
/**
72+
* Return single variable by key
73+
*
74+
* @param $key
75+
*
76+
* @return mixed|string
77+
*/
78+
public function getLayoutVar($key)
79+
{
80+
if (isset($this->layoutVars[$key])) {
81+
return $this->layoutVars[$key];
82+
}
83+
84+
return '';
85+
}
86+
87+
/**
88+
* Increase layout call depth
89+
*/
90+
public function incrementLayoutDepth()
91+
{
92+
$this->layoutDepth++;
93+
}
94+
95+
/**
96+
* Decrease layout call depth
97+
*/
98+
public function decrementLayoutDepth()
99+
{
100+
$this->layoutDepth--;
101+
}
102+
}

0 commit comments

Comments
 (0)