-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement file handling and the PhpHandler
- Loading branch information
Showing
21 changed files
with
347 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,56 @@ | ||
<?php namespace Tatter\Schemas\Handlers; | ||
|
||
use CodeIgniter\Config\BaseConfig; | ||
use CodeIgniter\Files\Exceptions\FileNotFoundException; | ||
use Tatter\Schemas\Exceptions\SchemasException; | ||
use Tatter\Schemas\Structures\Schema; | ||
|
||
class FileHandler extends BaseHandler | ||
{ | ||
/** | ||
* The file path. | ||
* | ||
* @var string | ||
*/ | ||
protected $path; | ||
|
||
// Initiate library | ||
public function __construct(BaseConfig $config = null, $path = null) | ||
{ | ||
parent::__construct($config); | ||
|
||
// Guess at a default file location | ||
if (is_null($file)) | ||
{ | ||
$this->file = WRITEPATH . 'uploads/schema.xml'; | ||
} | ||
// Save injected file path | ||
else | ||
{ | ||
$this->path = $path; | ||
} | ||
|
||
} | ||
|
||
// Change the path | ||
public function setPath(string $path) | ||
public function __construct(BaseConfig $config = null) | ||
{ | ||
$this->path = $path; | ||
return $this; | ||
} | ||
|
||
// Get the path | ||
public function getPath() | ||
{ | ||
return $this->path; | ||
parent::__construct($config); | ||
} | ||
|
||
// Validate the current file and get its contents | ||
public function getContents(): ?string | ||
// Scan the schemas directory and process any files found | ||
public function get(): ?Schema | ||
{ | ||
if (! is_file($this->path)) | ||
helper('filesystem'); | ||
$files = get_filenames($this->config->schemasDirectory, true); | ||
|
||
if (empty($files)) | ||
{ | ||
if ($this->config->silent) | ||
{ | ||
$this->errors[] = lang('Files.fileNotFound', [$this->path]); | ||
return null; | ||
} | ||
else | ||
{ | ||
throw FileNotFoundException::forFileNotFound($this->path); | ||
} | ||
$this->errors[] = lang('Schemas.emptySchemaDirectory', [$this->config->schemasDirectory]); | ||
return null; | ||
} | ||
|
||
return file_get_contents($this->path); | ||
} | ||
|
||
// Validate the target file and write to it | ||
public function putContents(string $data): bool | ||
{ | ||
if (! is_file($this->path)) | ||
// Try each file | ||
foreach ($files as $path) | ||
{ | ||
// WIP - should try to create the file | ||
if ($this->config->silent) | ||
// Make sure there is a handler for this extension | ||
$handler = $this->getHandlerForFile($path); | ||
if (is_null($handler)) | ||
{ | ||
$this->errors[] = lang('Schemas.unsupportedHandler', [pathinfo($path, PATHINFO_EXTENSION)]); | ||
continue; | ||
} | ||
|
||
if (empty($schema)) | ||
{ | ||
$this->errors[] = lang('Files.fileNotFound', [$this->path]); | ||
return null; | ||
$schema = $handler->get(); | ||
} | ||
else | ||
{ | ||
throw FileNotFoundException::forFileNotFound($this->path); | ||
$schema->merge($handler->get()); | ||
} | ||
} | ||
|
||
return (bool)file_put_contents($this->path, $data); | ||
return $schema ?? null; | ||
} | ||
|
||
public function save(Schema $schema): bool | ||
{ | ||
$this->methodNotImplemented(__CLASS__, 'save'); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php namespace Tatter\Schemas\Handlers; | ||
|
||
use CodeIgniter\Config\BaseConfig; | ||
use Tatter\Schemas\Exceptions\SchemasException; | ||
use Tatter\Schemas\Interfaces\SchemaHandlerInterface; | ||
use Tatter\Schemas\Structures\Schema; | ||
use Tatter\Schemas\Structures\Relation; | ||
use Tatter\Schemas\Structures\Table; | ||
use Tatter\Schemas\Structures\Field; | ||
use Tatter\Schemas\Structures\Index; | ||
use Tatter\Schemas\Structures\ForeignKey; | ||
|
||
class PhpHandler extends BaseHandler implements SchemaHandlerInterface | ||
{ | ||
/** | ||
* The file path. | ||
* | ||
* @var string | ||
*/ | ||
protected $path; | ||
|
||
// Initiate library | ||
public function __construct(BaseConfig $config = null, $path = null) | ||
{ | ||
parent::__construct($config); | ||
|
||
// Save the path | ||
$this->path = $path; | ||
} | ||
|
||
// Read in data from the file and fit it into a schema | ||
public function get(): ?Schema | ||
{ | ||
$contents = $this->getContents($this->path); | ||
if (is_null($contents)) | ||
{ | ||
$this->errors[] = lang('Schemas.emptySchemaFile', [$this->path]); | ||
return null; | ||
} | ||
|
||
// PHP files should contain pre-built schemas in the $schema variable | ||
// So the path just needs to be included and the variable checked | ||
try { | ||
require $this->path; | ||
} catch (\Exception $e) { | ||
$this->errors[] = $e->getMessage(); | ||
return null; | ||
} | ||
|
||
return $schema ?? null; | ||
} | ||
|
||
// Write out the schema to file | ||
public function save(Schema $schema): bool | ||
{ | ||
$this->methodNotImplemented(__CLASS__, 'save'); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.