Skip to content

Commit 905acc1

Browse files
committed
add utils classes
1 parent 80c98c5 commit 905acc1

File tree

4 files changed

+331
-0
lines changed

4 files changed

+331
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
namespace Ubiquity\devtools\utils;
3+
4+
class FileUtils {
5+
public static function deleteAllFilesFromFolder($folder){
6+
$files = glob($folder.'/*');
7+
foreach($files as $file){
8+
if(is_file($file))
9+
unlink($file);
10+
}
11+
}
12+
13+
public static function openFile($filename){
14+
if(file_exists($filename)){
15+
return file_get_contents($filename);
16+
}
17+
return false;
18+
}
19+
20+
public static function writeFile($filename,$data){
21+
return file_put_contents($filename,$data);
22+
}
23+
24+
public static function xcopy($source, $dest, $permissions = 0755){
25+
$path = pathinfo($dest);
26+
if (!file_exists($path['dirname'])) {
27+
mkdir($path['dirname'], 0777, true);
28+
}
29+
if (is_link($source)) {
30+
return symlink(readlink($source), $dest);
31+
}
32+
if (is_file($source)) {
33+
return copy($source, $dest);
34+
}
35+
$dir = dir($source);
36+
while (false !== $entry = $dir->read()) {
37+
if ($entry == '.' || $entry == '..') {
38+
continue;
39+
}
40+
self::xcopy("$source/$entry", "$dest/$entry", $permissions);
41+
}
42+
$dir->close();
43+
return true;
44+
}
45+
46+
public static function delTree($dir) {
47+
$files = array_diff(scandir($dir), array('.','..'));
48+
foreach ($files as $file) {
49+
(is_dir("$dir/$file")) ? self::delTree("$dir/$file") : unlink("$dir/$file");
50+
}
51+
return rmdir($dir);
52+
}
53+
54+
public static function safeMkdir($dir){
55+
if(!is_dir($dir))
56+
return mkdir($dir,0777,true);
57+
}
58+
59+
public static function cleanPathname($path){
60+
if($path!==null & $path!==""){
61+
if(DS==="/")
62+
$path=\str_replace("\\", DS, $path);
63+
else
64+
$path=\str_replace("/", DS, $path);
65+
$path=\str_replace(DS.DS, DS, $path);
66+
if(!substr($path, -1)=== DS){
67+
$path=$path.DS;
68+
}
69+
}
70+
return $path;
71+
}
72+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Ubiquity\devtools\utils\arrays;
4+
5+
use Ubiquity\utils\base\UIntrospection;
6+
7+
abstract class BaseArray {
8+
protected $messages=[];
9+
10+
public function hasMessages(){
11+
return sizeof($this->messages)>0;
12+
}
13+
14+
/**
15+
* @return multitype:
16+
*/
17+
public function getMessages() {
18+
return $this->messages;
19+
}
20+
21+
protected function parseArray($value,$iFields=null){
22+
$result=[];
23+
foreach ($value as $k=>$v){
24+
if(is_int($k) || !is_array($iFields) || array_search($k, $iFields)!==false){
25+
$prefix="";
26+
if(!is_int($k)){
27+
$prefix=$k." : ";
28+
}
29+
if(is_array($v)){
30+
$v=$this->parseInlineArray($v);
31+
}elseif($v instanceof \stdClass){
32+
$v=$this->parseInlineArray((array)$v);
33+
}elseif($v instanceof \Closure){
34+
$v=UIntrospection::closure_dump($v);
35+
}elseif(is_object($v)){
36+
$v='{o}';
37+
}else{
38+
$v=var_export($v,true);
39+
}
40+
$result[]=" · ".$prefix.$v;
41+
}
42+
43+
}
44+
return implode("\n",$result);
45+
}
46+
47+
protected function parseInlineArray($value){
48+
$result=[];
49+
foreach ($value as $k=>$v){
50+
$prefix="";
51+
if(!is_int($k)){
52+
$prefix=$k.": ";
53+
}
54+
if(is_array($v)){
55+
$v=$this->parseInlineArray($v);
56+
}elseif($v instanceof \stdClass){
57+
$v=$this->parseInlineArray((array)$v);
58+
}elseif($v instanceof \Closure){
59+
$v=UIntrospection::closure_dump($v);
60+
}elseif(is_object($v)){
61+
$v='{.}';
62+
}else{
63+
$v=var_export($v,true);
64+
}
65+
$result[]=$prefix.$v;
66+
}
67+
return '['.implode(",",$result).']';
68+
}
69+
70+
}
71+
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Ubiquity\devtools\utils\arrays;
4+
5+
use Ubiquity\utils\base\UArray;
6+
use Ubiquity\utils\base\UString;
7+
8+
class ClassicArray extends BaseArray{
9+
private $fields;
10+
private $iFields;
11+
private $datas;
12+
13+
public function __construct($datas,$part=null){
14+
if(!is_array($datas)){
15+
$datas=[$part=>$datas];
16+
}
17+
if(sizeof($datas)==1 && is_array(current($datas))){
18+
$datas=current($datas);
19+
}
20+
$this->datas=$datas;
21+
}
22+
23+
public function parse($reverse=true){
24+
if($reverse){
25+
return $this->parseReverse_();
26+
}
27+
return $this->parse_();
28+
}
29+
30+
private function getFields(){
31+
if(is_array($this->fields)){
32+
$fields=$this->fields;
33+
}else{
34+
$fields=array_keys($this->datas);
35+
}
36+
return $fields;
37+
}
38+
39+
private function parseReverse_(){
40+
if(sizeof($this->datas)==0){
41+
return [['Nothing to display']];
42+
}
43+
if(UArray::isAssociative($this->datas)){
44+
$array=[['field','value']];
45+
$fields=$this->getFields();
46+
foreach ($fields as $field){
47+
if(isset($this->datas[$field]))
48+
$array[]=[$field,$this->parseValue($this->datas[$field])];
49+
}
50+
}else{
51+
$array=[];
52+
53+
foreach ($this->datas as $data){
54+
$array[]=[$this->parseValue($data)];
55+
}
56+
57+
}
58+
return $array;
59+
}
60+
61+
private function parse_(){
62+
$fields=$this->getFields();
63+
$array=[$fields];
64+
$result=array_intersect_key($this->datas, array_flip($fields));
65+
array_walk($result, function(&$item){$item=$this->parseValue($item);});
66+
$array[]=$result;
67+
return $array;
68+
}
69+
70+
protected function parseValue($value) {
71+
if(is_array($value)){
72+
return $this->parseArray($value,$this->iFields);
73+
}elseif(UString::isValid($value)){
74+
return var_export($value,true);
75+
}
76+
return '{.}';
77+
}
78+
79+
/**
80+
* @return mixed
81+
*/
82+
public function getDatas() {
83+
return $this->datas;
84+
}
85+
86+
/**
87+
* @param array $fields
88+
*/
89+
public function setFields($fields) {
90+
$this->fields = $fields;
91+
}
92+
93+
/**
94+
* @param mixed $datas
95+
*/
96+
public function setDatas($datas) {
97+
$this->datas = $datas;
98+
}
99+
/**
100+
* @param mixed $iFields
101+
*/
102+
public function setIFields($iFields) {
103+
$this->iFields = $iFields;
104+
}
105+
106+
107+
}
108+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
namespace Ubiquity\devtools\utils\arrays;
3+
use Ubiquity\utils\base\UString;
4+
5+
class ReflectArray extends BaseArray{
6+
private $objects;
7+
private $properties;
8+
9+
public function parse(){
10+
$object=current($this->objects);
11+
if(!isset($object) || !$object){
12+
return [['Nothing to display']];
13+
}
14+
if(!is_array($this->properties)){
15+
$this->properties=$this->getProperties($object);
16+
}
17+
$result=[$this->properties];
18+
$r=new \ReflectionClass($object);
19+
20+
foreach ($this->objects as $object){
21+
if(is_array($object)){
22+
$result[]=$object;
23+
}elseif(is_object($object)){
24+
$row=[];
25+
foreach ($this->properties as $prop){
26+
if($r->hasProperty($prop)){
27+
$property=$r->getProperty($prop);
28+
$property->setAccessible(true);
29+
$row[]=$this->parseValue($property->getValue($object));
30+
}else{
31+
$this->messages[$prop]="Property {$prop} does not exists!";
32+
$row[]='';
33+
}
34+
}
35+
$result[]=$row;
36+
}
37+
}
38+
return $result;
39+
}
40+
41+
private function parseValue($value){
42+
$result="-";
43+
if(is_array($value)){
44+
return $this->parseArray($value);
45+
}elseif(UString::isValid($value)){
46+
$result=var_export($value,true);
47+
}else{
48+
$result=$value;
49+
}
50+
return $result;
51+
}
52+
53+
private function getProperties($object){
54+
$result=[];
55+
if(is_array($object)){
56+
return array_keys($object);
57+
}
58+
$reflect=new \ReflectionClass($object);
59+
$properties=$reflect->getProperties();
60+
foreach ($properties as $prop){
61+
$result[]=$prop->getName();
62+
}
63+
return $result;
64+
}
65+
66+
/**
67+
* @param mixed $objects
68+
*/
69+
public function setObjects($objects) {
70+
$this->objects = $objects;
71+
}
72+
73+
/**
74+
* @param multitype:NULL $properties
75+
*/
76+
public function setProperties($properties) {
77+
$this->properties = $properties;
78+
}
79+
80+
}

0 commit comments

Comments
 (0)