Skip to content

Commit

Permalink
Merge pull request #1 from picamator/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
picamator committed Mar 9, 2017
2 parents c74d922 + fdaa4c7 commit 7329e31
Show file tree
Hide file tree
Showing 44 changed files with 5,271 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .gitignore
@@ -0,0 +1,8 @@
vendor/
.idea/
nbproject/
.settings/
.buildpath
.project
*.*~
*.phar
18 changes: 18 additions & 0 deletions .travis.yml
@@ -0,0 +1,18 @@
language: php
php:
- "5.6"
- "7.0"

cache:
directories:
- vendor

before_script:
- composer install

script:
- mkdir -p build/logs
- php vendor/bin/phpunit -c tests/phpunit.xml.dist --coverage-clover build/logs/clover.xml

after_script:
- php vendor/bin/coveralls
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,6 @@
CHANGELOG
=========

1.0.0 (2017-03-09)
------------------
* Implemented first version with documentation, examples, and tests
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
@@ -0,0 +1,8 @@
CONTRIBUTING
============
If you find this project worth to use please add a star. Follow changes to see all activities.
And if you see room for improvement, proposals please feel free to create an issue or send pull request.
Here is a great [guide to start contributing](https://guides.github.com/activities/contributing-to-open-source/).

Please note that this project is released with a [Contributor Code of Conduct](http://contributor-covenant.org/version/1/4/).
By participating in this project and its community you agree to abide by those terms.
8 changes: 8 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c) 2017 Sergii Pryz

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.
175 changes: 174 additions & 1 deletion README.md
@@ -1 +1,174 @@
# ObjectManager
ObjectManager
=============

[![PHP 7 ready](http://php7ready.timesplinter.ch/picamator/ObjectManager/dev/badge.svg)](https://travis-ci.org/picamator/ObjectManager)
[![Latest Stable Version](https://poser.pugx.org/picamator/objectmanager/v/stable.svg)](https://packagist.org/packages/picamator/objectmanager)
[![License](https://poser.pugx.org/picamator/objectmanager/license.svg)](https://packagist.org/packages/picamator/objectmanager)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/fe0853b9-7842-47bc-9460-da9bc407a9b7/mini.png)](https://insight.sensiolabs.com/projects/fe0853b9-7842-47bc-9460-da9bc407a9b7)

Master
------
[![Build Status](https://travis-ci.org/picamator/ObjectManager.svg?branch=master)](https://travis-ci.org/picamator/ObjectManager)
[![Coverage Status](https://coveralls.io/repos/github/picamator/ObjectManager/badge.svg?branch=master)](https://coveralls.io/github/picamator/ObjectManager?branch=master)

Dev
---
[![Build Status](https://travis-ci.org/picamator/ObjectManager.svg?branch=dev)](https://travis-ci.org/picamator/ObjectManager)
[![Coverage Status](https://coveralls.io/repos/github/picamator/ObjectManager/badge.svg?branch=dev)](https://coveralls.io/github/picamator/ObjectManager?branch=dev)

Object Manager is one class tool to build objects supplied with a Singleton wrapper and unit test stub helper.

The main usage are:

* refactoring legacy code with unit-testable style without break backwards compatibility
* having one place for creating new instances

Installation
------------
Update to your `composer.json` with:

```json
{
"require": {
"picamator/object-manager": "~1.0"
}
}
```

Requirements
------------
* [PHP 5.6](http://php.net/manual/en/migration56.new-features.php) or [PHP 7.0](http://php.net/manual/en/migration70.new-features.php)

Examples
--------

### Legacy
Let's application has an ``UserRepository``:

```php
<?php
class UserRepository
{
private $connection;

public function __construct()
{
$this->connection = new Connection();
}
}
```

The ``Connection`` instance here was created inside constructor make it hard to [mock](https://en.wikipedia.org/wiki/Mock_object) for unit testing.

One of the solution to keep backward compatibility is using ``ObjectManagerSingleton``:

```php
<?php
class UserRepository
{
private $connection;

public function __construct()
{
$this->connection = ObjectManagerSingleton::getInstance()->create();
}
}
```

Inside unit test before running the test needs to stub ``ObjectManagerSingleton`` with mock ``ObjectManagerSingleton::setInstance($mockObjectManager)``.
Having such is open the door for mocking ``Connection`` class.

Please follow [link](docs/example/Legacy) to find example source and unit test for them.

### Factory
Let's application has a ``ConnectionFactory``:

```php
<?php
class ConnectionFactory
{
public function create()
{
return new Connection();
}
}

```

With ``ObjectManager`` it can be rewritten to:

```php
<?php
class ConnectionFactory
{
private $objectManager;

private $className;

public function __construct(ObjectManager $objectManager, $className = 'Connection')
{
$this->objectManager = $objectManager;
$this->className = $className;
}

public function create()
{
return $this->objectManager->create($this->className);
}
}

```
As a result it's possible to use Dependency Injection to override ``$className`` with new implementation.
With PHP 7 features ``ConnectionFactory`` would look like:

```php
<?php
declare(strict_types=1);

class ConnectionFactory
{
private $objectManager;

private $className;

public function __construct(ObjectManager $objectManager, string $className = 'Connection')
{
$this->objectManager = $objectManager;
$this->className = $className;
}

public function create() : ConnectionInterface
{
return $this->objectManager->create($this->className);
}
}

```

Please follow [link](docs/example/Factory) to find example source and unit test for them.

### Statistics
Suppose application needs to collect objects statistics without using any additional tools.
The solution might be ``ObjectManager`` overriding with injection in development environment.

Please follow [link](docs/example/Statistics) to find example source and unit test for them.

Documentation
-------------
* [UML class diagram](docs/uml/class.diagram.png)
* [Usage examples](docs/example)

Developing
----------
To configure developing environment please:

1. Follow [Docker installation steps](bin/docker/README.md)
2. Run inside Docker container `composer install`

Contribution
------------
To start helping the project please review [CONTRIBUTING](CONTRIBUTING.md).

License
-------
ObjectManager is licensed under the MIT License. Please see the [LICENSE](LICENSE.txt) file for details.
60 changes: 60 additions & 0 deletions bin/docker/README.md
@@ -0,0 +1,60 @@
Docker
======

Development environment has:

* object-manager-php: official [PHP 5.6 Docker](https://hub.docker.com/_/php/), [OpenSSH](https://www.openssh.com/), [Composer](https://getcomposer.org/)

Pre installation
----------------
Before start please be sure that was installed:

1. [Docker](https://docs.docker.com/engine/installation/)
2. [Compose](https://docs.docker.com/compose/install/)

Installation
------------
1. Set environment variable `HOST_IP` with your host machine IP, e.g. `export host_ip=192.168.0.104`
2. Run in application root `sudo docker-compose -f bin/docker/docker-compose.yml up`
3. Check containers `sudo docker-compose ps`

Containers
----------

### object-manager-php

#### SSH
SSH credentials:

1. user: `root`
2. password: `screencast`
3. ip: 0.0.0.0
4. port: 2240

To make connection via console simple run `ssh root@0.0.0.0 -p 2240`.

Usefull commands
----------------

* go to shell inside container `sudo docker-compose -f ./bin/docker/docker-compose.yml exec object-manager-php bash`
* build container `sudo docker-compose -f ./bin/docker/docker-compose.yml build object-manager-php`
* build container without caching `sudo docker-compose -f ./bin/docker/docker-compose.yml build --no-cache object-manager-php`

For more information please visit [Docker Compose Command-line Reference](https://docs.docker.com/compose/reference/).

Configuration IDE (PhpStorm)
----------------------------
### Remote interpreter
1. Use ssh connection to set php interpreter
2. PHP executable `/usr/local/bin/php`
3. Set "Path mappings": `host machine project root->/ObjectManager`

More information is [here](https://confluence.jetbrains.com/display/PhpStorm/Working+with+Remote+PHP+Interpreters+in+PhpStorm).

### UnitTests
1. Configure UnitTest using remote interpreter.
2. Choose "Use Composer autoload"
3. Set "Path to script": `/ObjectManager/vendor/autoload.php`
4. Set "Default configuration file": `/ObjectManager/tests/phpunit.xml.dist`

More information is [here](https://confluence.jetbrains.com/display/PhpStorm/Running+PHPUnit+tests+over+SSH+on+a+remote+server+with+PhpStorm).
12 changes: 12 additions & 0 deletions bin/docker/docker-compose.yml
@@ -0,0 +1,12 @@
version: '2'
services:
object-manager-php:
build:
context : ../..
dockerfile: bin/docker/php/Dockerfile
args:
- host_ip
volumes:
- ../../:/ObjectManager
ports:
- "2240:22"
70 changes: 70 additions & 0 deletions bin/docker/php/Dockerfile
@@ -0,0 +1,70 @@
FROM php:5.6-cli
ARG host_ip

RUN apt-get update

# git
RUN apt-get -y install git

# composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
RUN php composer-setup.php
RUN php -r "unlink('composer-setup.php');"
RUN mv composer.phar /usr/local/bin/composer

# ssh, source https://docs.docker.com/engine/examples/running_ssh_service/ with correction https://github.com/docker/docker/issues/23621#issuecomment-226575258
RUN apt-get -y install openssh-server

RUN mkdir /var/run/sshd
RUN echo 'root:screencast' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

# xdebug
RUN apt-get -y install wget

RUN mkdir -p /usr/lib/php/20151012

RUN wget -O ~/xdebug-2.4.1.tgz http://xdebug.org/files/xdebug-2.4.1.tgz
RUN tar -xvzf ~/xdebug-2.4.1.tgz
RUN rm ~/xdebug-2.4.1.tgz
RUN cd xdebug-2.4.1 && phpize
RUN cd xdebug-2.4.1 && ./configure
RUN cd xdebug-2.4.1 && make
RUN cd xdebug-2.4.1 && cp modules/xdebug.so /usr/lib/php/20151012
RUN rm -rf xdebug-2.4.1

# xdebug config cli
ADD ./bin/docker/php/config/20-xdebug.ini /usr/local/etc/php/conf.d/
RUN echo "xdebug.remote_host = $host_ip" >> /usr/local/etc/php/conf.d/20-xdebug.ini

# php
RUN apt-get -y install libcurl4-gnutls-dev
RUN docker-php-ext-install -j$(nproc) curl

RUN apt-get install -y zlib1g-dev
RUN docker-php-ext-install -j$(nproc) zip

RUN apt-get install -y libxslt-dev
RUN docker-php-ext-install -j$(nproc) xsl

# php.ini
ADD ./bin/docker/php/config/php.ini /usr/local/etc/php/

# volume
RUN mkdir /ObjectManager
VOLUME /ObjectManager

# workdir
WORKDIR /ObjectManager

# expose ports
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
4 changes: 4 additions & 0 deletions bin/docker/php/config/20-xdebug.ini
@@ -0,0 +1,4 @@
; xdebug
zend_extension = /usr/lib/php/20151012/xdebug.so
xdebug.remote_enable = 1
xdebug.remote_autostart = 1

0 comments on commit 7329e31

Please sign in to comment.