Browse Source

Added classes

master
anghenfil 3 years ago
parent
commit
0cd8588967
  1. 29
      README.md
  2. 47
      src/File.php
  3. 54
      src/Folder.php
  4. 17
      src/InvalidProjectException.php
  5. 37
      src/Navigation.php
  6. 56
      src/SimpleCodeViewer.php
  7. 33
      tests/project-1/.metainfo
  8. 2
      tests/project-1/1.file

29
README.md

@ -1,3 +1,30 @@
# SimpleCodeViewer
Simple html-javascript-php codeviewer developed for ShitHub
SimpleCodeViewer provides an small, project-based Code viewer and editor.
## Notes
- Files must belong to a project.
- All meta informations get stored in a database
## Datastruture
- MongoDB Support planned
- files are normal files on harddisk
- Metadata (Project structure) get saved in .metadata file in xml format
**Metadata to save (per file):**
- fileID
- filename
- code language
**Metadata to save (per project):**
- project structure (as on disk)
- authorname & id
- tags
- main code language
- date
### TODO
- Cache function (delete cached navigation if navigation updates)

47
src/File.php

@ -0,0 +1,47 @@
<?php
/**
* Created by PhpStorm.
* User: anghenfil
* Date: 03.08.18
* Time: 02:41
*/
namespace ShitHub\SimpleCodeViewer;
class File{
private $id;
private $name;
private $language;
public function __construct($id, $name, $language){
$this->id = $id;
$this->name = $name;
$this->language = $language;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @return mixed
*/
public function getLanguage()
{
return $this->language;
}
}

54
src/Folder.php

@ -0,0 +1,54 @@
<?php
/**
* Created by PhpStorm.
* User: anghenfil
* Date: 03.08.18
* Time: 02:41
*/
namespace ShitHub\SimpleCodeViewer;
class Folder{
private $id;
private $name;
private $childrens;
public function __construct($id, $name){
$this->id = $id;
$this->name = $name;
$this->childrens = array();
}
public function addChildren($children){
if($children instanceof File || $children instanceof Folder){
array_push($this->childrens, $children);
}else{ //Wrong type
throw new \InvalidArgumentException("Expected object of type 'Folder' or 'File'.");
}
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @return array
*/
public function getChildrens()
{
return $this->childrens;
}
}

17
src/InvalidProjectException.php

@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: anghenfil
* Date: 03.08.18
* Time: 01:55
*/
namespace ShitHub\SimpleCodeViewer;
class InvalidProjectException extends \LogicException{
public function __construct($message, $code = 0, \Exception $previous = null) {
parent::__construct($message, $code, $previous);
}
}

37
src/Navigation.php

@ -0,0 +1,37 @@
<?php
/**
* Generates navigation for CodeViewer
* User: anghenfil
* Date: 03.08.18
* Time: 01:38
*/
namespace ShitHub\SimpleCodeViewer;
class Navigation{
private $base_directory;
private $project;
private $metapath = "";
public function generateNavigation($base_directory, $project){
$this->base_directory = $base_directory;
$this->project = $project;
$this->metapath = $this->base_directory.'project-'.$this->project.'/.metainfo';
}
private function loadMetaFile(){ //TODO: Mode to SimpleCodeViewer class
if(file_exists($this->base_directory.'project-'.$this->project.'/.metainfo')){ //Metafile of project exists?
$xml = simplexml_load_file($this->metapath);
if($xml == false){
throw new InvalidProjectException("Can't parse Metainfo or project ".$this->project);
}else{//TODO get general project infos
foreach($xml->Project->children() as $key){
}
}
}else{
throw new InvalidProjectException("Metainfo for project ".$this->project." missing.");
}
}
}

56
src/SimpleCodeViewer.php

@ -0,0 +1,56 @@
<?php
/**
* Main class for CodeViewer
* User: anghenfil
* Date: 02.08.18
* Time: 21:10
*/
namespace ShitHub\SimpleCodeViewer;
class SimpleCodeViewer{
const BASE_DIRECTORY = '../tests/';
/**
* CodeViewer constructor.
* @param integer $project project id
* @param integer null $file file id of active file
* @param boolean false $editmode view or edit mode; false = read mode true = write mode
*/
public function __construct($project, $file = null, $editmode = false){
if (self::checkProject($project)) {
$this->projectID = $project;
$this->navigation = new Navigation($project);
$this->snippetview = new SnippetView();
//validation and so
} else {
throw new \InvalidArgumentException("Valid projectID as first parameter (int) required");
}
}
static public function checkFile($file, $project){
if (!is_null($file)) {
if (is_int($file) && file_exists(SimpleCodeViewer::BASE_DIRECTORY.'project-'.$project.'/'.$file.'.file')) { //Check if fileID is int
return true;
} else {
return false;
}
}else{
return false;
}
}
static public function checkProject($project){
if(!is_null($project) && is_int($project)) { //Check projectID
if (file_exists(SimpleCodeViewer::BASE_DIRECTORY.'project-'.$project)) {
return true;
}else{
return false;
}
}else{
return false;
}
}
}

33
tests/project-1/.metainfo

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE SimpleCodeViewerMeta>
<SimpleCodeViewerMeta version="0.1" xmlns="https://def.anghenfil.de/SimpleCodeViewerMeta">
<Project>
<ID>1</ID>
<Author>
<ID>1</ID>
<Name>anghenfil</Name>
</Author>
<Language>php</Language>
<Date>02.08.2018</Date>
<Files>
<Folder>
<ID>1</ID>
<Name>src</Name>
<Folder>
<ID>2</ID>
<Name>src-subfolder</Name>
<File>
<ID>3</ID>
<Name>index.php</Name>
<Language>php</Language>
</File>
</Folder>
</Folder>
<File>
<ID>3</ID>
<Name>index.php</Name>
<Language>php</Language>
</File>
</Files>
</Project>
</SimpleCodeViewerMeta>

2
tests/project-1/1.file

@ -0,0 +1,2 @@
<?php
print("example index file");
Loading…
Cancel
Save