Files
project/common/Factory/Project.php

136 lines
3.8 KiB
PHP

<?php
namespace ProVM\Projects\Common\Factory;
use Cz\Git\GitRepository;
class Project {
protected $folder;
protected $data;
public function find(string $folder): Project {
if (!file_exists($folder)) {
throw new \Exception('Proyecto no existe. ' . $folder);
}
$this->folder = realpath($folder);
$name = array_pop(explode(DIRECTORY_SEPARATOR, $this->folder));
$this->data = [
'name' => implode('/', ['provm', $name]),
'base_name' => $name,
'folder' => $this->folder
];
return $this;
}
public function build() {
return json_decode(json_encode($this->data));
}
public function getAll(): Project {
return $this
->getGit()
->getComposer()
->getConfig()
->getControllers()
->getServices()
->getRoutes()
->getViews();
}
public function getGit(): Project {
$repo = new GitRepository($this->folder);
$status = $repo->execute('status');
$this->data['git'] = $status;
return $this;
}
public function getComposer(): Project {
$filename = implode(DIRECTORY_SEPARATOR, [$this->folder, 'composer.json']);
$this->data['composer'] = json_decode(trim(file_get_contents($filename)));
return $this;
}
protected function getFolderFiles(string $name, string $folder) {
if (!file_exists($folder)) {
return $this;
}
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($folder));
$output = [];
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$dir = rtrim(str_replace([$file->getBasename(), $this->folder, "\\"], ['', '', '/'], $file->getRealPath()), '/');
if (!isset($output[$dir])) {
$output[$dir] = [];
}
$output[$dir] []= $file->getBasename();
}
ksort($output);
$this->data[$name] = $output;
}
public function getConfig(): Project {
$folder = implode(DIRECTORY_SEPARATOR, [
$this->folder,
'setup'
]);
$this->getFolderFiles('config', $folder);
return $this;
}
public function getControllers(): Project {
$folder = implode(DIRECTORY_SEPARATOR, [$this->folder, 'common', 'Controller']);
$this->getFolderFiles('controllers', $folder);
return $this;
}
public function getServices(): Project {
$folder = implode(DIRECTORY_SEPARATOR, [$this->folder, 'common', 'Service']);
$this->getFolderFiles('services', $folder);
return $this;
}
public function getRoutes(): Project {
$folder = implode(DIRECTORY_SEPARATOR, [
$this->folder,
'resources',
'routes'
]);
$output = [];
$folders = new \DirectoryIterator($folder);
foreach ($folders as $folder) {
if (!$folder->isDir() or $folder->isDot()) {
continue;
}
$output[$folder->getBasename()] = $this->getSpaceRoutes($folder->getRealPath());
}
$this->data['routes'] = $output;
return $this;
}
protected function getSpaceRoutes(string $folder) {
if (!file_exists($folder)) {
return [];
}
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($folder));
$output = [];
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$dir = rtrim(str_replace([$file->getBasename(), $this->folder, "\\"], ['', '', '/'], $file->getRealPath()), '/');
if (!isset($output[$dir])) {
$output[$dir] = [];
}
$output[$dir] []= $file->getBasename();
}
ksort($output);
return $output;
}
public function getViews(): Project {
$folder = implode(DIRECTORY_SEPARATOR, [
$this->folder,
'resources',
'views'
]);
$this->getFolderFiles('views', $folder);
/*array_walk($this->data['views'], function(&$item) {
if (is_array($item)) {
array_walk($item, function(&$i) {
$i = str_replace('.blade', '', $i);
});
}
});*/
return $this;
}
}