From 3177c0749343da16f229e2f347f5b25161233668 Mon Sep 17 00:00:00 2001 From: Romans Malinovskis Date: Mon, 4 Jul 2016 21:28:53 +0100 Subject: [PATCH 1/2] Implement FactoryTrait --- src/ContainerTrait.php | 2 +- src/FactoryTrait.php | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/ContainerTrait.php b/src/ContainerTrait.php index e9fe2ff5..ae19ef41 100644 --- a/src/ContainerTrait.php +++ b/src/ContainerTrait.php @@ -49,7 +49,7 @@ function add($obj, $args = []) { if (isset($this->_factoryTrait)) { // Factory allows us to pass string-type objects - $obj = $this->_add_Factory($obj, $args); + $obj = $this->factory($obj, $args); } $obj = $this->_add_Container($obj, $args); diff --git a/src/FactoryTrait.php b/src/FactoryTrait.php index 30f7901e..3abc3599 100644 --- a/src/FactoryTrait.php +++ b/src/FactoryTrait.php @@ -3,10 +3,48 @@ trait FactoryTrait { + public $_factoryTrait = true; + /** * Determine class name, call constructor */ - function factory($object, $defaults) { + function factory($object, $defaults = []) { + if (is_object($object)) { + return $object; + } + return new $object($defaults); + } + + /** + * First normalize class name, then add specified prefix to + * class name if it's passed and not already added. + * Class name can have namespaces and they are treated prefectly. + * + * If object is passed as $name parameter, then same object is returned. + * + * Example: normalizeClassName('User','Model') == 'Model_User'; + * + * @param string|object $name Name of class or object + * @param string $prefix Optional prefix for class name + * + * @return string|object Full, normalized class name or received object + */ + public function normalizeClassName($name, $prefix = null) + { + if (!is_string($name)) { + return $name; + } + + $name = str_replace('/', '\\', $name); + if ($prefix !== null) { + $class = ltrim(strrchr($name, '\\'), '\\') ?: $name; + $prefix = ucfirst($prefix); + if (strpos($class, $prefix) !== 0) { + $name = preg_replace('|^(.*\\\)?(.*)$|', '\1'.$prefix.'_\2', $name); + } + } + + return $name; } } From 835026eae093ee965f2adb4c22286fdb89c83e53 Mon Sep 17 00:00:00 2001 From: Romans Malinovskis Date: Mon, 4 Jul 2016 21:32:50 +0100 Subject: [PATCH 2/2] update readme --- README.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 14b3790f..832328b0 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,10 @@ **Agile Core is a collection of PHP Traits for designing object-oriented frameworks** -[![Join the chat at https://gitter.im/atk4/data](https://badges.gitter.im/atk4/data.svg)](https://gitter.im/atk4/data?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![Gitter](https://img.shields.io/gitter/room/atk4/data.svg?maxAge=2592000)](https://gitter.im/atk4/dataset?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![Documentation Status](https://readthedocs.org/projects/agile-core/badge/?version=latest)](http://agile-core.readthedocs.io/en/latest/?badge=latest) +[![License](https://poser.pugx.org/atk4/core/license)](https://packagist.org/packages/atk4/core) +[![GitHub release](https://img.shields.io/github/release/atk4/core.svg?maxAge=2592000)](https://packagist.org/packages/atk4/core) [![Build Status](https://travis-ci.org/atk4/core.png?branch=develop)](https://travis-ci.org/atk4/core) [![Code Climate](https://codeclimate.com/github/atk4/core/badges/gpa.svg)](https://codeclimate.com/github/atk4/core) [![Test Coverage](https://codeclimate.com/github/atk4/core/badges/coverage.svg)](https://codeclimate.com/github/atk4/core/coverage) @@ -30,20 +33,20 @@ See http://agile-core.readthedocs.io/ ## Current Status -Stable (BETA1) +Stable ## Roadmap ``` 1.1 Implement Debug -1.2 Implement Factory -1.3 Implement QuickException -1.4 Implement Sessions -1.5 Implement Renderable and Template +1.2 Implement QuickException +1.3 Implement Sessions +1.4 Implement Renderable and Template ``` ## Past Updates +* 04 Jul: Implemented FactoryTrait * 21 May: Released 1.0: Implemented ContainerTrait, Trackable, Initializer, AppScope, Hooks, DynamicMethod * 11 May: Released 0.1 * 11 May: Finished basic docs