Browse files

Imported adapters from Extool main project

  • Loading branch information...
1 parent 5155e49 commit 0ac31a2df4aa019a6fbfcbcb4883227b663d6030 @jlleblanc committed Sep 23, 2010
Showing with 525 additions and 0 deletions.
  1. +22 −0 napkinsketch/LICENSE
  2. +235 −0 napkinsketch/adapter.php
  3. +22 −0 simplecsv/LICENSE
  4. +123 −0 simplecsv/adapter.php
  5. +22 −0 tabtables/LICENSE
  6. +101 −0 tabtables/adapter.php
View
22 napkinsketch/LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2010 Joseph L. LeBlanc
+
+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.
View
235 napkinsketch/adapter.php
@@ -0,0 +1,235 @@
+<?php
+/**
+ * Extool Code Generator
+ *
+ * Copyright 2010 Joseph LeBlanc
+ * See LICENSE file for licensing details.
+ *
+ */
+namespace Extool\Adapter;
+
+class NapkinSketch implements AdapterInterface
+{
+ private $resource;
+ private $raw_plan;
+ private $parsed;
+ private $views;
+ private $adminViews;
+ private $models;
+ private $adminModels;
+ private $tables;
+
+ public function decorateRepresentation(\Extool\Representation\Representation &$rep)
+ {
+ $this->parse();
+
+ if (isset($this->parsed['project'])) {
+ $rep->setName($this->parsed['project']);
+ } else {
+ throw new Exception("Plan is not in NapkinSketch format.");
+ }
+
+ $tables = $this->getTables();
+
+ foreach ($tables as $table) {
+ $rep->addTable($table);
+ }
+
+ foreach ($this->views as $view) {
+ $rep->addPublicView($view);
+ }
+
+ foreach ($this->adminViews as $view) {
+ $rep->addAdminView($view);
+ }
+
+ foreach ($this->models as $model) {
+ $rep->addPublicModel($model);
+ }
+
+ foreach ($this->adminModels as $model) {
+ $rep->addAdminModel($model);
+ }
+
+ // TODO: models, views
+ }
+
+ public function setResource($resource)
+ {
+ if(is_file($resource)) {
+ $this->resource = $resource;
+ } else {
+ throw new \Exception("File does not exist");
+ }
+ }
+
+ public function parse()
+ {
+ $this->raw_plan = file_get_contents($this->resource);
+
+ preg_match_all('/^(\w+):\s+(.+)/m', $this->raw_plan, $matches);
+
+ $properties = $this->getMatchedProperties($matches);
+
+ foreach ($properties as $key => $value) {
+ $this->parsed[$key] = $value;
+ }
+
+ $this->views = $this->parseViews();
+ $this->adminViews = $this->parseViews('admin');
+ }
+
+ private function getTables()
+ {
+ if (!isset($this->tables)) {
+ $this->tables = array();
+
+ preg_match_all('/table\s+(\w+)\s*\((.+)\)/sU', $this->raw_plan, $matches);
+
+ foreach ($matches[1] as $i => $table) {
+ $fields = trim($matches[2][$i]);
+ $fields = explode("\n", $fields);
+
+ $extool_fields = new \Extool\Representation\Fields();
+
+ foreach ($fields as $field) {
+ $title = $this->makeFieldTitle($field);
+ $type = $this->getFieldType($field);
+
+ if ($type == 'keyint') {
+ $extool_fields->addField($title, 'integer');
+ $extool_fields->setKey($title);
+ } else {
+ $extool_fields->addField($title, $type);
+ }
+ }
+
+ $table = new \Extool\Representation\Table($table);
+ $table->setFields($extool_fields);
+ $this->tables[$table->name] = $table;
+ }
+ }
+
+ return $this->tables;
+ }
+
+ private function parseViews($prefix = '')
+ {
+ $views = array();
+
+ preg_match_all('/^' . $prefix . 'view\s+(.+)\s*\((.+)\)/sUm', $this->raw_plan, $matches);
+
+ foreach ($matches[1] as $i => $view_name) {
+ preg_match_all('/(\w+):\s+(.+)/', $matches[2][$i], $fields);
+
+ $tables = $this->getMatchedProperties($fields, true);
+
+ $model = $this->makeModel($tables, $view_name, $prefix);
+
+ $merged_fields = new \Extool\Representation\Fields();
+
+ foreach ($model->tables as $table) {
+ foreach ($table->fields as $field_name => $type) {
+ $merged_fields->addField($field_name, $type);
+ }
+ }
+
+ $view = new \Extool\Representation\View($view_name);
+
+ if ($prefix == 'admin') {
+ $view->setAccess('admin');
+ }
+
+ if (preg_match('/ list$/i', $view_name)) {
+ $view->setType('list');
+ }
+
+ $view->setFields($merged_fields);
+ $views[$view_name] = $view;
+ }
+
+ return $views;
+ }
+
+ private function makeModel($view_tables, $view, $prefix = '')
+ {
+ $tables = $this->getTables();
+
+ $model = new \Extool\Representation\Model($view);
+
+ // An extremely ugly way of creating filtered fields for models
+ foreach ($view_tables as $table_name => $table) {
+ if ($table[0] = '*') {
+ $model->addTable($tables[$table_name]);
+ } else {
+ $fields = new \Extool\Representation\Fields();
+
+ foreach ($table as $field) {
+ $fields->addField($field, $tables[$table_name]->types[$field]);
+ }
+
+ $model_table = new \Extool\Representation\Table($table_name);
+ $model->addTable($model_table);
+ }
+ }
+
+ if ($prefix == 'admin') {
+ $this->adminModels[$view] = $model;
+ } else {
+ $this->models[$view] = $model;
+ }
+
+ return $model;
+ }
+
+ private function getMatchedProperties($matches, $returnArray = false)
+ {
+ $data = array();
+
+ foreach ($matches[1] as $i => $key) {
+
+ $value = $matches[2][$i];
+
+ if (stristr($value, ',')) {
+ $value = explode(',', $value);
+
+ // This removes all of the extra whitespace
+ // there should be a more succinct way of writing this :(
+ foreach ($value as $i => $val) {
+ $value[$i] = trim($val);
+ }
+ } else if ($returnArray) {
+ $value = array($value);
+ }
+
+ $data[$key] = $value;
+ }
+
+ return $data;
+ }
+
+ private function makeFieldTitle($field)
+ {
+ return preg_replace('/ @(.*)/', '', $field);
+ }
+
+ private function getFieldType($field)
+ {
+ // default: use plain text field
+ $type = 'text';
+
+ if ($field == 'id') {
+ $type = 'keyint';
+ } else if (preg_match('/(_id|_num|@int)$/', $field) || $field == 'ordering') {
+ $type = 'integer';
+ } else if (preg_match('/(_date|@date)$/', $field)) {
+ $type = 'date';
+ } else if (preg_match('/(_text|@text)$/', $field)) {
+ $type = 'long text';
+ } else if ($field == 'published') {
+ $type = 'integer';
+ }
+
+ return $type;
+ }
+}
View
22 simplecsv/LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2010 Joseph L. LeBlanc
+
+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.
View
123 simplecsv/adapter.php
@@ -0,0 +1,123 @@
+<?php
+/**
+ * Extool Code Generator
+ *
+ * Copyright 2010 Joseph LeBlanc
+ * See LICENSE file for licensing details.
+ *
+ */
+namespace Extool\Adapter;
+
+class SimpleCSV implements AdapterInterface
+{
+ private $resource;
+ private $headers = array();
+ private $raw_data = array();
+ private $keyed_data = array();
+ private $data;
+
+ public function decorateRepresentation(\Extool\Representation\Representation &$rep)
+ {
+ $this->parse();
+
+ $pieces = explode('/', $this->resource);
+ $resource_name = ucfirst(preg_replace('/\..*/', '', array_pop($pieces)));
+ $rep->setName($resource_name);
+
+ $fields = $this->determineFields();
+ $data = new \Extool\Representation\Data($fields);
+ $data->setData($this->keyed_data);
+
+ $rep->addData($data, $resource_name);
+
+ $table = new \Extool\Representation\Table($resource_name);
+ $table->setFields($fields);
+ $rep->addTable($table);
+
+ $model = new \Extool\Representation\Model($resource_name);
+ $model->setCriteria($fields);
+ $model->addTable($table);
+
+ $view = new \Extool\Representation\View($resource_name);
+ $view->setFields($fields);
+ $rep->addPublicView($view);
+ $rep->addPublicModel($model);
+
+ $view->setAccess('admin');
+ $rep->addAdminView($view);
+ $rep->addAdminModel($model);
+
+ // Create new model for the list views
+ $model = new \Extool\Representation\Model($resource_name . ' List');
+ $model->setCriteria($fields);
+ $model->addTable($table);
+
+ $view = new \Extool\Representation\View($resource_name . ' List');
+ $view->setType('list');
+ $view->setFields($fields);
+ $rep->addPublicView($view);
+ $rep->addPublicModel($model);
+
+ $view->setAccess('admin');
+ $rep->addAdminView($view);
+ $rep->addAdminModel($model);
+ }
+
+ public function setResource($resource)
+ {
+ if(is_file($resource)) {
+ $this->resource = $resource;
+ } else {
+ throw new \Exception("File does not exist");
+ }
+ }
+
+ public function parse()
+ {
+ if (isset($this->resource)) {
+ $file = fopen($this->resource, 'r');
+
+ $this->headers = fgetcsv($file);
+
+ while ($line = fgetcsv($file)) {
+ $this->raw_data[] = $line;
+ $this->keyed_data[] = $this->keyedRow($line);
+ }
+
+ fclose($file);
+ }
+ }
+
+ private function keyedRow($row)
+ {
+ $keyed = array();
+
+ foreach ($this->headers as $index => $header) {
+ $keyed[$header] = $row[$index];
+ }
+
+ return $keyed;
+ }
+
+ private function determineFields()
+ {
+ $types = new \Extool\Defines\Types();
+ $fields = new \Extool\Representation\Fields();
+
+ for ($i=0, $column_count = count($this->headers); $i < $column_count; $i++) {
+
+ $column_data = array();
+
+ // going into undefined offsets here, I think my array math is off
+ for ($j=0, $row_count=count($this->raw_data); $j < $row_count; $j++) {
+ $column_data[] = $this->raw_data[$j][$i];
+ }
+
+ $header = $this->headers[$i];
+
+ $fields->addField($header, $types->determineTypeArray($column_data));
+ }
+
+ return $fields;
+ }
+}
View
22 tabtables/LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2010 Joseph L. LeBlanc
+
+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.
View
101 tabtables/adapter.php
@@ -0,0 +1,101 @@
+<?php
+/**
+ * Extool Code Generator
+ *
+ * Copyright 2010 Joseph LeBlanc
+ * See LICENSE file for licensing details.
+ *
+ */
+namespace Extool\Adapter;
+
+class TabTables implements AdapterInterface
+{
+ private $resource;
+ private $table_fields = array();
+
+ public function decorateRepresentation(\Extool\Representation\Representation &$rep)
+ {
+ $this->parse();
+
+ $pieces = explode('/', $this->resource);
+ $rep->setName(ucfirst(array_pop($pieces)));
+
+ foreach ($this->table_fields as $table_name => $fields) {
+ $table = new \Extool\Representation\Table($table_name);
+ $table->setFields($fields);
+ $rep->addTable($table);
+
+ $model = new \Extool\Representation\Model($table_name);
+ $model->setCriteria($fields);
+ $model->addTable($table);
+
+ // Generate four views for each table
+ $view = new \Extool\Representation\View($table_name);
+ $view->setFields($fields);
+ $rep->addPublicView($view);
+ $rep->addPublicModel($model);
+
+ $view->setAccess('admin');
+ $rep->addAdminView($view);
+ $rep->addAdminModel($model);
+
+ // Create new model for the list views
+ $model = new \Extool\Representation\Model($table_name . ' List');
+ $model->setCriteria($fields);
+ $model->addTable($table);
+
+ $view = new \Extool\Representation\View($table_name . ' List');
+ $view->setType('list');
+ $view->setFields($fields);
+ $rep->addPublicView($view);
+ $rep->addPublicModel($model);
+
+ $view->setAccess('admin');
+ $rep->addAdminView($view);
+ $rep->addAdminModel($model);
+ }
+ }
+
+ public function setResource($resource)
+ {
+ if(is_file($resource)) {
+ $this->resource = $resource;
+ } else {
+ throw new \Exception("File does not exist");
+ }
+
+ }
+
+ public function parse()
+ {
+ if (isset($this->resource)) {
+ $contents = file_get_contents($this->resource);
+ $this->setTablesFromString($contents);
+ } else {
+ throw new \Exception("Resource file not set");
+ }
+ }
+
+ private function setTablesFromString($tables_file)
+ {
+ $lines = explode("\n", $tables_file);
+
+ $current_table = '';
+ foreach ($lines as $line) {
+ if (substr_count($line, "\t")) {
+ $field_name = trim($line);
+
+ $type = 'text';
+
+ if (preg_match('/[_ ]id$/Ui', $field_name)) {
+ $type = 'integer';
+ }
+
+ $this->table_fields[$current_table]->addField($field_name, $type);
+ } else if ($line != "") {
+ $current_table = trim($line);
+ $this->table_fields[$current_table] = new \Extool\Representation\Fields();
+ }
+ }
+ }
+}

0 comments on commit 0ac31a2

Please sign in to comment.