<commit_before><?php

namespace ZF\ApiFirstAdmin;

use Zend\Mvc\MvcEvent;
use ZF\Hal\Link\Link;
use ZF\Hal\Link\LinkCollection;
use ZF\Hal\Resource;
use ZF\Hal\View\HalJsonModel;

class Module
{
    /**
     * @var \Zend\ServiceManager\ServiceLocatorInterface
     */
    protected $sm;

    public function onBootstrap(MvcEvent $e)
    {
        $app      = $e->getApplication();
        $this->sm = $app->getServiceManager();
        $events   = $app->getEventManager();
        $events->attach('render', array($this, 'onRender'), 100);
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__,
                ),
            ),
        );
    }

    public function getDiagnostics()
    {
        return array(
            'Config File Writable' => function () {
                if (!defined('APPLICATION_PATH')) {
                    return false;
                }
                if (!is_writable(APPLICATION_PATH . '/config/autoload/development.php')) {
                    return false;
                }
                return true;
            },
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/../../../config/module.config.php';
    }

    public function getServiceConfig()
    {
        return array('factories' => array(
            'ZF\ApiFirstAdmin\Model\ModuleModel' => function ($services) {
                if (!$services->has('ModuleManager')) {
                    throw new ServiceNotCreatedException(
                        'Cannot create ZF\ApiFirstAdmin\Model\ModuleModel service because ModuleManager service is not present'
                    );
                }
                $modules    = $services->get('ModuleManager');
                $restConfig = array();
                $rpcConfig  = array();
                if ($services->has('Config')) {
                    $config = $services->get('Config');
                    if (isset($config['zf-rest'])) {
                        $restConfig = $config['zf-rest'];
                    }
                    if (isset($config['zf-rpc'])) {
                        $rpcConfig = $config['zf-rpc'];
                    }
                }
                return new Model\ModuleModel($modules, $restConfig, $rpcConfig);
            },
            'ZF\ApiFirstAdmin\Model\ModuleResource' => function ($services) {
                $moduleModel = $services->get('ZF\ApiFirstAdmin\Model\ModuleModel');
                $listener    = new Model\ModuleResource($moduleModel);

                if ($services->has('Config')) {
                    $config = $services->get('Config');
                    if (isset($config['zf-api-first-admin'])) {
                        if (isset($config['zf-api-first-admin']['module_path'])) {
                            $listener->setModulePath($config['zf-api-first-admin']['module_path']);
                        }
                    }
                }
                return $listener;
            },
            'ZF\ApiFirstAdmin\Model\RestEndpointModelFactory' => function ($services) {
                if (!$services->has('ZF\Configuration\ModuleUtils')
                    || !$services->has('ZF\Configuration\ConfigResourceFactory')
                ) {
                    throw new ServiceNotCreatedException(
                        'ZF\ApiFirstAdmin\Model\RestEndpointModelFactory is missing one or more dependencies from ZF\Configuration'
                    );
                }
                $moduleUtils   = $services->get('ZF\Configuration\ModuleUtils');
                $configFactory = $services->get('ZF\Configuration\ConfigResourceFactory');
                return new Model\RestEndpointModelFactory($moduleUtils, $configFactory);
            },
            'ZF\ApiFirstAdmin\Model\RpcEndpointModelFactory' => function ($services) {
                if (!$services->has('ZF\Configuration\ModuleUtils')
                    || !$services->has('ZF\Configuration\ConfigResourceFactory')
                ) {
                    throw new ServiceNotCreatedException(
                        'ZF\ApiFirstAdmin\Model\RpcEndpointModelFactory is missing one or more dependencies from ZF\Configuration'
                    );
                }
                $moduleUtils   = $services->get('ZF\Configuration\ModuleUtils');
                $configFactory = $services->get('ZF\Configuration\ConfigResourceFactory');
                return new Model\RpcEndpointModelFactory($moduleUtils, $configFactory);
            },
            'ZF\ApiFirstAdmin\Model\RestEndpointResource' => function ($services) {
                if (!$services->has('ZF\ApiFirstAdmin\Model\RestEndpointModelFactory')) {
                    throw new ServiceNotCreatedException(
                        'ZF\ApiFirstAdmin\Model\RestEndpointResource is missing one or more dependencies'
                    );
                }
                $factory = $services->get('ZF\ApiFirstAdmin\Model\RestEndpointModelFactory');
                return new Model\RestEndpointResource($factory);
            },
            'ZF\ApiFirstAdmin\Model\RpcEndpointResource' => function ($services) {
                if (!$services->has('ZF\ApiFirstAdmin\Model\RpcEndpointModelFactory')) {
                    throw new ServiceNotCreatedException(
                        'ZF\ApiFirstAdmin\Model\RpcEndpointResource is missing one or more dependencies'
                    );
                }
                $factory = $services->get('ZF\ApiFirstAdmin\Model\RpcEndpointModelFactory');
                return new Model\RpcEndpointResource($factory);
            },
        ));
    }

    public function getControllerConfig()
    {
        return array('factories' => array(
            'ZF\ApiFirstAdmin\Controller\ModuleCreation' => function ($controllers) {
                $services = $controllers->getServiceLocator();
                $model    = $services->get('ZF\ApiFirstAdmin\Model\ModuleModel');
                return new Controller\ModuleCreationController($model);
            },
        ));
    }

    /**
     * Inject links into Module resources for the service endpoints
     *
     * @param  \Zend\Mvc\MvcEvent $e
     */
    public function onRender($e)
    {
        $matches = $e->getRouteMatch();
        $controller = $matches->getParam('controller', false);
        if ($controller != 'ZF\ApiFirstAdmin\Controller\ModuleResource') {
            return;
        }

        $result = $e->getResult();
        if (!$result instanceof HalJsonModel) {
            return;
        }

        if ($result->isResource()) {
            $this->injectEndpointLinks($result->getPayload(), $result);
            return;
        }

        if ($result->isCollection()) {
            $viewHelpers = $this->sm->get('ViewHelperManager');
            $halPlugin   = $viewHelpers->get('hal');
            $halPlugin->getEventManager()->attach('renderCollection.resource', array($this, 'onRenderCollectionResource'), 10);
        }
    }

    /**
     * Inject links for the service endpoints of a module
     *
     * @param  Resource $resource
     * @param  HalJsonModel $model
     */
    protected function injectEndpointLinks(Resource $resource, HalJsonModel $model)
    {
        $module = $resource->resource;
        $links  = $resource->getLinks();<commit_msg>Fixed listener for link injection<commit_after><?php

namespace ZF\ApiFirstAdmin;

use Zend\Mvc\MvcEvent;
use ZF\Hal\Link\Link;
use ZF\Hal\Link\LinkCollection;
use ZF\Hal\Resource;
use ZF\Hal\View\HalJsonModel;

class Module
{
    /**
     * @var \Zend\ServiceManager\ServiceLocatorInterface
     */
    protected $sm;

    public function onBootstrap(MvcEvent $e)
    {
        $app      = $e->getApplication();
        $this->sm = $app->getServiceManager();
        $events   = $app->getEventManager();
        $events->attach('render', array($this, 'onRender'), 100);
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__,
                ),
            ),
        );
    }

    public function getDiagnostics()
    {
        return array(
            'Config File Writable' => function () {
                if (!defined('APPLICATION_PATH')) {
                    return false;
                }
                if (!is_writable(APPLICATION_PATH . '/config/autoload/development.php')) {
                    return false;
                }
                return true;
            },
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/../../../config/module.config.php';
    }

    public function getServiceConfig()
    {
        return array('factories' => array(
            'ZF\ApiFirstAdmin\Model\ModuleModel' => function ($services) {
                if (!$services->has('ModuleManager')) {
                    throw new ServiceNotCreatedException(
                        'Cannot create ZF\ApiFirstAdmin\Model\ModuleModel service because ModuleManager service is not present'
                    );
                }
                $modules    = $services->get('ModuleManager');
                $restConfig = array();
                $rpcConfig  = array();
                if ($services->has('Config')) {
                    $config = $services->get('Config');
                    if (isset($config['zf-rest'])) {
                        $restConfig = $config['zf-rest'];
                    }
                    if (isset($config['zf-rpc'])) {
                        $rpcConfig = $config['zf-rpc'];
                    }
                }
                return new Model\ModuleModel($modules, $restConfig, $rpcConfig);
            },
            'ZF\ApiFirstAdmin\Model\ModuleResource' => function ($services) {
                $moduleModel = $services->get('ZF\ApiFirstAdmin\Model\ModuleModel');
                $listener    = new Model\ModuleResource($moduleModel);

                if ($services->has('Config')) {
                    $config = $services->get('Config');
                    if (isset($config['zf-api-first-admin'])) {
                        if (isset($config['zf-api-first-admin']['module_path'])) {
                            $listener->setModulePath($config['zf-api-first-admin']['module_path']);
                        }
                    }
                }
                return $listener;
            },
            'ZF\ApiFirstAdmin\Model\RestEndpointModelFactory' => function ($services) {
                if (!$services->has('ZF\Configuration\ModuleUtils')
                    || !$services->has('ZF\Configuration\ConfigResourceFactory')
                ) {
                    throw new ServiceNotCreatedException(
                        'ZF\ApiFirstAdmin\Model\RestEndpointModelFactory is missing one or more dependencies from ZF\Configuration'
                    );
                }
                $moduleUtils   = $services->get('ZF\Configuration\ModuleUtils');
                $configFactory = $services->get('ZF\Configuration\ConfigResourceFactory');
                return new Model\RestEndpointModelFactory($moduleUtils, $configFactory);
            },
            'ZF\ApiFirstAdmin\Model\RpcEndpointModelFactory' => function ($services) {
                if (!$services->has('ZF\Configuration\ModuleUtils')
                    || !$services->has('ZF\Configuration\ConfigResourceFactory')
                ) {
                    throw new ServiceNotCreatedException(
                        'ZF\ApiFirstAdmin\Model\RpcEndpointModelFactory is missing one or more dependencies from ZF\Configuration'
                    );
                }
                $moduleUtils   = $services->get('ZF\Configuration\ModuleUtils');
                $configFactory = $services->get('ZF\Configuration\ConfigResourceFactory');
                return new Model\RpcEndpointModelFactory($moduleUtils, $configFactory);
            },
            'ZF\ApiFirstAdmin\Model\RestEndpointResource' => function ($services) {
                if (!$services->has('ZF\ApiFirstAdmin\Model\RestEndpointModelFactory')) {
                    throw new ServiceNotCreatedException(
                        'ZF\ApiFirstAdmin\Model\RestEndpointResource is missing one or more dependencies'
                    );
                }
                $factory = $services->get('ZF\ApiFirstAdmin\Model\RestEndpointModelFactory');
                return new Model\RestEndpointResource($factory);
            },
            'ZF\ApiFirstAdmin\Model\RpcEndpointResource' => function ($services) {
                if (!$services->has('ZF\ApiFirstAdmin\Model\RpcEndpointModelFactory')) {
                    throw new ServiceNotCreatedException(
                        'ZF\ApiFirstAdmin\Model\RpcEndpointResource is missing one or more dependencies'
                    );
                }
                $factory = $services->get('ZF\ApiFirstAdmin\Model\RpcEndpointModelFactory');
                return new Model\RpcEndpointResource($factory);
            },
        ));
    }

    public function getControllerConfig()
    {
        return array('factories' => array(
            'ZF\ApiFirstAdmin\Controller\ModuleCreation' => function ($controllers) {
                $services = $controllers->getServiceLocator();
                $model    = $services->get('ZF\ApiFirstAdmin\Model\ModuleModel');
                return new Controller\ModuleCreationController($model);
            },
        ));
    }

    /**
     * Inject links into Module resources for the service endpoints
     *
     * @param  \Zend\Mvc\MvcEvent $e
     */
    public function onRender($e)
    {
        $matches = $e->getRouteMatch();
        $controller = $matches->getParam('controller', false);
        if ($controller != 'ZF\ApiFirstAdmin\Controller\Module') {
            return;
        }

        $result = $e->getResult();
        if (!$result instanceof HalJsonModel) {
            return;
        }

        if ($result->isResource()) {
            $this->injectEndpointLinks($result->getPayload(), $result);
            return;
        }

        if ($result->isCollection()) {
            $viewHelpers = $this->sm->get('ViewHelperManager');
            $halPlugin   = $viewHelpers->get('hal');
            $halPlugin->getEventManager()->attach('renderCollection.resource', array($this, 'onRenderCollectionResource'), 10);
        }
    }

    /**
     * Inject links for the service endpoints of a module
     *
     * @param  Resource $resource
     * @param  HalJsonModel $model
     */
    protected function injectEndpointLinks(Resource $resource, HalJsonModel $model)
    {
        $module = $resource->resource;
        $links  = $resource->getLinks();
{'fraction_non_alphanumeric': 0.12184986595174263, 'fraction_numerical': 0.0006702412868632708, 'mean_word_length': 1.7173556729193225, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6590083', 'n_tokens_mistral': 3794, 'n_tokens_neox': 3629, 'n_words': 637}