Dependencies
This commit is contained in:
5
aldarien/root/.gitignore
vendored
Normal file
5
aldarien/root/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
.buildpath
|
||||
.project
|
||||
.settings
|
||||
*.lock
|
||||
vendor
|
21
aldarien/root/LICENSE
Normal file
21
aldarien/root/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Aldarien
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
22
aldarien/root/README.md
Normal file
22
aldarien/root/README.md
Normal file
@ -0,0 +1,22 @@
|
||||
# root
|
||||
get root directory path for your project
|
||||
|
||||
## Usage
|
||||
|
||||
add `Root::root('project')` or `root('project')` or `Root::root()` or `root()` where you need the root directory of your proyect.
|
||||
|
||||
## Example
|
||||
|
||||
For the structure:
|
||||
|
||||
~~~
|
||||
/usr/share/www/projects
|
||||
- myProject
|
||||
-- src
|
||||
-- tests
|
||||
~~~
|
||||
|
||||
using `Root::root('myProject')`
|
||||
|
||||
outputs:
|
||||
`/usr/share/www/projects/myProject`
|
5
aldarien/root/app/Helper/functions.php
Normal file
5
aldarien/root/app/Helper/functions.php
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
function root(string $proyect_name = '') {
|
||||
return \Proyect\Root\Root::root($proyect_name);
|
||||
}
|
||||
?>
|
21
aldarien/root/composer.json
Normal file
21
aldarien/root/composer.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name" : "aldarien/root",
|
||||
"description" : "Find the root path for your proyect",
|
||||
"authors" : [{
|
||||
"name" : "Aldarien"
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"require-dev" : {
|
||||
"phpunit/phpunit" : "~6",
|
||||
"kint-php/kint" : "~2"
|
||||
},
|
||||
"autoload" : {
|
||||
"psr-4" : {
|
||||
"Proyect\\Root\\" : "src"
|
||||
},
|
||||
"files": [
|
||||
"app/Helper/functions.php"
|
||||
]
|
||||
}
|
||||
}
|
19
aldarien/root/phpunit.xml
Normal file
19
aldarien/root/phpunit.xml
Normal file
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.2/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
forceCoversAnnotation="true"
|
||||
beStrictAboutCoversAnnotation="true"
|
||||
beStrictAboutOutputDuringTests="true"
|
||||
beStrictAboutTodoAnnotatedTests="true"
|
||||
verbose="true">
|
||||
<testsuite>
|
||||
<directory suffix="Test.php">tests</directory>
|
||||
</testsuite>
|
||||
|
||||
<filter>
|
||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||
<directory suffix=".php">src</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
48
aldarien/root/src/Root.php
Normal file
48
aldarien/root/src/Root.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
namespace Proyect\Root;
|
||||
|
||||
class Root
|
||||
{
|
||||
/**
|
||||
* gives base path for your proyect.
|
||||
* eg. <code>$proyect_name/public/index.php</code> calls for <code>$proyect_name/bootstrap/autoload.php</code>,
|
||||
* you just need to
|
||||
*
|
||||
* <code>include root() . '/bootstrap/autoload.php'</code>
|
||||
*
|
||||
* @param string $proyect_name
|
||||
* @return string
|
||||
*/
|
||||
public static function root(string $proyect_name = '')
|
||||
{
|
||||
$dir = realpath(__DIR__);
|
||||
if ($proyect_name == '') {
|
||||
return self::findComposerFile($dir);
|
||||
} else {
|
||||
$ini = strpos($dir, $proyect_name) + strlen($proyect_name);
|
||||
}
|
||||
$path = substr($dir, $ini);
|
||||
$cnt = substr_count($path, DIRECTORY_SEPARATOR);
|
||||
$root = DIRECTORY_SEPARATOR;
|
||||
for ($i = 0; $i < $cnt; $i ++) {
|
||||
$root .= '..' . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
|
||||
return realpath($dir . $root);
|
||||
}
|
||||
protected static function findComposerFile($dir)
|
||||
{
|
||||
if (file_exists($dir . '/vendor/')) {
|
||||
return $dir;
|
||||
}
|
||||
|
||||
$root = realpath('/');
|
||||
if (realpath($dir) == $root) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$dir = dirname($dir);
|
||||
return self::findComposerFile($dir);
|
||||
}
|
||||
}
|
||||
?>
|
61
aldarien/root/tests/RootTest.php
Normal file
61
aldarien/root/tests/RootTest.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Proyect\Root\Root;
|
||||
|
||||
class RootTest extends TestCase
|
||||
{
|
||||
private $proyect_name = 'root';
|
||||
private $current_dir;
|
||||
|
||||
public function testBaseRoot()
|
||||
{
|
||||
$this->getCurrentDir();
|
||||
$this->assertEquals($this->getRoot(), $this->current_dir);
|
||||
$this->assertEquals($this->getBaseRoot(), $this->current_dir);
|
||||
}
|
||||
public function testBaseRootFunction()
|
||||
{
|
||||
$this->getCurrentDir();
|
||||
$this->assertEquals($this->getFRoot(), $this->current_dir);
|
||||
$this->assertEquals($this->getFBaseRoot(), $this->current_dir);
|
||||
}
|
||||
public function testSrcRoot()
|
||||
{
|
||||
$this->changeDir('src');
|
||||
$this->assertEquals(realpath($this->getRoot() . '/src'), $this->current_dir);
|
||||
$this->assertEquals(realpath($this->getBaseRoot() . '/src'), $this->current_dir);
|
||||
}
|
||||
public function testSrcRootFunction()
|
||||
{
|
||||
$this->changeDir('src');
|
||||
$this->assertEquals(realpath($this->getFRoot() . '/src'), $this->current_dir);
|
||||
$this->assertEquals(realpath($this->getFBaseRoot() . '/src'), $this->current_dir);
|
||||
}
|
||||
|
||||
private function getCurrentDir()
|
||||
{
|
||||
$this->current_dir = getcwd();
|
||||
}
|
||||
private function changeDir($dir)
|
||||
{
|
||||
chdir($dir);
|
||||
$this->getCurrentDir();
|
||||
}
|
||||
private function getRoot()
|
||||
{
|
||||
return Root::root($this->proyect_name);
|
||||
}
|
||||
private function getBaseRoot()
|
||||
{
|
||||
return Root::root();
|
||||
}
|
||||
private function getFRoot()
|
||||
{
|
||||
return root($this->proyect_name);
|
||||
}
|
||||
private function getFBaseRoot()
|
||||
{
|
||||
return root();
|
||||
}
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user